presence

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

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) Broadcast

func (ch *Channel) Broadcast(event Event)

Broadcast sends an event to everyone in the channel.

func (*Channel) BroadcastExcept

func (ch *Channel) BroadcastExcept(event Event, exceptUserID string)

BroadcastExcept sends to everyone in the channel except one user.

func (*Channel) Count

func (ch *Channel) Count() int

Count returns the number of distinct users in the channel.

func (*Channel) Join

func (ch *Channel) Join(client *Client)

Join adds a connection to the channel. Other members get presence:join only for the user's first connection; a second tab joins silently.

func (*Channel) Leave

func (ch *Channel) Leave(client *Client)

Leave removes a connection from the channel. Members get presence:leave only when it was the user's last connection.

func (*Channel) Users

func (ch *Channel) Users() []User

Users returns the distinct users in the channel, across instances.

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 NewHub

func NewHub(cfg Config) *Hub

NewHub creates a new presence hub.

func (*Hub) Broadcast

func (h *Hub) Broadcast(channel string, event Event)

Broadcast sends an event to everyone in a channel.

func (*Hub) BroadcastExcept

func (h *Hub) BroadcastExcept(channel string, event Event, exceptUserID string)

BroadcastExcept sends an event to everyone in a channel except one user (all of that user's connections).

func (*Hub) Channels

func (h *Hub) Channels() []string

Channels returns the names of channels with anyone in them.

func (*Hub) Close added in v1.9.1

func (h *Hub) Close() error

Close stops cross-instance syncing. Connections stay open.

func (*Hub) GetChannel

func (h *Hub) GetChannel(name string) *Channel

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

func (h *Hub) SendTo(channel string, event Event, userID string)

SendTo sends an event to every connection of one user in a channel.

func (*Hub) UserCount

func (h *Hub) UserCount(channel string) int

UserCount returns the number of distinct users in a channel.

func (*Hub) UsersIn

func (h *Hub) UsersIn(channel string) []User

UsersIn returns the users in a channel, or nil if it is empty.

type PresencePlugin

type PresencePlugin struct {
	nimbus.BasePlugin
	Hub *Hub
}

PresencePlugin integrates presence channels with Nimbus.

func NewPlugin

func NewPlugin(cfg Config) *PresencePlugin

NewPlugin creates a presence plugin.

func (*PresencePlugin) Boot

func (p *PresencePlugin) Boot(app *nimbus.App) error

func (*PresencePlugin) Register

func (p *PresencePlugin) Register(app *nimbus.App) error

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.

type User

type User struct {
	ID       string            `json:"id"`
	Name     string            `json:"name,omitempty"`
	Avatar   string            `json:"avatar,omitempty"`
	Metadata map[string]string `json:"metadata,omitempty"`
}

User represents a connected user in a presence channel.

Jump to

Keyboard shortcuts

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