broadcasting

package
v0.0.0-...-599b610 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ServeWs

func ServeWs(hub *Hub, w http.ResponseWriter, r *http.Request)

ServeWs handles websocket requests from the peer.

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

type ChannelCallback func(user any, channelName string) (authorized bool, data map[string]any)

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

func (m *ChannelManager) Authorize(user any, channelName string) (bool, map[string]any)

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.

func NewHub

func NewHub() *Hub

NewHub creates a new Hub instance.

func (*Hub) Run

func (h *Hub) Run()

Run starts the hub's main event loop.

func (*Hub) Stop

func (h *Hub) Stop()

Stop signals the hub to stop its Run loop for graceful shutdown.

type LogDriver

type LogDriver struct{}

LogDriver outputs the broadcast to the standard logger for local development.

func (*LogDriver) Broadcast

func (d *LogDriver) Broadcast(channels []string, eventName string, payload map[string]any) error

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager resolves broadcast drivers and dispatches events.

func NewManager

func NewManager(defaultDriver string) *Manager

NewManager creates a new Broadcaster Manager.

func (*Manager) Broadcast

func (m *Manager) Broadcast(channels []string, eventName string, payload map[string]any) error

Broadcast dispatches the event via the default connection.

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.

func (*WebSocketDriver) Broadcast

func (d *WebSocketDriver) Broadcast(channels []string, eventName string, payload map[string]any) error

Broadcast sends the event payload to the specified channels via the Hub.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL