Tuesday, 11 October 2016

Getting started with SignalR

Follows on from 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 hookup 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 initialised (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.

No comments:

Post a Comment