| title | WebSocket Shared State | ||||||
|---|---|---|---|---|---|---|---|
| keywords |
|
||||||
| description | Demonstrates thread-safe shared state management and real-time broadcasting across multiple WebSocket connections and HTTP handlers in Fiber v3. |
This recipe demonstrates thread-safe shared state management and real-time state synchronization across multiple WebSocket connections and concurrent REST endpoints in Fiber v3.
- Thread-Safe State Manager: Encapsulates session data with
sync.RWMutexproviding atomic mutations and read snapshots. - Zero-Allocation Safety: The REST handler clones the session ID from
c.Params(strings.Clone) before it becomes a long-lived map key, because Fiber's zero-allocation strings point into reused request buffers (community report gofiber/fiber#3195). Values decoded byjson.Unmarshalandkws.Paramsin the WebSocket handler are already copies. - Multi-Client State Synchronization: Connect multiple WebSocket clients to the same session ID. State mutations (via REST or WebSocket) are immediately broadcast to all active subscribers. Every snapshot carries a
versionthat grows with each mutation; under concurrent updates broadcasts can arrive out of order, so clients keep the snapshot with the highest version. - Clean Connection Lifecycle: Handles client unregistration upon connection drops without memory leaks or race conditions.
Sessions live in process memory and are never evicted, and any client can create them. Before exposing this to untrusted clients, add authentication and a session limit or TTL.
.
├── go.mod
├── go.sum
├── main.go
├── main_test.go
└── README.md
Start the server:
go run main.goThe server listens on http://localhost:3000.
Using wscat or your browser's WebSocket API:
# Connect client 1 to session 'room-101'
wscat -c ws://localhost:3000/ws/room-101If the session already has state, the server immediately sends a state snapshot upon connection.
curl -i -X POST http://localhost:3000/api/sessions/room-101 \
-H "Content-Type: application/json" \
-d '{"key": "status", "value": "active"}'Returns:
{"id":"room-101","data":{"status":"active"},"version":1,"updated_at":"2026-11-04T15:00:00Z"}curl -i http://localhost:3000/api/sessions/room-101Returns:
{"id":"room-101","data":{"status":"active"},"version":1,"updated_at":"2026-11-04T15:00:00Z"}Update the session state:
curl -i -X POST http://localhost:3000/api/sessions/room-101 \
-H "Content-Type: application/json" \
-d '{"key": "order_step", "value": "shipped"}'Every connected WebSocket client subscribed to room-101 immediately receives:
{
"id": "room-101",
"data": {
"status": "active",
"order_step": "shipped"
},
"version": 2,
"updated_at": "2026-11-04T15:00:01Z"
}Send a JSON message through the wscat terminal:
{"key": "user_status", "value": "typing"}The updated state is stored in the StateManager and broadcast to every WebSocket connection subscribed to the session, including the sender.
Run the test suite with race detection enabled:
go test -v -race ./...