Documentation
¶
Index ¶
- func BroadcastMiddleware(manager *Manager) func(http.Handler) http.Handler
- func ServeWs(hub *Hub, w http.ResponseWriter, r *http.Request)
- func SetAllowedOrigins(origins []string)
- type AuthController
- type BroadcastMessage
- type Broadcaster
- type Channel
- type ChannelAuthorizer
- type ChannelCallback
- type ChannelManager
- type Client
- type Event
- type Hub
- type LogDriver
- type Manager
- type PresenceChannel
- type PresenceMember
- type PrivateChannel
- type PublicChannel
- type PusherBroadcaster
- type RedisBroadcaster
- func (r *RedisBroadcaster) Broadcast(channels []string, eventName string, payload map[string]any) error
- func (r *RedisBroadcaster) ChannelExists(channel string) bool
- func (r *RedisBroadcaster) GetChannelCount(channel string) int
- func (r *RedisBroadcaster) Subscribe(channel string) <-chan BroadcastMessage
- func (r *RedisBroadcaster) Unsubscribe(channel string, msgChan chan BroadcastMessage)
- type ServiceProvider
- type WebSocketDriver
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BroadcastMiddleware ¶
BroadcastMiddleware adds broadcasting capabilities to HTTP handlers.
func ServeWs ¶
func ServeWs(hub *Hub, w http.ResponseWriter, r *http.Request)
ServeWs handles websocket requests from the peer.
func SetAllowedOrigins ¶
func SetAllowedOrigins(origins []string)
SetAllowedOrigins configures the allowed origins for WebSocket connections.
Types ¶
type AuthController ¶
type AuthController struct {
// contains filtered or unexported fields
}
AuthController handles channel authorization for private and presence channels.
func NewAuthController ¶
func NewAuthController(authorizer ChannelAuthorizer) *AuthController
NewAuthController creates a new authorization controller.
func (*AuthController) AuthorizeRequest ¶
func (ac *AuthController) AuthorizeRequest(w http.ResponseWriter, r *http.Request)
AuthorizeRequest handles the authorization request for a channel.
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 ChannelAuthorizer ¶
ChannelAuthorizer determines if a user can access a channel.
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 PusherBroadcaster ¶
type PusherBroadcaster struct {
// contains filtered or unexported fields
}
PusherBroadcaster broadcasts events using the Pusher HTTP API.
func NewPusherBroadcaster ¶
func NewPusherBroadcaster(appID, key, secret, cluster string) *PusherBroadcaster
NewPusherBroadcaster creates a new Pusher broadcaster.
type RedisBroadcaster ¶
type RedisBroadcaster struct {
// contains filtered or unexported fields
}
RedisBroadcaster broadcasts events using Redis pub/sub.
func NewRedisBroadcaster ¶
func NewRedisBroadcaster(url string) *RedisBroadcaster
NewRedisBroadcaster creates a new Redis broadcaster. The url parameter should be in the format "redis://host:port/db".
func (*RedisBroadcaster) Broadcast ¶
func (r *RedisBroadcaster) Broadcast(channels []string, eventName string, payload map[string]any) error
Broadcast sends a message to the specified channels.
func (*RedisBroadcaster) ChannelExists ¶
func (r *RedisBroadcaster) ChannelExists(channel string) bool
ChannelExists checks if a channel has any subscribers.
func (*RedisBroadcaster) GetChannelCount ¶
func (r *RedisBroadcaster) GetChannelCount(channel string) int
GetChannelCount returns the number of subscribers for a channel.
func (*RedisBroadcaster) Subscribe ¶
func (r *RedisBroadcaster) Subscribe(channel string) <-chan BroadcastMessage
Subscribe subscribes to a channel and returns a channel to receive messages.
func (*RedisBroadcaster) Unsubscribe ¶
func (r *RedisBroadcaster) Unsubscribe(channel string, msgChan chan BroadcastMessage)
Unsubscribe removes a subscription from a channel.
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.