SignalR Hub demo site

I needed a solution to broadcast real-time data from a webserver to either a desktop or webclient. SignalR is perfect for this. I’ve uploaded a simple working demo for SignalR using the hub strategy. This is what you’d typically use in a broadcast type of system. This could be a chat or stock-market quotes client. The system needs to send information for any user who’s subscribed.

For this example, I created a simple server timer thread which updates both a web page and console window.  Both clients are updated by “push” notifications from the server.

The same libraries are used for both desktop web clients. This makes implementation maintainable. In addition, the integration with ASP.NET MVC4 allows for very rapid development.

There are quite a few steps involved to get SignalR Hubs working the first time if you’re using ASP.NET MVC4.

The entire working solution can be downloaded here:

SignalRDemo

Use NuGet and add the SignlaR package to your ASP.NET MVC4 project

In the Solution Explorer, right click on your ASP.NET MVC4 project and select Manage NuGet Packages. Search for SignalR. Select the top package as shown.

SignalR NuGet Install

Subclass Hub and create your logic

I created a folder for my Hubs in the ASP.NET MVC4 project.  The code is very simple.  If a client subscribes to the “addMessage” signal, they will receive the message each time the timer elapses.  I also have a StartTimer method to demonstrate that a client can send controlling messages to the server.

Update BungleConfigs.cs to include the SignalR scripts

You must the new SignalR scripts to the site.

Add the auto-generated SignalR hubs scripts to the layout

In _Layout.cshtml, I render the SignalR scripts and the hubs scripts.  I also add my custom demo js which is covered next.

Create a JavaScript file to connect/respond to the server

This is very simple. I create a connection to the TimeQuery hub and subscribe to any “addMessage” signals.  Then I start hub listener.  Once the hub listener connection is made, I start the timer.

Looking at the result

Take a look at the project.  When you run the example, you’ll find the server timer starts and the web page is updated by the server.  Take a look at the network traffic.  Chrome only shows network traffic for setting up the connections for SignalR.  Monitoring my SignalR traffic is something I still need to look into.

Share this Post