Skip to main content

Production-ready Go WebSocket hub

A reusable WebSocket connection management package for Go. Rooms, broadcasting, middleware, lifecycle hooks, metrics, rate limiting, multi-node scaling, and more — with a pluggable, zero-business-logic architecture.

go get github.com/KARTIKrocks/wshub

Production Ready

Proper concurrency, graceful shutdown, error handling

Multi-Node Support

Scale horizontally with Redis or NATS adapters and presence gossip

Room Support

Group clients into rooms for targeted broadcasting

Middleware System

Chain handlers with custom logic using the middleware pattern

Lifecycle Hooks

Hook into connection, message, and room events

Pluggable Architecture

Bring your own logger and metrics collector

Thread Safe

All methods are safe for concurrent use

Zero-Alloc JSON

Pre-serialized JSON API skips marshaling — 0 allocs, ~35 ns per send

Graceful Drain

Zero-downtime deploys with connection draining, idle reaping, and hub state inspection

Write Coalescing

Batch queued text messages into a single WebSocket frame, reducing syscalls under high throughput

Health Probes

Drop-in HTTP handlers for Kubernetes /healthz and /readyz with live state, uptime, and client count

Prometheus Metrics

Official drop-in MetricsCollector subpackage with 12 metrics covering connections, messages, latency, and rooms

Why wshub?

A raw gorilla/websocket connection gets you a socket. Everything past that — the parts that turn “I can send a frame” into “I can run this in production” — is what wshub provides. It is not a replacement for a WebSocket protocol library; it is built on top of one.

CapabilitywshubRaw WebSocket library
Connection registry, rooms, broadcastingIncludedYou build it
Backpressure & drop policiesIncludedYou build it
Graceful drain + shutdownIncludedYou build it
Multi-node scaling (Redis / NATS)IncludedYou build it
Rate limiting (connections, rooms, messages)IncludedYou build it
Metrics + official Prometheus subpackageIncludedYou build it
/healthz and /readyz probesIncludedYou build it
Lifecycle hooks & middleware chainIncludedYou build it

Performance

Measured on an Intel i5-11400H (12 cores), Go 1.27, Linux — in-process benchmarks for dispatch cost, and end-to-end load tests over real WebSocket connections for the rest.

SendToClient

105 ns/op

0 allocs, at 100K clients

Broadcast

22.6 ms

0 allocs, to 100K clients

Handshakes

36,891 conn/s

measured over 10K connections

Fanout

499K msg/s

5K clients, one broadcaster

Reproduce them yourself: go test -bench=. -benchmem ./... covers the two dispatch figures, make loadtest LOADTEST_ARGS="-scenario connect -clients 10000" the handshake rate, and make loadtest LOADTEST_ARGS="-scenario fanout -clients 5000" the fanout — a bare make loadtest defaults to 1,000 clients and reproduces neither. The full benchmark tables list every figure measured.