In my previous post I wrote about subscribing and publishing to Redis message queues in node.js using the node_redis library.

The proposed architecture of the Kraken Office system ( see selecting technologies for Kraken Office for details ) uses Redis as a message broker to link the various parts of the system together. The previous post shows how to do this from the node.js web socket server but now we need to access the message queues in Redis from C# as the application and data access layer of the system will be written using the .NET framework.

We will be using the ServiceStack.Redis client library to access Redis from .NET.

Using ServiceStack.Redis

ServiceStack.Redis is a client .NET library that enables you to access the Redis no-SQL data store. It has functionality for using Redis as a data store and also for accessing the subscriber / publisher messaging functionality.

To use ServiceStack.Redis simply download the lastest code base and compile it using Visual Studio. Once compiled you will have access to the binaries used to interface with Redis.

Firstly you will need to start a Redis service instance (see subscribing and publishing to Redis message queues in node.js for details).

Open or create a .NET project using C# and add references to the following binaries generated during compilation of the ServiceStack.Redis project:

  1. ServiceStack.Common.dll
  2. ServiceStack.Interfaces.dll
  3. ServiceStack.Redis.dll
  4. ServiceStack.Redis.Text.dll

Once you have added the references you can then create a Redis client object in C# as following:

var client = new RedisClient("localhost", 6379);

Publishing a message to Redis is now as simple as:

var client = new RedisClient("localhost", 6379);
client.PublishMessage("node_layer", "message_body");

This publishes the message body to the specified message channel in Redis, in this case ‘node_layer‘. The url and port specified when constructing the client should point to your Redis server instance.

Subscribing to receive messages from a particular channels also uses the same Redis client object. To subscribe to a channel and start receiving messages the following code can be used:

var client = new RedisClient("localhost", 6379);
IRedisSubscription redisSubscription = null;

using(_redisSubscription = client.CreateSubscription())
     _redisSubscription.OnMessage += (channel, message) =>
          // handle message as appropriate

_redisSubscription.SubscribeToChannels(new string[] { "application_layer" });

You can easily subscribe to multiple channels by specifying additional channel names in the array parameter of the SubscribeToChannels method.

Note that the SubscribeToChannels method is blocking so your process will block on this method call and messages to the OnMessage event will be fired on a different thread.


Accessing Redis’ messaging facilities in .NET can be achieved easily by using the ServiceStack.Redis client library.

The message broker and client library have a major limitation, however, in that they work on a broadcast basis. As a result, multiple clients listening to a particular channel will all receive every message.

This raises issues when trying to use the Redis message broker for creating scalable systems where extra clients can be added to provide additional processing resource. For this type of scalability a round-robin system of message is needed, where messages are balanced across subscribers.

Currently ServiceStack.Redis does not provide a round-robin message distribution option. As I like the Redis product and have found message queue integration easy from both .NET and node.js I have decided to write my own message proxy service to provide round-robin messaging facilities. This will be the topic of my next post.

Until then – happy coding!