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.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

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

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: