Click here to Skip to main content
15,892,298 members
Articles / Programming Languages / Visual Basic

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
2 May 2011CPOL2 min read 33.1K   10.6K   3  
Create a simple redis pub/sub client pair in VB.NET
Module MainModule

    Private MyRedisStore As RedisStore

    Sub Main()
        MyRedisStore = New RedisStore(True)
        AddHandler MyRedisStore.OnSubscriptionMessage, AddressOf SubcribedMessageReceived
        Select Case ChooseOption()
            Case 1
                RunAsSubscriber()
            Case 2
                RunAsPublisher()
            Case Else
                EndMe()
        End Select
    End Sub

    Private Sub RunAsPublisher()
        Dim Stopped As Boolean = False
        Do While Not Stopped
            Console.Write("Write the message [Type Exit to stop]: ")
            Dim Message As String = Console.ReadLine()
            If Message.ToLower = "exit" Then
                Stopped = True
            Else
                MyRedisStore.Publish(AChannel, Message)
            End If
        Loop
    End Sub

    Private Sub SubcribedMessageReceived(ByVal sender As Object, ByVal e As SubscriptionMessageEventArgs)
        Console.WriteLine("Received Message on channel {0} -> {1}", e.Key, e.value)
    End Sub

    Private AChannel As String = "aKey:UserInput"
    Private Sub RunAsSubscriber()
        Console.WriteLine("Now Listening for messages....")
        MyRedisStore.Subscribe(AChannel)
    End Sub

    Private Function ChooseOption() As Integer
        Console.WriteLine("=============== MENU ===================")
        Console.WriteLine("   Any other number, exits the program..")
        Console.WriteLine("1. Start as subscriber")
        Console.WriteLine("2. Start as publisher")
        Console.WriteLine("--------------------------")
        Console.Write("Choose :")
        Dim Userinput As String = Console.ReadLine()
        Return If(IsNumeric(Userinput), Userinput, -1)
    End Function

    Private Sub EndMe()
        Console.Write("Press any key to exit...")
        Console.ReadKey()
        End
    End Sub

End Module

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)


Written By
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.

Comments and Discussions