Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting Started With SignalR

DZone's Guide to

Getting Started With SignalR

The author shares some things he picked up while he was learning about SignalR. Come check it out!

· Web Dev Zone
Free Resource

Should you build your own web experimentation solution? Download this whitepaper by Optimizely to find out.

This article picks up from the author's previous post: Real-time SignalR and Angular 2 dashboard

I had a couple of questions from someone who forked the project on GitHub and is using it as the basis for their own custom dashboard, which reminded me that there are a few key things I picked up on my learning journey that I should share. These are primarily focused around SignalR aspects.

An Instance of a SignalR Hub Is Created for Each Request

A single instance is not shared across all clients connected to that hub. This is much like a controller in ASP.NET MVC, whereby a new instance of the controller is created for each request. Hence in my SignalRDashboard project, the mechanism for holding a single instance of shared data for all connections, is via a singleton instance that gets passed into each hub's constructor.

Setup Callback Methods in Javascript for SignalR Hubs Before Starting the SignalR Connection

To open a SignalR connection, you use the following:

    $.connection.hub.start();
 To be able to receive messages from the server-side SignalR hub in client-side Javascript, you have to hook up a method to be called. You'd do this using something like: 
    var hub = $.connection.myDemoHub;
    hub.client.updateMeWhenYouHaveUpdates = function(message) {
        // Do something useful with what we've just received from the server
    };
 The import thing to remember is once you've started the SignalR connection, you can't hook up these callbacks. So make sure they are done BEFORE you start the connection. The correct ordering would be: 
    var hub = $.connection.myDemoHub;
    hub.client.updateMeWhenYouHaveUpdates = function(message) {
        // Do something useful with what we've just received from the server
    };
    $.connection.hub.start();

You Can Connect to Multiple SignalR Hubs Over the Same Connection

This is great as it means, you can connect to multiple hubs easily — all you need to do is setup the callback methods in Javascript before you open the connection, as per the previous point. 

    var hub1 = $.connection.myDemoHub;
    hub1.client.updateMeWhenYouHaveUpdates = function(message) {
        // Do something useful with what we've just received from the server
    };

    var hub2 = $.connection.myOtherHub;
    hub2.client.callMePlease = function(message) {
        // Do something useful with what we've just received from the server
    };
    $.connection.hub.start();
 In the SignalRDashboard project, you'll see this being handled in dashboard.js as follows: 
  • each Angular 2 dashboard component registers itself with dashboard.js when it is constructed
  • once the component has been initialized (ngOnInit event), it lets dashboard.js know that it's finished and registration is completed
  • once all components have completed registration, then the initialiseComponents() method is called which then calls in to each registered component (each must expose a setupHub() method) so they can set up the callbacks they're interested in from the hubs they use
  • finally, the connection is then started

The basics are covered well in the official SignalR site, but the points above were some of the main things that I found myself looking a bit deeper for.

Implementing an Experimentation Solution: Choosing whether to build or buy?

Topics:
singleton ,new ,instance ,server-side ,real-time ,custom ,asp.net mvc

Published at DZone with permission of Adrian Hills, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}