Pub/Sub Messaging in Redis Using JavaScript

Pub/Sub Messaging in Redis Using JavaScript

Welcome back! In this lesson, we will dive into another powerful feature of Redis: Publish/Subscribe (Pub/Sub) messaging. This topic builds on our understanding of Redis and introduces a dynamic way to enable real-time communication within your applications.

What You'll Learn

In this lesson, you will learn how to set up and use Redis Pub/Sub messaging to send and receive messages between different parts of your application. This is useful for creating real-time features like notifications, chat systems, or live updates.

Here's a sneak peek at how you can set up a simple Pub/Sub system in Redis:

import { createClient } from 'redis';

const client = createClient({ url: 'redis://localhost:6379' });

await client.connect();

const pubsub = client.duplicate();
await pubsub.connect();

function messageHandler(message) {
    console.log(`Received message: ${message}`);
}

async function runPubSub() {
    await pubsub.subscribe('notifications', (message) => {
        messageHandler(message);
    });
}

runPubSub();

await new Promise(resolve => setTimeout(resolve, 1000));

const publishResult = await client.publish('notifications', 'Hello, Redis!');
console.log(`Message published, number of subscribers that received the message: ${publishResult}`);

await pubsub.unsubscribe('notifications');
await pubsub.disconnect();
await client.disconnect();

Let's break down the code snippet above:

  • First, we create a messageHandler function that prints the message received.
  • Then, we create two Redis clients using createClient and connect them to the Redis server.
  • We duplicate the initial client to handle the Pub/Sub functionalities separately and connect it.
  • In the runPubSub function, we subscribe to the notifications channel and call the messageHandler function when a message is received.
  • We publish a message to the notifications channel after a short delay using the client.publish method. It returns the number of subscribers that received the message. Then the messageHandler function will print the message received.
  • After handling the published message, we unsubscribe from the notifications channel and disconnect both Redis clients to avoid running indefinitely.

The output will be:

Message published, number of subscribers that received the message: 1
Received message: Hello, Redis!

Why It Matters

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal