Pub/Sub Guide
SUBSCRIBE & PUBLISH
A subscribed client can only run SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE, PUNSUBSCRIBE, PING, and RESET.
# Terminal 1: subscriber
SUBSCRIBE news:sports news:tech
# Terminal 2: publisher
PUBLISH news:sports "Team wins championship!"
PUBLISH news:tech "New framework released"
# Subscriber receives messages in format:
# 1) "message"
# 2) "news:sports"
# 3) "Team wins championship!"
# Subscribe to multiple channels
SUBSCRIBE ch1 ch2 ch3
# Unsubscribe
UNSUBSCRIBE news:sports # leave one channel
UNSUBSCRIBE # leave all channels
Pattern Subscribe (PSUBSCRIBE)
# Subscribe with glob pattern
PSUBSCRIBE news:* # all news channels
PSUBSCRIBE user:[0-9]* # channels matching user:NNN
PSUBSCRIBE h?llo # hello, hallo, hxllo ...
# Received message format (pmessage):
# 1) "pmessage"
# 2) "news:*" <- matched pattern
# 3) "news:sports" <- actual channel
# 4) "Team wins!" <- payload
PUNSUBSCRIBE news:* # remove pattern subscription
# List active subscriptions
PUBSUB CHANNELS # all active channels
PUBSUB CHANNELS news:* # filtered
PUBSUB NUMSUB news:sports news:tech # subscriber counts
PUBSUB NUMPAT # number of pattern subscriptions
Keyspace Notifications
Subscribe to events triggered by Redis commands on keys (e.g., SET, EXPIRE, DEL).
# Enable keyspace notifications in redis.conf or at runtime
# K = Keyspace events E = Keyevent events
# g = generic $ = strings l = lists z = sorted sets
# x = expired d = stream
CONFIG SET notify-keyspace-events "KEA" # enable all
# Subscribe to keyspace channel for key "mykey"
SUBSCRIBE __keyspace@0__:mykey
# Subscribe to all expired key events
SUBSCRIBE __keyevent@0__:expired
# Subscribe to all SET operations
SUBSCRIBE __keyevent@0__:set
# Example: track session expiry
SUBSCRIBE __keyevent@0__:expired
# Receives channel: __keyevent@0__:expired
# message: session:abc123
Node.js Pub/Sub Example
// Using ioredis (separate connections required for sub/pub)
const Redis = require("ioredis");
const pub = new Redis();
const sub = new Redis();
// Subscribe
sub.subscribe("chat:room1", (err, count) => {
console.log(`Subscribed to ${count} channels`);
});
sub.on("message", (channel, message) => {
console.log(`[${channel}] ${message}`);
});
// Publish
await pub.publish("chat:room1", JSON.stringify({
user: "alice",
text: "Hello!"
}));
// Pattern subscribe
sub.psubscribe("chat:*", (err, count) => {});
sub.on("pmessage", (pattern, channel, message) => {
console.log(`Pattern: ${pattern}, Channel: ${channel}`);
});
Pub/Sub vs. Streams
| Feature | Pub/Sub | Streams (XADD/XREAD) |
|---|---|---|
| Message persistence | No (fire and forget) | Yes (stored in log) |
| Consumer groups | No | Yes |
| Replay messages | No | Yes |
| Offline consumers | Miss messages | Read from last position |
| Use case | Real-time notifications | Task queues, event sourcing |