Node Inspector
Redis Pub/Sub is a messaging paradigm where senders (publishers) do not program the messages to be sent directly to specific receivers (subscribers).
Topic Legend
Live Message Stream
Redis Pub/Sub: Real-time Messaging
Redis Pub/Sub (Publish/Subscribe) is a messaging system where publishers send messages to channels, and subscribers receive messages from the channels they are interested in. It is a Stateless "fire and forget" system.
Fire and Forget
Messages are not persisted. If a subscriber is offline when a message is published, they will never receive it. For persistence, use Redis Streams or Redis Lists.
One-to-Many
A single PUBLISH command can reach hundreds of thousands of
subscribers simultaneously with very low latency.
Common Commands
# Subscribe to one or more channels
SUBSCRIBE news alerts
# Subscribe to channels matching a pattern
PSUBSCRIBE user.*
# Publish a message to a channel
PUBLISH news "New architecture flow launched!"
Implementation (Node.js)
const redis = require('redis');
const publisher = redis.createClient();
const subscriber = redis.createClient();
// Subscriber Logic
await subscriber.connect();
await subscriber.subscribe('news', (message) => {
console.log('Received news:', message);
});
// Publisher Logic
await publisher.connect();
await publisher.publish('news', 'Hello World!');
When to use Pub/Sub?
- Real-time Notifications: Sending alerts to active web-socket users.
- Chat Systems: Distributing chat messages across multiple server instances.
- Cache Invalidation: Notifying other app nodes to purge their local caches when data changes.