presence

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 12 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 represents a single presence channel.

func (*Channel) Broadcast

func (ch *Channel) Broadcast(event Event)

Broadcast sends a message to all clients in the channel.

func (*Channel) BroadcastExcept

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

BroadcastExcept sends to all clients except one.

func (*Channel) Count

func (ch *Channel) Count() int

Count returns the number of connected users.

func (*Channel) Join

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

Join adds a client to the channel.

func (*Channel) Leave

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

Leave removes a client from the channel.

func (*Channel) Users

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

Users returns all users in the channel.

type Client

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

Client represents a single websocket connection.

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
}

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 all presence channels and their connected clients.

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 a message to all clients in a channel.

func (*Hub) BroadcastExcept

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

BroadcastExcept sends a message to all clients except the specified user.

func (*Hub) Channels

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

Channels returns all active channel names.

func (*Hub) GetChannel

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

GetChannel returns a channel if it exists, 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) UserCount

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

UserCount returns the number of users in a channel.

func (*Hub) UsersIn

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

UsersIn returns all users currently in a channel.

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.

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