Core dataReal-time

Real-Time Chat and Notifications

Fan messages out to every connected client in real time, with sharded channels for clusters and streams for history.

publish, every subscriber
1
channels for clusters
Sharded
with streams
+history
Step 1 of 5
Servers subscribe

Each WebSocket server subscribes to the rooms its connected users are in.

Example sessionplaying
# Servers subscribe
hotkv> SUBSCRIBE room:42
1) "subscribe"
2) "room:42"
3) (integer) 1

An animated walkthrough with real HotKV commands and example replies. Click any step to jump to it.

The problem

A chat room or live notification feed has to reach every connected user at once, across many WebSocket servers. Polling a database is too slow and too expensive, and the servers need a shared way to tell each other that something happened.

How HotKV handles it

Each WebSocket server subscribes to the channels its users care about. When anything happens, one PUBLISH reaches every subscribed server, which forwards it to its users. Sharded channels keep the traffic on the node that owns the channel in a cluster, and a stream next to each room keeps the history for users who join later.

Step by step

What happens, command by command

  1. 01

    Servers subscribe

    Each WebSocket server subscribes to the rooms its connected users are in.

    SUBSCRIBE room:42
  2. 02

    Alice sends a message

    Her server publishes once. The reply is how many subscribers received it.

    PUBLISH room:42 "{\"from\":\"alice\",\"text\":\"Deploy is green\"}"
  3. 03

    Keep the history

    The message is also appended to the room's stream, capped at the last 1000 messages.

    XADD room:42:history MAXLEN ~ 1000 * from alice text "Deploy is green"
  4. 04

    Bob reconnects later

    He reads the last 50 messages from the stream, then goes live on the channel.

    XREVRANGE room:42:history + - COUNT 50
  5. 05

    Sharded channels in a cluster

    In cluster mode, SPUBLISH keeps the message on the node that owns the channel.

    SPUBLISH room:{42}:typing "alice"
Where it fits

Real-world scenarios

Team chat and support chat

Messages fan out to every server holding a member's connection, and the stream gives newcomers the backlog.

Live notifications

Order updates, mentions and alerts are pushed to the browser the moment they happen.

Collaborative apps

Cursor positions and typing indicators are broadcast on short-lived channels.

Built in

What you get

Fan-out

One publish reaches every subscriber.

PUBLISHSUBSCRIBE

Pattern subscriptions

Subscribe to room:* to receive every room.

PSUBSCRIBE

Sharded pub/sub

Channels stay on the node that owns them in a cluster.

SPUBLISHSSUBSCRIBE

History

Pair a channel with a capped stream for messages users missed.

XADDXREVRANGE

Common questions

Are pub/sub messages stored?

No. Pub/sub delivers to whoever is subscribed at that moment. Use a stream next to the channel if users need history.

Does it work in a cluster?

Yes. Use sharded pub/sub (SPUBLISH, SSUBSCRIBE) so each channel's traffic stays on the node that owns it.

Can I get notified when keys change?

Yes. Keyspace notifications publish events for key changes. See the docs on keyspace notifications.

Related use cases

Try it on your own workload

HotKV speaks the Redis protocol, so you can point an existing client at it and run these same commands.