Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » VB.NET » General » Downloads
 
Add your own
alternative version

Redis Tutorial Part II: Using Redis as a Pub/Sub Medium

Create a simple redis pub/sub client pair in VB.NET
redisPart02_demo.zip
Debug
RedisExample02.exe
RedisExample02.vshost.exe
RedisExample02.vshost.exe.manifest
ServiceStack.dll
ServiceStack.Interfaces.dll
ServiceStack.ServiceInterface.dll
redisPart02_src.zip
RedisExample02
RedisExample01
bin
Debug
RedisExample02.exe
RedisExample02.vshost.exe
RedisExample02.vshost.exe.manifest
ServiceStack.dll
ServiceStack.Interfaces.dll
ServiceStack.ServiceInterface.dll
ext-dll
ServiceStack.dll
ServiceStack.Interfaces.dll
ServiceStack.ServiceInterface.dll
ServiceStack.ServiceInterface.pdb
My Project
Application.myapp
Settings.settings
RedisExample02.vbproj.user
RedisExample01.suo
RedisExample02.suo
Imports ServiceStack.Redis

Public Class SubscriptionMessageEventArgs
    Inherits EventArgs

    Public Property Key As String
    Public Property value As String

    ''' <summary>
    ''' Initializes a new instance of the SubscriptionMessageEventArgs class.
    ''' </summary>
    Public Sub New(ByVal key As String, ByVal value As String)
        Me.Key = key
        Me.value = value
    End Sub
End Class

''' <summary>
''' The redis store encapsulation class around the ServiceStack redis client 
''' </summary>
''' <remarks>This class is cumulatively constructed across the tutorial and is not broken.</remarks>
Public Class RedisStore

#Region " Properties "

    Private _sourceClient As RedisClient
    Public ReadOnly Property SourceClient() As RedisClient
        Get
            Return _sourceClient
        End Get
    End Property

#End Region

#Region " Event "

    Public Event OnSubscriptionMessage As EventHandler(Of SubscriptionMessageEventArgs)
#End Region

#Region " Constructors "

    Public Sub New()
        MyClass.New(False)
    End Sub

    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!")
        End If
    End Sub

#End Region

    Public Function IsServerAlive() As Boolean
        Try
            Return SourceClient.Ping
        Catch ex As Exception
            Return False
        End Try
    End Function

#Region " Functionalities "

#Region " Get/Set Keys "

    Public Function SetKey(ByVal key As String, ByVal value As String) As Boolean
        Return SourceClient.Set(key, value)
    End Function

    Public Function SetKey(Of T)(ByVal key As String, ByVal value As T) As Boolean
        Return SourceClient.Set(Of T)(key, value)
    End Function

    Public Function GetKey(ByVal key As String) As String
        Return Helper.GetString(SourceClient.Get(key))
    End Function

    Public Function GetKey(Of T)(ByVal key As String) As T
        Return SourceClient.Get(Of T)(key)
    End Function

#End Region

#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)
        SubClient.SubscribeToChannels(channels)
    End Sub

    Private Sub OnMessage(ByVal channel As String, ByVal value As String)
        RaiseEvent OnSubscriptionMessage(Me, New SubscriptionMessageEventArgs(channel, value))
    End Sub

    Public Sub Publish(ByVal channel As String, ByVal value As String)
        SourceClient.Publish(channel, Helper.GetBytes(value))
    End Sub

#End Region

#End Region

End Class

Public Class Helper

    Private Shared ReadOnly UTF8EncObj As New System.Text.UTF8Encoding()

    Public Shared Function GetBytes(ByVal source As Object) As Byte()
        Return UTF8EncObj.GetBytes(source)
    End Function

    Public Shared Function GetString(ByVal sourceBytes As Byte()) As String
        Return UTF8EncObj.GetString(sourceBytes)
    End Function

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Hariharan Arunachalam
Student
India India
Hariharan leapt into the field of programming after being introduced to C in his second year at college. A fan of basketball and chess, the logical thought process behind programming and the concepts of linking real principles onto a concrete platform pushed him deep into this field. On finishing his bachelor's majoring in Electrical and Electronics engineering with Soft Computing, Numerical Analysis and Biomedical Engineering as minor, he did a six month stint at India's second largest IT company- Infosys Technologies. He left Infosys armed with strong concepts of SDL Cycles and process development, gained domain knowledge in Java and had explored Visual basic, C++, C# and kept his mind open for more. Currently working in a startup as the product designer, his arsenal of technologies has doubled to accommodate the challenges his job demands.
Off work, he enjoys learning new languages (non-programming) and is currently teaching himself German and Spanish. He also enjoys travelling around and exploring new ideas, places and relationships.
 
Currently attempting to extend his education to post graduation.
Follow on   Twitter

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 2 May 2011
Article Copyright 2011 by Hariharan Arunachalam
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid