Implementing PubSub with Redis
Redis exposes a Publish-Subscribe messaging pattern (not so dissimilar to the CouchDB changes
stream), which can be used to listen to specific data change events. Data from these events could be passed between processes to, for instance, instantly update a web app with fresh new data.
With PubSub, we publish a message to a specific channel, this channel can then be picked up by any amount of subscribers. The publishing mechanism doesn't care who's listening or how many, it chats away regardless.
In this recipe, we will create a publishing process and a subscribing process. For publishing, we'll extend our quotes.js
file from the previous recipe Storing and retrieving data with Redis, and we'll write the code to a new file for the subscription mechanism.
Getting ready
Let's create a new directory, copy quotes.js
from the previous recipe, and rename it to quotes_publish.js
. We will also create a file called quotes_subscribe.js
. We'll need to ensure that Redis is...