websocket

package
v1.9.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const DefaultRedisTopic = "nimbus:websocket:broadcast"

DefaultRedisTopic is the Pub/Sub topic hubs use to share broadcasts.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	*websocket.Conn
	// contains filtered or unexported fields
}

Conn wraps a websocket connection.

type Hub

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

Hub holds connected clients and broadcasts to them. With UseRedis, a broadcast on any instance reaches the clients of every instance.

func NewHub

func NewHub() *Hub

NewHub returns a new hub. Call Run() to start.

func (*Hub) Broadcast

func (h *Hub) Broadcast(msg []byte)

Broadcast sends a message to all connected clients (of every instance, with UseRedis).

func (*Hub) Close added in v1.9.1

func (h *Hub) Close() error

Close stops the Redis subscription, if any.

func (*Hub) Len added in v1.9.1

func (h *Hub) Len() int

Len returns the number of clients connected to this instance.

func (*Hub) Run

func (h *Hub) Run()

Run runs the hub (blocks). Call in a goroutine.

func (*Hub) SetAllowedOrigins

func (h *Hub) SetAllowedOrigins(origins []string)

SetAllowedOrigins configures allowed websocket origins. When empty, same-origin requests are allowed by default.

func (*Hub) Upgrade

func (h *Hub) Upgrade(w http.ResponseWriter, r *http.Request) (*Conn, error)

Upgrade upgrades the HTTP request to WebSocket and registers the conn with the hub.

func (*Hub) UseRedis added in v1.9.1

func (h *Hub) UseRedis(client *redis.Client, topic string) error

UseRedis makes Broadcast go through Redis Pub/Sub so every hub subscribed to topic (default DefaultRedisTopic) delivers it to its own clients. Call before Run.

Jump to

Keyboard shortcuts

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