Algobook
- The developer's handbook
mode-switch
back-button
Buy Me A Coffee
Mon Apr 24 2023

How to use Redis pub/sub in NodeJs

In a recent post, we did an introduction in to using Redis in NodeJs. In todays guide, we will extend our knowledge and using the pub/sub technique with Redis as our message broker.

We will have two projects, one that are publishing updates and one that are subscribing. Both projects will interact with the Redis cache. We will finish up with showing how to subscribe with wildcards to catch mulitple channel publishes with one subscription.

Our publisher

We will do an example, where we will have an interval running for 2.5 seconds, and for each cycle we will increment a counter in the cache, and publish to the subscriber with the new value. And we will setup another interval for 5 seconds that will publish the current number of interval cycles that has been performed. We will use two channels for communication, counter.updates and healthcheck.

const redis = require("redis"); const publisher = redis.createClient({ url: "redis://localhost:6379" }); publisher.connect(); let intervalCycles = 0; setInterval(async () => { intervalCycles++; const counter = await publisher.incr("counter"); publisher.publish("counter.updates", `New counter value: ${counter}`); }, 2500); setInterval(async () => { publisher.publish( "healthcheck", JSON.stringify({ intervalCycles: `Right now, ${intervalCycles} intervals has been performed`, }) ); }, 5000);

Our subscriber

Now, let's subscribe and receive the updates with our subscriber.

const redis = require("redis"); const subscriber = redis.createClient({ url: "redis://localhost:6379" }); subscriber.connect(); subscriber.subscribe("counter.updates", async (message) => { console.log(message); }); subscriber.subscribe("healthcheck", (message) => { const parsed = JSON.parse(message); console.log(parsed.intervalCycles); });

Output

In our log, we can see message like this.

Right now, 5 intervals has been performed New counter value: 6 New counter value: 7 Right now, 7 intervals has been performed New counter value: 8

Wildcard

If we want to subscribe to multiple channels, or all channels perhaps, we can utilize wildcards in Redis as well. Let's extend our subscriber a bit.

subscriber.pSubscribe("*", (message) => { console.log(`I hear everything! Message: ${message}`); }); subscriber.pSubscribe("health*", (message) => { console.log(`I hear everything regarding health! Message: ${message}`); });

There we go. So, by using pSubscribe, we can specify the channel pattern with "*". Our first subscription will capture everything whilst our second will catch all messages published with a prefix of health.

Outro

That's it for todays guide. I hope you got some value by reading this, and I hope that you find this useful. I personally love Redis, it is very powerful and easy to use. Multiple companies I have worked with are using Redis as well, and that speaks for itself I would say.

Have a great day!

signatureMon Apr 24 2023
See all our articles