Friday 26 April 2013

Closer Look at SignalR

This post is in continuation to the previous SignalR Introduction Post here.
If you know what SignalR is and want to get into it, you can skip the above mentioned post and continue reading below.

Before we start digging into SignalR code, lets see a sample application from NuGet running on SignalR.

NOTE: Wondering what NuGet is? Refer to this wonderful NuGet Intro.
  • Open VS 2012.
  • Create a new asp.net 4.5 Empty Web Application.
  • In the solution explorer, right click on References and select Manage NuGet packages.
  • In the search box type "signalr sample" as shown below:


  • Click Install button on "Microsoft.AspNet.SignalR.Sample".
  • Accept the license. It will install the new SignalR sample stock ticker application.
  • Open the stockticker.html in two browser instances and click open market. 
  • You will see both instances of browser in sync while updating stocks after getting data from server.



 


This application is using websockets under the hood to push the data from server to client.
You can open the fiddler and monitor the traffic. Let me just see the logs that are coming in my fiddler:













Oops!

Fiddler says the application is using ServerSentEvents as the transport rather than websockets. If you remember we discussed (in previous blog) about this fallback option that SignalR will use in case WebSockets are not available as below:











But why is SignalR not using Web-sockets. Well the reason for that is Web-sockets are not natively supported in Win7.

Lets run this application on Windows 8 and see what happens. You will see fiddler will show transport as web-sockets. Great!










Now that we have seen SignalR working in a cool sample application (with actually using Websockets when available), its time to drill into the more details of how it works.

NOTE: Wondering what exactly are WebSockets? Refer to this wonderful Wiki link here. For those who already know just continue reading.

Lets see now what composes SignalR.
Complete SignalR code is divided into few NuGet packages.(What are NuGet Packages? Click to read...)

NOTE: NuGet Package: Everything necessary to install a library or tool is bundled into a package (a .nupkg file). A package includes files to copy to your project and a manifest file that describes the contents of the package and what needs to be done to add or remove the library.

I created a picture for high level package view as below:

















When you downloaded the clock ticker sample application above, NuGet actually pulled the Samples Package shown in above picture, from its repository along with all its dependencies (which in this case was the rest of the packages shown above in picture.)

To start with and for this blog post, we will just focus on the Core package. So, lets begin our Journey to the Core.


The Core SignalR Package

This package contains Microsoft.AspNet.SignalR.Core class library project which constitutes of several important classes responsible for building server side components needed to build SignalR endpoints.
The core revolves around Connections and Hubs. Lets first see what are these and how they are different from each other.

Connections: In SignalR connections is a low level API, for making RPC calls from server to client or client to server to send raw data. User is responsible to send and receive data and then frame that data into meaningful message. In the core package connections is represented by Microsoft.AspNet.SignalR.PersistentConnection class. It exposes SignalR service over the http.

For more details refer the git-hub PersistentConnection documentation here.

Hubs: Hubs on the other hand is easy to use high level API which is built on top of the Connections API. It enables you connect to the client from server and vice-verse with all the underlying connection plumbing being done by SignalR.

For more details refer the git-hub Hub documentation here.


Sunday 31 March 2013

Introduction to SignalR

SignalR is an asp.net library that we can use to add real-time functionality to our applications. Under the hood it uses HTML5 websockets and various other old techniques as a fallback option to support realtime communication between client and server. We will explore all these in detail in this signalR blog.

In this context real-time means pushing information from server to client as and when it happens or is available with the server.

Damian Edwards at Microsoft and one of the creators of SignalR describes it as "Incredibly simple real-time web for .NET". Well, we will definitely explore simplicity of this library in upcoming posts on this blog.

SignalR started of as a side project and was later on included into ASP.NET out of the box and fully supported in release "ASP.NET and Web Tools 2012.2".

So now SignalR is a part ASP.NET family.


There are many scenarios in today's world, be it an Enterprise or social media etc, where real time has almost become a prerequisite. I have personally worked on many applications where we have to constantly fetch updated data  from the server and display to the clients. We would end up either giving a refresh button(really old school :) ) or creating the page as auto refresh with meta tags or do some timely polling to server from client with some ajax calls.

SignalR has come to your rescue in such scenarios(and many more..). It provides you the capability to actually establish a persistent connection between client and server using web-sockets (if available) and lets server communicate with client seamlessly.

SignalR is a real-time, persistent connection abstraction over HTTP for .NET. I believe what Microsoft's intent in creating such a library is to let you write code against SignalR classes that abstracts out the low level  details of managing client connections.

We all know that Websockets is an upcoming standard and is only supported in new browsers like IE 10, chrome 16+, hence SignalR has a beautiful support for fallback to our old school techniques.



SignalR will check of web-sockets support in the client browser. If it is not present it will fallback to other techniques like Server Sent Events, Forever Frames or Long Polling. I will describe about all these in some of my upcoming posts as this post is more focused upon SignalR.

SignalR is open source and is available on GitHub here.