Documentation
¶
Index ¶
- type Channel
- type Client
- type Config
- type Event
- type Hub
- func (h *Hub) Broadcast(channel string, event Event)
- func (h *Hub) BroadcastExcept(channel string, event Event, exceptUserID string)
- func (h *Hub) Channels() []string
- func (h *Hub) Close() error
- func (h *Hub) GetChannel(name string) *Channel
- func (h *Hub) HandleWebSocket(w http.ResponseWriter, r *http.Request)
- func (h *Hub) SendTo(channel string, event Event, userID string)
- func (h *Hub) UserCount(channel string) int
- func (h *Hub) UsersIn(channel string) []User
- type PresencePlugin
- type User
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Channel ¶
type Channel struct {
// contains filtered or unexported fields
}
Channel is a presence channel as seen from one hub. Membership queries (Users, Count) cover every instance; Broadcast reaches every instance.
func (*Channel) BroadcastExcept ¶
BroadcastExcept sends to everyone in the channel except one user.
func (*Channel) Join ¶
Join adds a connection to the channel. Other members get presence:join only for the user's first connection; a second tab joins silently.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents a single websocket connection. A user may have several (one per tab or device).
type Config ¶
type Config struct {
// AuthFunc authenticates a user for a specific channel.
// Return nil user to reject the connection.
AuthFunc func(r *http.Request, channel string) (*User, error)
// PingInterval controls how often to send ping frames (default: 30s).
PingInterval time.Duration
// WriteTimeout for websocket writes (default: 10s).
WriteTimeout time.Duration
// MaxMessageSize in bytes (default: 4096).
MaxMessageSize int64
// Path for the websocket endpoint (default: "/_presence").
Path string
// AllowedOrigins controls accepted websocket Origin hosts.
// When empty, same-origin requests are allowed by default.
AllowedOrigins []string
// Redis shares membership and events between app instances. Without
// it, each instance only sees its own connections.
Redis *redis.Client
// RedisPrefix namespaces the Redis keys and Pub/Sub topic (default
// "nimbus:presence:"). Hubs that should see each other must match.
RedisPrefix string
}
Config configures the presence hub.
type Event ¶
type Event struct {
Type string `json:"type"`
Channel string `json:"channel,omitempty"`
User *User `json:"user,omitempty"`
Users []User `json:"users,omitempty"`
Data any `json:"data,omitempty"`
}
Event is a message sent over the presence channel.
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub manages presence channels and the connections this instance serves. With Config.Redis set, membership and events are shared by every hub using the same Redis, so users connected to different app instances see each other.
func (*Hub) BroadcastExcept ¶
BroadcastExcept sends an event to everyone in a channel except one user (all of that user's connections).
func (*Hub) GetChannel ¶
GetChannel returns a channel if anyone is in it, nil otherwise.
func (*Hub) HandleWebSocket ¶
func (h *Hub) HandleWebSocket(w http.ResponseWriter, r *http.Request)
HandleWebSocket upgrades an HTTP connection and joins a presence channel.
func (*Hub) SendTo ¶ added in v1.9.1
SendTo sends an event to every connection of one user in a channel.
type PresencePlugin ¶
type PresencePlugin struct {
nimbus.BasePlugin
Hub *Hub
}
PresencePlugin integrates presence channels with Nimbus.
func (*PresencePlugin) RegisterRoutes ¶
func (p *PresencePlugin) RegisterRoutes(r *router.Router)
RegisterRoutes mounts presence endpoints.
func (*PresencePlugin) Shutdown ¶ added in v1.9.1
func (p *PresencePlugin) Shutdown() error
Shutdown stops cross-instance syncing.