Pub/Sub with Redis on Azure

After getting publish/subscribe working with ZeroMQ, I turned to Redis (mainly for a working Node.JS client). Redis is an in-memory key-value store, and includes a pub/sub feature.

Redis is easy to get working on Azure, however, I felt the need to make it even easier with a plugin.

Installing Redis on Azure

To create a new server with Redis installed, create a Cloud Project with a Worker Role, use APM to add the Redis Plugin.

> apm install Redis

Then add Redis as a plugin in your Service Definition file:

<WorkerRole name="WorkerRole1" vmsize="Small">
    <Import moduleName="Diagnostics" />
    <Import moduleName="Redis" />


Setting up the Subscriber (in Node.js)

Install the Redis module using NPM:

> npm install redis

In your node code, you can subscribe to messages like this:

var redis = require("redis");
receiveclient = redis.createClient(6379, "WorkerRole1_IN_0");
receiveclient.on("message", function (channel, message) {
  // do something with the message

And you can publish messages like this (using a separate client).

sendclient = redis.createClient(6379, "WorkerRole1_IN_0");
sendclient.publish("pub", message);


If you’ve got Redis running on the same machine as the Node process, there is no need to specify the port and server name when creating a client. However, if this isn’t the case, Node needs to know the ip address of the instance hosting Redis. If your process is hosted by IISNode, then you can call the Azure API to discover the topology of your deployment, and find the correct IP address. This isn’t the case for me. I’m using  raw sockets and I can’t use IISNode.

So the question is, how do you discover the IP address of your Redis instance?

I created another plugin (HostsWriter) which continually updates the hosts file in Windows, with the IP address of each instance in the deployment.

This means you can just refer to instances by their instance name, and Windows will connect you to the right IP address.

To install, run this command with APM :

> apm install HostsWriter

And add it to the Service Definition file of the WorkerRole hosting your Node.js script.

<WorkerRole name="WorkerRole2" vmsize="Small">
    <Import moduleName="Diagnostics" />
    <Import moduleName="HostsWriter" />


Redis is really easy to set up and use. It also works with Node, and module is 100% JavaScript, which is a big tick for me. Whether it’s as fast as ZeroMQ, I’m not sure, but it’s my current favourite!