reverb

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

README

Reverb (WebSocket broadcasting)

Laravel Reverb–style WebSocket channels for Nimbus. Pairs with Transmit (SSE): use SSE for one-way HTTP-friendly streams, and Reverb when you need interactive WebSocket subscriptions.

Install

nimbus plugin install reverb

Or in bin/server.go:

import "github.com/CodeSyncr/nimbus/plugins/reverb"

app.Use(reverb.New(nil))

Configure

Env Purpose
REDIS_URL Cross-instance fan-out (Pub/Sub). Without it, broadcasts are single-process only.
REVERB_PATH WebSocket route (default /reverb/ws)
REVERB_ALLOWED_ORIGINS Comma-separated Origin hosts allowed to upgrade (default: same host as request)
REVERB_REDIS_CHANNEL Override Redis channel (default nimbus:reverb:fanout)

Server → clients

import "context"

_ = reverb.Broadcast(context.Background(), "orders.1", map[string]any{
    "status": "shipped",
})

Browser protocol

  1. GET WebSocket ws://host/reverb/ws (or your REVERB_PATH).
  2. Send JSON text frames:
{"action":"subscribe","channel":"orders.1"}
{"action":"unsubscribe","channel":"orders.1"}
{"action":"ping"}
  1. Server events:
  • connected — handshake ok
  • subscribed / unsubscribed
  • message{ "event":"message", "channel":"...", "data": ... }
  • pong

Health

GET {base}/health (e.g. /reverb/health when WS path is /reverb/ws) returns {"reverb":true,"redis_fanout":...}.

Advanced

reverb.GetHub() returns the live *Hub (or nil) if you need direct access.

Optional gate

reverb.New(&reverb.Config{
    Gate: func(c *http.Context) bool { return c.Auth != nil },
})

Prefer route-level middleware for session auth when possible.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Broadcast

func Broadcast(ctx context.Context, channel string, data any) error

Broadcast sends to all subscribers on channel across instances when Redis is configured.

Types

type Config

type Config struct {
	// Path is the WebSocket upgrade route (GET). Default /reverb/ws
	Path string
	// RedisURL enables cross-instance fan-out (same as REDIS_URL if empty).
	RedisURL string
	// AllowedOrigins restricts WS Origin header; empty allows same-host only.
	AllowedOrigins []string
	// FanoutChannel overrides the Redis Pub/Sub channel name.
	FanoutChannel string
	// Gate authorizes the upgrade request. Nil = allow (use middleware on the route group for auth).
	Gate func(*http.Context) bool
}

Config configures the Reverb plugin.

type Hub

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

Hub tracks WebSocket subscribers per logical channel.

func GetHub

func GetHub() *Hub

GetHub returns the active hub (for advanced use). May be nil if the plugin is not registered.

func (*Hub) Broadcast

func (h *Hub) Broadcast(ctx context.Context, channel string, data any) error

Broadcast sends a JSON-serializable payload to every WebSocket subscribed to channel. With Redis configured, delivery happens via Pub/Sub so every instance (including this one) receives exactly one fanout per publish. Without Redis, delivery is local-only.

type Plugin

type Plugin struct {
	nimbus.BasePlugin
	// contains filtered or unexported fields
}

Plugin provides WebSocket broadcasting.

func New

func New(cfg *Config) *Plugin

New creates a Reverb plugin with defaults from environment.

func (*Plugin) Boot

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

Boot is reserved.

func (*Plugin) Register

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

Register exposes the hub for Broadcast.

func (*Plugin) RegisterRoutes

func (p *Plugin) RegisterRoutes(r *router.Router)

RegisterRoutes mounts the WebSocket endpoint.

func (*Plugin) Shutdown

func (p *Plugin) Shutdown() error

Shutdown closes Redis subscriptions.

Jump to

Keyboard shortcuts

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