WebSocket hub with channel-based pub/sub
- Go 100%
Wires the *Hub surface into Core's service-registration plumbing behind a *core.ServiceRuntime[HubConfig]. NewService() factory builds the hub via NewHubWithConfig() and returns a *Service ready for c.Service() registration. Action handlers exposed via OnStartup: ws.broadcast — opts.type/channel/process_id/data → all clients ws.send_channel — opts.channel + message-shape → channel ws.send_event — opts.event + opts.data ws.send_error — opts.message ws.send_process_output — opts.process_id + opts.output ws.send_process_status — opts.process_id + opts.status + opts.exit_code ws.client_count — int ws.channel_count — int ws.subscriber_count — opts.channel → int ws.stats — HubStats Subscribe/Unsubscribe (take *Client), HandleWebSocket / Handler (HTTP handlers), iter.Seq accessors stay direct method calls — not IPC-friendly. OnStartup spawns Hub.Run in a background goroutine; OnShutdown cancels the hub context. Note: pre-existing auth_test failures in baseline (not introduced by this change). Service-specific tests pass. Co-authored-by: Hephaestus <hephaestus@lthn.ai> |
||
|---|---|---|
| .core | ||
| .forgejo/workflows | ||
| docs | ||
| external | ||
| go | ||
| tests/cli/ws | ||
| .editorconfig | ||
| .gitignore | ||
| .gitmodules | ||
| .golangci.yml | ||
| .woodpecker.yml | ||
| AGENTS.md | ||
| CLAUDE.md | ||
| CONTRIBUTING.md | ||
| go.work | ||
| go.work.sum | ||
| LICENCE | ||
| README.md | ||
| sonar-project.properties | ||
go-ws
WebSocket hub for real-time streaming in Go. Implements the hub pattern with centralised connection management, named channel pub/sub, token-based authentication on upgrade, client-side reconnection with exponential backoff, and a Redis pub/sub bridge for coordinating broadcasts across multiple hub instances. The envelope pattern with a per-bridge source ID prevents loop amplification when the Redis bridge is in use.
Module: forge.lthn.ai/core/go-ws
Licence: EUPL-1.2
Language: Go 1.25
Quick Start
import "forge.lthn.ai/core/go-ws"
hub := ws.NewHub()
go hub.Run(ctx)
// Mount on any HTTP mux
http.HandleFunc("/ws", hub.Handler())
// Send process output to subscribers of "process:abc"
hub.SendProcessOutput("abc", "output line")
// Redis bridge for multi-instance coordination
bridgeResult := ws.NewRedisBridge(hub, ws.RedisConfig{Addr: "localhost:6379"})
if !bridgeResult.OK {
return bridgeResult
}
bridge := bridgeResult.Value.(*ws.RedisBridge)
if r := bridge.Start(ctx); !r.OK {
return r
}
Documentation
- Architecture — hub pattern, channel subscriptions, authentication, Redis bridge, envelope loop prevention
- Development Guide — prerequisites, test patterns, coding standards
- Project History — completed phases with commit hashes, known limitations
Build & Test
go test ./...
go test -race ./...
go test -bench=. -benchmem ./...
go build ./...
Licence
European Union Public Licence 1.2 — see LICENCE for details.