Help us shape the future of Go! Take the 2026 Go Developer Survey

websocket

package module
v0.0.0-...-59a70be Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package websocket provides a Hub + Connection abstraction over WebSockets, modelled on Laravel Echo's room/channel semantics.

The adapter is a separate Go module so the core lagodev module stays dependency-free:

go get github.com/devituz/lagodev/adapters/websocket@latest

Architecture:

  • Hub — the per-app singleton. Tracks all live connections by ID and by channel (Laravel "room").
  • Connection — a single open WebSocket; sends are non-blocking with a bounded outbox.
  • Handler — http.Handler that performs the WebSocket handshake and registers the connection on the Hub.

Usage:

hub := websocket.NewHub()
go hub.Run(ctx)

mux.Handle("/ws", hub.Handler(func(ctx context.Context, c *websocket.Connection, m []byte) error {
    // m is one incoming text/binary message
    return hub.Broadcast(ctx, "chat.42", m)
}))

// later, from anywhere:
_ = hub.Broadcast(ctx, "chat.42", []byte(`{"text":"hello"}`))

The connection lifecycle (join channel, receive, broadcast, leave) is fully exposed so applications can layer auth, presence, or per-message ACK semantics on top.

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("websocket: closed")

ErrClosed is returned when an operation is attempted on a closed Hub or Connection.

Functions

This section is empty.

Types

type Connection

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

Connection represents one live WebSocket. Methods are safe for concurrent use unless otherwise noted.

func (*Connection) Close

func (c *Connection) Close()

Close gracefully terminates the connection.

func (*Connection) Dropped

func (c *Connection) Dropped() uint64

Dropped returns how many outbound payloads have been dropped due to a full outbox. Useful as a saturation metric.

func (*Connection) ID

func (c *Connection) ID() string

ID returns the connection's identifier within its Hub.

func (*Connection) Join

func (c *Connection) Join(channel string)

Join adds the connection to a channel. Idempotent.

func (*Connection) Leave

func (c *Connection) Leave(channel string)

Leave removes the connection from a channel. Idempotent.

func (*Connection) Send

func (c *Connection) Send(payload []byte)

Send queues payload for delivery to the client. Non-blocking — a full outbox drops the payload and bumps the dropped counter.

type Hub

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

Hub orchestrates Connections.

func NewHub

func NewHub(opts ...HubOption) *Hub

NewHub returns an empty Hub.

func (*Hub) Broadcast

func (h *Hub) Broadcast(_ context.Context, channel string, payload []byte) error

Broadcast sends payload to every connection joined to channel. Per-connection delivery is best-effort: full outboxes drop the message and increment the connection's drop counter.

func (*Hub) ChannelCount

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

ChannelCount returns the number of subscribers on channel.

func (*Hub) Close

func (h *Hub) Close()

Close terminates every open connection. After Close, further Handler calls accept handshakes but immediately reject them.

func (*Hub) Count

func (h *Hub) Count() int

Count returns the number of live connections.

func (*Hub) Handler

func (h *Hub) Handler(onMessage MessageHandler) http.Handler

Handler returns an http.Handler that upgrades the request to a WebSocket connection, registers it on the Hub, and runs onMessage for each incoming frame until the connection closes.

func (*Hub) Run

func (h *Hub) Run(ctx context.Context)

Run is reserved for future per-Hub housekeeping (cleanup tickers, metrics flush, …). It blocks until ctx is done; safe to omit if nothing else uses the Hub's background ticks.

func (*Hub) SendTo

func (h *Hub) SendTo(connID string, payload []byte) error

SendTo addresses a single connection by ID.

type HubOption

type HubOption func(*Hub)

HubOption customises Hub construction.

func WithOrigins

func WithOrigins(origins ...string) HubOption

WithOrigins restricts the handshake's Origin header. Empty list disables the check (DEV ONLY). Default empty.

func WithOutbox

func WithOutbox(n int) HubOption

WithOutbox sets the per-connection outbound buffer size. Default 64.

func WithPingInterval

func WithPingInterval(d time.Duration) HubOption

WithPingInterval sets the server-side keep-alive ping cadence. Default 30s.

type MessageHandler

type MessageHandler func(ctx context.Context, c *Connection, message []byte) error

MessageHandler receives each frame the client sends. Return non-nil to close the connection.

Jump to

Keyboard shortcuts

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