Akxost

WebSockets · Redis · Realtime

Real-Time Features with WebSockets and Redis

2024-12-05 · 6 min read

The scaling problem

One server handles thousands of socket connections. But as soon as you scale horizontally, you need state coordination across instances.

Redis pub/sub as the backbone

TypeScript
const pub = redis.createClient();
const sub = pub.duplicate();
io.adapter(createAdapter(pub, sub));

Presence and typing indicators

I store presence in Redis with short TTL keys and refresh on activity. It keeps the data lightweight and resilient to disconnects.

Watch out for fan-out

Broadcasting to large rooms can blow up memory usage fast. Partition large rooms by region or use targeted channels for updates.