SignalR

SignalR is an asynchronous signaling library for ASP.NET that helps you build real-time multi-user web applications.

You can start playing with it by getting it with NuGet:

Install-Package SignalR

SignalR is broken up into a few package on NuGet:

SignalR – A meta package that brings in SignalR.Server and SignalR.Js (you should install this)
SignalR.Server – Server side components needed to build SignalR endpoints
SignalR.Js – Javascript client for SignalR
SignalR.Client – .NET client for SignalR
SignalR.Ninject – Ninject dependeny resolver for SignalR.

If you just want to play and make a small up, start up Visual Studio 2010. First, make an Empty ASP.NET application, and install-package SignalR with NuGet, either with the UI or the Package Console.

Second, create a new default.aspx page and add a button, a textbox, references to jQuery and jQuery.signalR along with this script.

The /signalr/hubs endpoint exposes all the available hubs in the solution and lets you access their public methods. You will first need to initiate connection.

Entire page source below:

We have two choices of models with SignalR: low level using PersistentConnection or highlevel using Hub. Hubs expose public methods to the clients and raise callbacks on the clients. In most web-based scenarios, you will be utilizing hubs, which follow the publish-subscribe pattern. See Hubs for additional details.

When a connection between a web client and a server is made, SignalR will determine a suitable transport type based on your client capabilities. It will gracefully degrade so older browsers might get long-polling instead of the fancy WebSockets. The transport mode can have a significant impact on the performance of the app.

WebSockets (bidirectional stream)
Server Sent Events (push notifications from server to browser using DOM events)
Forever Frame (uses HTTP 1.1 chunked encoding to establish a single long-lived HTTP connection in a hidden iframe)
Long polling (hit the server hit the server hit the server hit the server hit server and hope something comes back with data)

We’ll use second approach based on Hubs. Simply create a class and inherit from an abstract Hub class.

When you want to distribute messages to your clients, you can do so using the Clients dynamic object. Any method that you call on Clients will raise a callback on the client. Furthermore, you can get access to the current client call id using Context.ConnectionId or Groups dynamic object which looks after groups management. To publish on a specific connection you can use Clients[“groupName”].method(params) or Clients[Context.ConnectionId].method(params).

Please check this link with a short demo with MVC3, signalR and backbone.js.

Live presentation from authors of SignalR can be found on channel9.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.