Loading Depth Study
Preparing your workspace
Redis Pub/Sub allows one process to publish messages to a channel while other processes subscribed to that channel receive them.
Pub/Sub is useful when application processes need to react to events in real time. A publisher sends a message to a channel, and active subscribers receive it. Pub/Sub is designed for transient real-time communication; messages are not a permanent queue if a subscriber is disconnected. For durable background processing, other Redis structures or dedicated queue systems may be more appropriate.
Multiple API servers can publish a notification event that connected application processes use to update live dashboards.
const publisher = redis.duplicate();
const subscriber = redis.duplicate();
await subscriber.connect();
await publisher.connect();
await subscriber.subscribe('course-events', (message) => {
console.log(message);
});
await publisher.publish(
'course-events',
JSON.stringify({ event: 'course.updated', courseId: 'c1' })
);The subscriber receives the course update message published to the channel.