Production Ready
Proper concurrency, graceful shutdown, error handling
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/wshubProper concurrency, graceful shutdown, error handling
Scale horizontally with Redis or NATS adapters and presence gossip
Group clients into rooms for targeted broadcasting
Chain handlers with custom logic using the middleware pattern
Hook into connection, message, and room events
Bring your own logger and metrics collector
All methods are safe for concurrent use
Pre-serialized JSON API skips marshaling — 0 allocs, ~35 ns per send
Zero-downtime deploys with connection draining, idle reaping, and hub state inspection
Batch queued text messages into a single WebSocket frame, reducing syscalls under high throughput
Drop-in HTTP handlers for Kubernetes /healthz and /readyz with live state, uptime, and client count
Official drop-in MetricsCollector subpackage with 12 metrics covering connections, messages, latency, and rooms
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.
| Capability | wshub | Raw WebSocket library |
|---|---|---|
| Connection registry, rooms, broadcasting | Included | You build it |
| Backpressure & drop policies | Included | You build it |
| Graceful drain + shutdown | Included | You build it |
| Multi-node scaling (Redis / NATS) | Included | You build it |
| Rate limiting (connections, rooms, messages) | Included | You build it |
| Metrics + official Prometheus subpackage | Included | You build it |
| /healthz and /readyz probes | Included | You build it |
| Lifecycle hooks & middleware chain | Included | You build it |
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.
105 ns/op
0 allocs, at 100K clients
22.6 ms
0 allocs, to 100K clients
36,891 conn/s
measured over 10K connections
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.