Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Web Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SignalR, Quartz.Net and ASP.NetSignalR and VB.Net »
    comments

    Introduction

    Yesterday I wrote a blog about SignalR and just 5 seconds after posting it I got this on twitter.

    So, here I am on a Sunday morning writing about SignalR and Hubs.

    Why hubs? You ask.

    Hubs provide a higher level RPC framework over a PersistentConnection. If you have different types of messages that you want to send between server and client then hubs is recommended so you don't have to do your own dispatching.

    It took me a while to figure this out, but I got there in the end.

    The server

    Setting up the server should have been easy. But it lacked something.

    So watch out if you are working with VB.Net.

    First I started to create my hub.

    1. Imports Microsoft.AspNet.SignalR.Hubs
    2.  
    3. Public Class Plants
    4.     Inherits Hub
    5.  
    6.     Dim _plants As IList(Of Plant)
    7.  
    8.     Public Sub New()
    9.         _plants = New List(Of Plant)
    10.         _plants.Add(New Plant With {.Id = 1, .Genus = "Fagus", .Species = "Sylvatica", .CommonNames = {"Common Beech"}})
    11.     End Sub
    12.  
    13.     Public Function GetPlant(id As Integer) As Plant
    14.         Return _plants.Where(Function(x) x.Id = id).FirstOrDefault
    15.     End Function
    16.  
    17. End Class

    This hub should return a plant for the id given or nothing when the id is not found.

    If you start this you should get the url to your server.

    if you browse to that url and add /signalr/hubs to it you should get something like this.

    /*!
    * ASP.NET SignalR JavaScript Library v1.0.0
    * http://signalr.net/
    *
    * Copyright Microsoft Open Technologies, Inc. All rights reserved.
    * Licensed under the Apache 2.0
    * https://github.com/SignalR/SignalR/blob/master/LICENSE.md
    *
    */

    /// /// (function ($, window) { /// "u

    But I did not get that. I got a 404 not found page instead. Which isn't good.

    And it was caused by the routes not being registered. Which normally happens via this cs file that is in your App_Start folder (which was created by the nuget package).

    .Web.Routing;
    using Microsoft.AspNet.SignalR;
    using Microsoft.AspNet.SignalR.Hosting.AspNet;
    
    [assembly: PreApplicationStartMethod(typeof(SignalRTesting.RegisterHubs), "Start")]
    
    namespace SignalRTesting
    {
        public static class RegisterHubs
        {
            public static void Start()
            {
                // Register the default hubs route: ~/signalr/hubs
                RouteTable.Routes.MapHubs();            
            }
        }
    }
    

    And normally you don't need to setup any roots in your Global.asax.

    But when I did. It worked.

    So add a Global.asax and change it to this.

    1. Imports System.Web.SessionState
    2. Imports System.Web.Routing
    3. Imports Microsoft.AspNet.SignalR
    4.  
    5. Public Class Global_asax
    6.     Inherits System.Web.HttpApplication
    7.  
    8.     Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    9.         RouteTable.Routes.MapHubs()
    10.     End Sub
    11.  
    12.  
    13. End Class

    And now your server should return the correct page on browsing to /signalr/hubs

    The client

    First thing to try is to make a console client.

    Look at the code of our server and see that we return an object of type Plant.

    Our Plant object looks like this BTW.

    1. Public Class Plant
    2.     Public Property Id As Integer
    3.     Public Property Genus As String
    4.     Public Property Species As String
    5.     Public Property CommonNames As String()
    6. End Class

    The return means that only the caller will get that returnvalue, not any of the other clients.

    And here is such a client.

    1. Imports Microsoft.AspNet.SignalR.Client.Hubs
    2. Imports Microsoft.AspNet.SignalR.Client
    3.  
    4. Module Module1
    5.  
    6.     Sub Main()
    7.         Dim connection = New HubConnection("http://localhost:50865")
    8.  
    9.         Dim plants = connection.CreateHubProxy("Plants")
    10.  
    11.         connection.Start().Wait()
    12.  
    13.         Dim line As String = Nothing
    14.         Console.WriteLine("type the id + enter to send, type exit + enter to exit.")
    15.         While line <> "exit"
    16.             line = Console.ReadLine()
    17.             plants.Invoke(Of Object)("GetPlant", line).ContinueWith(Sub(data)
    18.                                                                         If data.Result IsNot Nothing Then
    19.                                                                             Console.WriteLine(data.Result.ToString)
    20.                                                                         Else
    21.                                                                             Console.WriteLine("No plant with that id.")
    22.                                                                         End If
    23.                                                                     End Sub)
    24.         End While
    25.     End Sub
    26.  
    27. End Module

    And if you type 1 you should get this in return.

    But we could have just use ServiceStack if we needed something like that.

    So what is the point?

    Let's just say that we want to inform all our clients that someone just requested a plant with id 1 than we could do this instead.

    Change the server code and start it up.

    1. Imports Microsoft.AspNet.SignalR.Hubs
    2.  
    3. Public Class Plants
    4.     Inherits Hub
    5.  
    6.     Dim _plants As IList(Of Plant)
    7.  
    8.     Public Sub New()
    9.         _plants = New List(Of Plant)
    10.         _plants.Add(New Plant With {.Id = 1, .Genus = "Fagus", .Species = "Sylvatica", .CommonNames = {"Common Beech"}})
    11.     End Sub
    12.  
    13.     Public Function GetPlant(id As Integer) As Plant
    14.         Clients.All.addMessage("Someone requested id: " & id)
    15.         Return _plants.Where(Function(x) x.Id = id).FirstOrDefault
    16.     End Function
    17.  
    18. End Class

    As you can see I use Clients.All.addMessage to send a message to all Clients with the id that was requested.

    Change the console client to this.

    1. Imports Microsoft.AspNet.SignalR.Client.Hubs
    2.  
    3. Module Module1
    4.  
    5.     Sub Main()
    6.         Dim connection = New HubConnection("http://localhost:50865")
    7.  
    8.         Dim plants = connection.CreateHubProxy("Plants")
    9.  
    10.         plants.On(Of String)("addMessage", Sub(data) Console.WriteLine(data.ToString()))
    11.  
    12.         connection.Start().Wait()
    13.  
    14.         Dim line As String = Nothing
    15.         Console.WriteLine("type the id + enter to send, type exit + enter to exit.")
    16.         While line <> "exit"
    17.             line = Console.ReadLine()
    18.             plants.Invoke(Of Object)("GetPlant", line).ContinueWith(Sub(data)
    19.                                                                         If data.Result IsNot Nothing Then
    20.                                                                             Console.WriteLine(data.Result.ToString)
    21.                                                                         Else
    22.                                                                             Console.WriteLine("No plant with that id.")
    23.                                                                         End If
    24.                                                                     End Sub)
    25.         End While
    26.     End Sub
    27.  
    28. End Module

    I just added the plants.On method to intercept the addMessage events.

    And now when I open multiple clients I get this.

    Which is cool.

    Conclusion

    Hubs are what you will mostly use when you use SignalR.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    signalr, vb.net
    InstapaperVote on HN

    3 comments

    Comment from: Someone [Visitor]
    Someone "Ass you can sse I use Clients.All.addMessage to send a message to all Clients with the id that was requested."

    Can you please check the spelling before publishing . Instead of "As" you have written as "Ass"
    11/23/12 @ 01:50
    Comment from: Christiaan Baes (chrissie1) [Member]
    Christiaan Baes (chrissie1) Thanks I fixed that. Sorry.
    11/23/12 @ 02:18
    Comment from: Prasannanjaneyulu.K [Visitor]
    Prasannanjaneyulu.K Good
    11/23/12 @ 05:34

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)