WebSocket server with topic subscriptions (ws)

5371
0

Raw WebSockets can turn into an unmaintainable mess unless you define a tiny protocol up front. I keep messages typed (even if it’s ‘JSON with a type field’) and I implement topic subscriptions so clients opt into exactly what they need. I track subscriptions per connection and clean them up on close so state doesn’t leak. Another practical detail is defensive limits: set maxPayload and validate incoming payloads so one bad client can’t chew CPU and memory. For many apps I’ll start with SSE, but when I truly need bidirectional real-time interactions, a topic-based approach keeps the system sane as features grow.