If you have gone through Part I of this series, you will now have a bit of an idea of what redis is and its simple but awesome power. This part explains how redis can act as a publish - subscribe medium. In simpler terms- Notifications: the simplest of things that makes coding so much easier! This series will conclude with Part III: Creating a chat system with Redis in VB.NET.
As with its predecessor, this tutorial is going to be simple and direct. I can show you what powers you, not how to use them :D. One of the most powerful features of redis is its PubSub mode. In other words, when something happens, anything that wants to know it will be informed. This tutorial will show you the fundamental way to use this. My next tutorial will show how to develop a complete chat application (twitter/Facebook) in VB.NET. I know that mostly applications like that are developed in the web platform, but for the purpose of these tutorials, I am going to use it anyway.
Using the Code
The prerequisites are the same as from part I. You can use the same project or build a new one.
Here, we'll have a single application that can be run in 2 modes:
- As a Subscriber
- As a Publisher
I've built the project in such a way that this is toggled by a menu at startup. You may use it as different applications too! Run the EXE more than once to use it...
Public Class SubscriptionMessageEventArgs
Public Property Key As String
Public Property value As String
</span> </span> Public Sub New(ByVal key As String, ByVal value As String)
Me.Key = key
Me.value = value
</span></span></span></span>Public Class RedisStore
#Region " Properties "
Private _sourceClient As RedisClient
Public ReadOnly Property SourceClient() As RedisClient
#Region " Event "
Public Event OnSubscriptionMessage As EventHandler(Of SubscriptionMessageEventArgs)
#Region " Constructors "
Public Sub New()
Public Sub New(ByVal ForceCheckServer As Boolean)
_sourceClient = New RedisClient
If ForceCheckServer AndAlso Not IsServerAlive() Then
Throw New Exception("The server has not been started!")
Public Function IsServerAlive() As Boolean
Catch ex As Exception
#Region " Functionalities "
#Region " Get/Set Keys "
Public Function SetKey(ByVal key As String, ByVal value As String) As Boolean
Return SourceClient.Set(key, value)
Public Function SetKey(Of T)(ByVal key As String, ByVal value As T) As Boolean
Return SourceClient.Set(Of T)(key, value)
Public Function GetKey(ByVal key As String) As String
Public Function GetKey(Of T)(ByVal key As String) As T
Return SourceClient.Get(Of T)(key)
#Region " Pub/Sub "
Public Sub Subscribe(ByVal ParamArray channels() As String)
Dim SubClient As IRedisSubscription = SourceClient.CreateSubscription
SubClient.OnMessage = New Action(Of String, String)(AddressOf OnMessage)
Private Sub OnMessage(ByVal channel As String, ByVal value As String)
(Me, New SubscriptionMessageEventArgs(channel, value))
Public Sub Publish(ByVal channel As String, ByVal value As String)
Public Class Helper
Private Shared ReadOnly UTF8EncObj As New System.Text.UTF8Encoding()
Public Shared Function GetBytes(ByVal source As Object) As Byte()
Public Shared Function GetString(ByVal sourceBytes As Byte()) As String
To subscribe to an array of channels, call the
Subscribe function. To publish to a channel, call the
Publish function. As you may see, a client that subscribes cannot do anything else. We deal with this situation in the next tutorial, unless of course you figure out the simple solution for it on your own!
Well, that's it for the second part of the tutorial. Do ask and comment if you have any queries.
- 1st May, 2011: Initial version