Documentation
¶
Overview ¶
Package ws wires WebSocket endpoints onto a Gin engine using gorilla/websocket and records metadata about them in the nexus registry.
Index ¶
- Constants
- type Builder
- func (b *Builder) Describe(s string) *Builder
- func (b *Builder) Mount()
- func (b *Builder) OnClose(fn CloseFunc) *Builder
- func (b *Builder) OnConnect(fn ConnectFunc) *Builder
- func (b *Builder) OnMessage(fn MessageFunc) *Builder
- func (b *Builder) Tag(k, v string) *Builder
- func (b *Builder) Upgrader(u websocket.Upgrader) *Builder
- func (b *Builder) Use(name string, mw gin.HandlerFunc) *Builder
- func (b *Builder) WithHub(h *Hub) *Builder
- type CloseFunc
- type ConnectFunc
- type Connection
- type Event
- type EventTarget
- type Hub
- func (h *Hub) BroadcastRaw(data []byte)
- func (h *Hub) ConnectionCount() int
- func (h *Hub) Emit(e *Event)
- func (h *Hub) EmitBroadcast(eventType string, data any)
- func (h *Hub) EmitToClients(eventType string, data any, clientIDs ...string)
- func (h *Hub) EmitToRoom(eventType string, data any, room string)
- func (h *Hub) EmitToUsers(eventType string, data any, userIDs ...string)
- func (h *Hub) Join(conn *Connection, room string)
- func (h *Hub) Leave(conn *Connection, room string)
- func (h *Hub) OnConnect(fn OnConnectFunc) *Hub
- func (h *Hub) OnDisconnect(fn OnDisconnectFunc) *Hub
- func (h *Hub) OnIdentify(fn IdentifyFunc) *Hub
- func (h *Hub) OnMessage(fn OnMessageFunc) *Hub
- func (h *Hub) RoomConnectionCount(room string) int
- func (h *Hub) Rooms() map[string]int
- func (h *Hub) Serve(gctx *gin.Context, upgrader websocket.Upgrader)
- func (h *Hub) ServeGin(gctx *gin.Context)
- func (h *Hub) Start(ctx context.Context)
- func (h *Hub) Stop()
- func (h *Hub) UserConnectionCount(userID string) int
- type HubOption
- type IdentifyFunc
- type MessageFunc
- type OnConnectFunc
- type OnDisconnectFunc
- type OnMessageFunc
Constants ¶
const ( EventTypeConnected = "connection.established" EventTypeDisconnected = "connection.closed" EventTypeError = "error" EventTypePong = "pong" EventTypeSubscribed = "subscribed" EventTypeUnsubscribed = "unsubscribed" EventTypeAuthed = "authenticated" )
Built-in event types. Keep the string values stable — clients depend on them.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
func (*Builder) Mount ¶
func (b *Builder) Mount()
Mount attaches the WebSocket endpoint to Gin and records it in the registry. Terminal.
Tracing: NO trace.Middleware on the upgrade route. WS upgrade is a one-time HTTP request that promotes to a long-lived connection; wrapping it in request.start/request.end would keep one trace open for the entire connection lifetime (until close) and produce no renderable spans. Per-frame traces are emitted inside b.serve's read loop instead — each frame becomes its own root trace on the dashboard's waterfall, matching how AsWS's typed dispatcher does it.
func (*Builder) OnConnect ¶
func (b *Builder) OnConnect(fn ConnectFunc) *Builder
func (*Builder) OnMessage ¶
func (b *Builder) OnMessage(fn MessageFunc) *Builder
func (*Builder) WithHub ¶
WithHub hands connection management to a Hub: rooms, user/client-targeted events, broadcast, worker-pool fan-out, slow-client backpressure, and the default subscribe/authenticate/ping message protocol all become available. When a hub is set, OnConnect/OnMessage/OnClose on the Builder are ignored — install hooks on the Hub instead (hub.OnMessage, hub.OnConnect, ...).
type Connection ¶
type Connection struct {
ClientID string
UserID string
Metadata map[string]any
// contains filtered or unexported fields
}
Connection is one authenticated WebSocket client.
func (*Connection) Send ¶
func (c *Connection) Send(message []byte)
Send queues a raw message. If the send buffer is repeatedly full the hub closes the connection — slow clients do not hold up the fan-out loop.
func (*Connection) SendEvent ¶
func (c *Connection) SendEvent(e *Event) error
SendEvent marshals and sends an Event to this single connection.
type Event ¶
type Event struct {
Type string `json:"type"`
Data any `json:"data"`
Timestamp int64 `json:"timestamp"`
Target *EventTarget `json:"-"`
}
Event is the envelope the hub sends to clients. Target decides recipients.
func (*Event) ToBroadcast ¶
type EventTarget ¶
EventTarget specifies which connections receive an Event.
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub is a pub/sub engine for a pool of WebSocket connections with rooms, user-/client-targeted events, and a fan-out worker pool.
func NewHub ¶
NewHub creates a hub with default settings. Call Start() (or pass it to a Builder that calls Start for you) before mounting endpoints on it.
func (*Hub) BroadcastRaw ¶ added in v0.9.0
BroadcastRaw queues a raw byte payload to every connection without wrapping it in an Event envelope. Intended for higher-level APIs (e.g. AsWS) that manage their own envelope — drop an already-marshalled message straight onto the fan-out worker pool. Drops silently if the broadcast channel is full.
func (*Hub) ConnectionCount ¶
func (*Hub) Emit ¶
Emit queues an event for fan-out. Dropped silently if the event channel is full — callers trade completeness for backpressure.
func (*Hub) EmitBroadcast ¶
func (*Hub) EmitToClients ¶
func (*Hub) Join ¶
func (h *Hub) Join(conn *Connection, room string)
Join subscribes a connection to a room. Idempotent.
func (*Hub) Leave ¶
func (h *Hub) Leave(conn *Connection, room string)
Leave unsubscribes a connection from a room.
func (*Hub) OnConnect ¶
func (h *Hub) OnConnect(fn OnConnectFunc) *Hub
func (*Hub) OnDisconnect ¶
func (h *Hub) OnDisconnect(fn OnDisconnectFunc) *Hub
func (*Hub) OnMessage ¶
func (h *Hub) OnMessage(fn OnMessageFunc) *Hub
func (*Hub) RoomConnectionCount ¶
func (*Hub) Rooms ¶
Rooms returns a snapshot of room names and their member counts. Cheap for dashboards; do not call on the hot path.
func (*Hub) Serve ¶ added in v0.9.0
Serve handles one HTTP upgrade request using the supplied upgrader: it upgrades the connection, runs the identify hook, registers the conn with the hub, and starts the read/write pumps. Exposed for callers (notably the nexus.AsWS path) that mount the hub on their own gin route and need the upgrade behavior without going through ws.Builder.
For the no-op upgrader default (AllowAll origins), see ServeGin — a zero-config sibling.
func (*Hub) ServeGin ¶ added in v0.9.0
ServeGin is Serve with the hub's default upgrader (CheckOrigin: always true). Convenient for callers that don't need to customize upgrade behavior — matches the upgrader the Builder installs.
func (*Hub) Start ¶
Start spins up the hub's main loop and the broadcast worker pool. Safe to call repeatedly — only the first call starts goroutines.
func (*Hub) Stop ¶
func (h *Hub) Stop()
Stop cancels the hub context, closes every connection, and returns. Idempotent.
func (*Hub) UserConnectionCount ¶
type HubOption ¶
type HubOption func(*hubConfig)
HubOption configures a Hub at construction.
func WithEventBuffer ¶
func WithLogger ¶
func WithMaxConnections ¶
func WithMaxMessageSize ¶
func WithPingPong ¶
func WithWorkers ¶
type IdentifyFunc ¶
Hooks the caller can install.
type OnMessageFunc ¶
type OnMessageFunc func(conn *Connection, msgType int, data []byte) error
Hooks the caller can install.