Using the Redis NoSql database with .NET Part 14: messaging in the .NET client

Introduction

In the previous post we saw how to execute transactions in the ServiceStack.Redis client. We could conclude that it was in fact extremely easy using the CreateTransaction, QueueCommand and Commit functions.

In this post we’ll look at how to use the Redis messaging function in the .NET client. You’ll see that it’s just as easy as executing transactions.

Publishing

private static void TryMessagePublishing()
{
	IRedisClientsManager pooledClientManager = new PooledRedisClientManager(0, "127.0.0.1:6379");
	using (IRedisClient pooledClient = pooledClientManager.GetClient())
	{
		pooledClient.PublishMessage("learn-redis", "This is a message to the Redis channel from the .NET client");
	}
}

The PublishMessage function accepts two string paramenters: the channel name and the message. That’s it, it’s almost shamefully easy.

Subscribing and receiving

This is only slightly more complicated than publishing a message. We first create a subscription with the CreateSubscription which returns an object of type IRedisSubscription. The subscription object has an OnMessage property which can be set to an Action with two parameters: the channel and the message. The assigned Action will handle the message in the way we want to. Finally we declare which channels we subscribe to:

private static void TrySubscribingToChannel()
{
	IRedisClientsManager pooledClientManager = new PooledRedisClientManager(0, "127.0.0.1:6379");
	using (IRedisClient pooledClient = pooledClientManager.GetClient())
	{
		IRedisSubscription subscription = pooledClient.CreateSubscription();
		subscription.OnMessage = (channel, message) => {
			Debug.WriteLine($"Received the following message from {channel}: {message}");
		};
		subscription.SubscribeToChannels("learn-redis", "world-news");
	}
}

We can subscribe to multiple channels in the SubscribeToChannels. Note that the call to SubscribeToChannels is a blocking one, i.e. it will block all code after it from executing. This is quite common across listeners/subscribers in messaging systems that they keep polling the assigned channel for messages. At first this behaviour will seem strange and may give the impression that the application has stopped running. In a real application therefore it may be better to put this code on a separate thread so that the rest of the code can continue to run. A long running application such as a Windows service is a good candidate for a message listener.

So, if you test the above code then your demo application will stop at SubscribeToChannels. Open a command prompt, start the Redis CLI and publish a message:

publish learn-redis “Redis is great”

The simple OnMessage action handler will be executed and the message will be printed in the debug window.

We’ll continue in the next post.

You can view all posts related to data storage on this blog here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: