Documentation
¶
Index ¶
- func ServeWs(hub *Hub, w http.ResponseWriter, r *http.Request)
- type BroadcastMessage
- type Broadcaster
- type Channel
- type ChannelCallback
- type ChannelManager
- type Client
- type Event
- type Hub
- type LogDriver
- type Manager
- type PresenceChannel
- type PresenceMember
- type PrivateChannel
- type PublicChannel
- type ServiceProvider
- type WebSocketDriver
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BroadcastMessage ¶
type BroadcastMessage struct {
Event string `json:"event"`
Channel string `json:"channel"`
Data any `json:"data"`
}
BroadcastMessage defines the custom JSON protocol format.
type Broadcaster ¶
type Broadcaster interface {
Broadcast(channels []string, eventName string, payload map[string]any) error
}
Broadcaster handles the actual transmission of the event.
type Channel ¶
type Channel interface {
Name() string
}
Channel represents an abstract broadcasting channel (e.g. public, private, presence).
type ChannelCallback ¶
ChannelCallback is invoked when a user attempts to authenticate to a private or presence channel. `user` is the currently authenticated user (from context). `authorized` should be true if they can join. `data` is the PresenceMember user info (only required for presence channels).
type ChannelManager ¶
type ChannelManager struct {
// contains filtered or unexported fields
}
ChannelManager stores authorization callbacks for private and presence channels.
func NewChannelManager ¶
func NewChannelManager() *ChannelManager
NewChannelManager creates a new ChannelManager.
func (*ChannelManager) Authorize ¶
Authorize resolves the channel pattern and executes the callback.
func (*ChannelManager) Channel ¶
func (m *ChannelManager) Channel(pattern string, callback ChannelCallback)
Channel registers an authorization callback for a given channel pattern. Supports exact matches, wildcard "chat.*", and brace patterns "chat.{id}".
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a middleman between the websocket connection and the hub.
type Event ¶
type Event interface {
BroadcastOn() []Channel
BroadcastAs() string
BroadcastWith() map[string]any
}
Event represents an event that can be broadcasted.
type Hub ¶
type Hub struct {
Broadcast chan BroadcastMessage
Register chan *Client
Unregister chan *Client
// contains filtered or unexported fields
}
Hub maintains the set of active clients and broadcasts messages to the channels.
type LogDriver ¶
type LogDriver struct{}
LogDriver outputs the broadcast to the standard logger for local development.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager resolves broadcast drivers and dispatches events.
func NewManager ¶
NewManager creates a new Broadcaster Manager.
func (*Manager) Connection ¶
func (m *Manager) Connection(name string) Broadcaster
Connection gets a driver by name.
func (*Manager) Extend ¶
func (m *Manager) Extend(name string, driver Broadcaster)
Extend registers a custom driver (e.g., Pusher, Redis, WebSocket).
type PresenceChannel ¶
type PresenceChannel struct {
// contains filtered or unexported fields
}
PresenceChannel represents a channel requiring authorization and tracking users.
func NewPresenceChannel ¶
func NewPresenceChannel(name string) *PresenceChannel
func (*PresenceChannel) Name ¶
func (c *PresenceChannel) Name() string
type PresenceMember ¶
type PresenceMember struct {
UserID string `json:"user_id"`
UserInfo map[string]any `json:"user_info"`
}
PresenceMember defines a member in a presence channel.
type PrivateChannel ¶
type PrivateChannel struct {
// contains filtered or unexported fields
}
PrivateChannel represents a channel requiring authorization.
func NewPrivateChannel ¶
func NewPrivateChannel(name string) *PrivateChannel
func (*PrivateChannel) Name ¶
func (c *PrivateChannel) Name() string
type PublicChannel ¶
type PublicChannel struct {
// contains filtered or unexported fields
}
PublicChannel represents a channel anyone can subscribe to.
func NewPublicChannel ¶
func NewPublicChannel(name string) *PublicChannel
func (*PublicChannel) Name ¶
func (c *PublicChannel) Name() string
type ServiceProvider ¶
type ServiceProvider struct {
foundation.BaseServiceProvider
}
ServiceProvider bootstraps the native WebSocket broadcasting system. It creates a Hub (started in background), the WebSocketDriver, registers the driver with the broadcasting Manager (if present in container), and binds both for dependency injection.
To wire the WebSocket endpoint, register the route in your application routes:
hub := app.Make(broadcasting.Hub{})
router.Any("/ws", func(w http.ResponseWriter, r *http.Request) {
broadcasting.ServeWs(hub, w, r)
})
The /ws path is conventional for native WebSocket connections.
func (*ServiceProvider) Boot ¶
func (p *ServiceProvider) Boot(app *foundation.Application)
Boot can be used to attach shutdown hooks etc.
func (*ServiceProvider) Register ¶
func (p *ServiceProvider) Register(app *foundation.Application)
Register sets up the Hub and WebSocket driver.
type WebSocketDriver ¶
type WebSocketDriver struct {
// contains filtered or unexported fields
}
WebSocketDriver implements the Broadcaster interface to push messages into the native WebSocket server Hub.
func NewWebSocketDriver ¶
func NewWebSocketDriver(hub *Hub) *WebSocketDriver
NewWebSocketDriver creates a new WebSocket driver.