DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Getting Started With SignalR

Getting Started With SignalR

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

Adrian Hills user avatar by
Adrian Hills
·
Oct. 15, 16 · Web Dev Zone · Opinion
Like (3)
Save
Tweet
5.67K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Connection (dance)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • JIT Compilation of SQL in NoSQL
  • Monolith vs Microservices Architecture: To Split or Not to Split?
  • Understand Source Code — Deep Into the Codebase, Locally and in Production
  • What's the Difference Between Static Class vs. Singleton Patterns in C#?

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo