ws

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

togo

togo-framework/realtime-ws

marketplace pkg.go.dev MIT

Part of the togo framework.

Install

togo install togo-framework/realtime-ws

togo

realtime-ws

part of the togo-framework — the full-stack Go + React framework

Channel-based WebSocket transport for togo realtime (implements togo.Broker, overrides the default SSE broker when installed).

togo install togo-framework/realtime-ws

Channels & broadcasting

  • Clients subscribe over the socket: {"action":"subscribe","channel":"orders"}.
  • Public channels: any name (no private- prefix), subscribable by anyone.
  • Private channels: prefix private-. Subscribing requires a ticket whose grants match the channel — either an exact name or a prefix* glob (e.g. a ticket granted private-driver.42* may subscribe to private-driver.42.location).
  • Channel names over 200 bytes are rejected with a subscription_error frame.
  • Broadcast to a channel by emitting "channel:event" (e.g. app.Emit(ctx, "orders:created", o)). A plain event (no :) is an unchanneled broadcast and only reaches connections that presented a valid ticket — anonymous connections never see it.

Tickets (private channels)

GET /events/ticket returns a short-lived (60s), HMAC-signed ticket scoped to the channels it grants. You mount it, behind your auth, so only authenticated users get one:

ws.MountTicket(k, authSvc.Middleware)

Tell the package which channels each caller's ticket should grant — this is what makes a ticket useless for anyone else's private channel:

ws.SetTicketChannels(func(r *http.Request) []string {
	id := auth.Identity(r.Context()) // however your app resolves the caller
	return []string{"private-driver." + id.UserID} // exact names and/or "prefix*" globs
})

If SetTicketChannels is never called, IssueTicket grants no private channels (secure by default) and logs a one-time warning. Call ws.IssueTicketFor(w, channels) directly instead if you'd rather skip SetTicketChannels and decide the grants inline.

The client connects with /events?ticket=...; a valid ticket unlocks subscriptions to private-* channels it was granted. Set REALTIME_SECRET so tickets survive a restart.

The provider mounts no routes of its own, on purpose. A provider that mounts a route makes chi's mux "have routes", after which any plugin calling Router.Use — togo's auth plugin does, for CORS — panics with "all middlewares must be defined before routes on a mux". Leaving the mount to the app keeps this package from breaking another plugin's boot at any priority.

Origin checking

websocket.Accept no longer disables origin verification. Set REALTIME_ORIGINS to a comma-separated list of host patterns (as understood by coder/websocket's OriginPatterns, e.g. app.example.com,localhost:*) to allow cross-origin connections. If unset, only same-host requests are accepted (coder/websocket's default check).

Per-client buffer

Each connection has a 256-frame outbound buffer, sized for high-frequency streams such as driver location updates. If a client's reader falls behind and the buffer fills, further frames for that client are dropped, not queued or blocked on — the client should treat gaps as expected and resync (e.g. re-fetch state) on reconnect.

Breaking changes in v0.4.0

  • Tickets are now scoped. The ticket format changed from nonce.expiry.sig to a base64url JSON payload ({"c":[channels],"e":expiry,"n":nonce}) plus . and an HMAC signature. A valid ticket only unlocks the private-* channels it was issued for, not every private-* channel. Old tickets are incompatible with this version.
  • IssueTicket grants nothing by default. Call ws.SetTicketChannels(...) during boot, or your users' tickets will not be able to subscribe to any private channel.
  • Unchanneled broadcasts are no longer sent to anonymous connections. A Publish call with no channel: prefix now reaches only connections that presented a valid ticket.
  • websocket.Accept no longer sets InsecureSkipVerify. Cross-origin browser clients now need REALTIME_ORIGINS set, or same-host requests only are accepted.
  • The per-client buffer grew from 32 to 256 frames. Frames are still dropped, not queued, once the buffer is full.
  • New exported API: ws.IssueTicketFor(w, channels) and ws.SetTicketChannels(f).

Upgrading from v0.2.0 — /events/ticket was mounted for you (unauthenticated, and it made any app using togo auth panic on boot). Call ws.MountTicket yourself now. IssueTicket is exported if you would rather mount it by hand.


💎 Premium sponsors

togo is proudly sponsored by ID8 Media and One Studio.

ID8 Media        One Studio


Premium sponsors

ID8 Media  ·  One Studio

Support togo — become a sponsor.

Documentation

Overview

Package ws is a channel-based WebSocket transport for togo realtime. It supports public and private channels, signed connection tickets (so private channels are authenticated and scoped to the channels the ticket was granted), and channel-scoped broadcasting. It implements togo.Broker and overrides the default SSE broker when installed.

Publish uses an event of the form "channel:event" to target a channel (private channels are prefixed "private-"); an event with no ":" broadcasts to every connection that presented a valid ticket (unauthenticated connections never see unchanneled broadcasts).

Install: `togo install togo-framework/realtime-ws`. Env: REALTIME_SECRET, REALTIME_ORIGINS.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IssueTicket added in v0.4.0

func IssueTicket(w http.ResponseWriter, r *http.Request)

IssueTicket returns a signed ticket for private-channel access. Mount it behind the app's auth middleware — see MountTicket — so only authenticated users receive one.

The channels it grants come from SetTicketChannels, called with this request. If SetTicketChannels was never configured, the ticket grants no private channels (secure by default) and a one-time warning is logged.

func IssueTicketFor added in v0.4.0

func IssueTicketFor(w http.ResponseWriter, channels []string)

IssueTicketFor writes a ticket granting exactly the given channels. Use it to hand-roll ticket issuance instead of going through SetTicketChannels.

func MountTicket added in v0.4.0

func MountTicket(k *togo.Kernel, mw ...func(http.Handler) http.Handler)

MountTicket mounts IssueTicket at /events/ticket wrapped in the middleware you pass — which should be your auth, so only authenticated users get a ticket.

The provider deliberately does not do this for you. A provider that mounts a route makes chi's mux "have routes", and any plugin that later calls Router.Use — togo's own auth plugin does, for CORS — then panics with "chi: all middlewares must be defined before routes on a mux". Leaving the mount to the app means this package cannot break another plugin's boot, at any provider priority, in any order.

func NewBroker

func NewBroker() togo.Broker

NewBroker creates a channel-aware WebSocket broker.

func SetTicketChannels added in v0.4.0

func SetTicketChannels(f func(r *http.Request) []string)

SetTicketChannels registers the function IssueTicket (and so MountTicket) uses to decide which channels the requesting user's ticket is granted. f is called per request and should return the exact channel names and/or "prefix*" globs the caller may subscribe to (typically their own private-* channels).

If this is never called, IssueTicket grants no private channels — secure by default — and logs a one-time warning explaining that SetTicketChannels needs to be configured to unlock private-channel access.

Types

This section is empty.

Jump to

Keyboard shortcuts

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