Real-Time Web Enablement with SignalR in .Net (Part I)

We know that Twitter is the classy example of the real-time web and we can find the latest idea, stories, trends and what not on Twitter as it happens. But then again what is real-time web and what is so gripping about it? The real-time web is fundamentally different from real-time computing since there is no knowing when, or if, a response will be received1 and HTTP being a stateless protocol, the server does not have any connection with the client once a response is sent back. So how can a server make its connected client update with some information when the client has not asked for it and can I achieve it for my web application to make it trendy? The Answer is Yes… read on to learn more about the Enabler, SignalR.

So What is SignalR

Real-time web functionality is the ability to have server-side code push content to connected clients instantly as and when required2 and ASP.NET SignalR is a library that enables applications to include real-time web as a feature in no time. It offers a simple to use, high-level API for making server to client RPC in ASP.NET applications, through which we can call JavaScript functions in clients’ browsers from our server-side .NET code. Enhancing it for connection management, e.g. connect/disconnect actions, grouping connections, authorization is also doable with SignalR, although Authorization is not offered out of the box here.

It is an Open Source library and currently licensed under the Apache License, Version 2.0. One may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

How SignalR Works

SignalR is something that sits a layer above all of the different techniques of transport whether it’s Web Sockets or AJAX long polling or server-sent events. I, as a developer, code against the SignalR API, and SignalR takes care of ensuring that the appropriate persistent connection passage is set up and maintained between the browser and the server. So it is like layered abstraction over a connection.  

When an application has a SignalR connection and it wants to send some data to the server, data is not sent in a raw form; SignalR wraps that data in some JSON along with other information and wraps it all on the JSON payload before sending it to the server. Similarly on the server side, when an app broadcasts data to all connected clients, it does not just broadcast the raw data but a bunch of framing information that contains connection information as well.

A typical request header sent through SignalR client could be something like the following.

POST/signalr/send?transport=foreverFrame&connectionId=ba0c3fe4-1a34-48b4-9a3a-8c7cdc02b5d9 HTTP/1.1

It has transport type (Web socket/Forever frame etc.), Connection Id and HTTP protocol information.

Most of SignalR pieces are replaceable with customized implementations. You can check more at https://github.com/SignalR/SignalR/wiki/Extensibility

SignalR consists of a client-side library and a server-side library that work together; let’s check these one by one.

Server

There are two programming prototypes of servers possible with SignalR.

Two programming prototypes of servers
Two programming prototypes of servers

  • Persistent Connections
    Persistent connection, the simpler of the two possibilities, provides mechanisms to alert the connection and disconnection of users, and to manage asynchronous messages to linked users, both individually and collectively. In this kind of implementation, your endpoint or server derives from PersistentConnection class.
  • Hubs
    Hubs provide a development interface, which is much easier to use. The integration between client and server is almost seamless. Hubs provide a higher level RPC framework over a PersistentConnection. It is advisable to use when the application has different types of messages that need to be sent between servers. We can create the same applications using persistent connections, but with hubs it will be simpler.

And Self Host

Any .NET application (console, forms, Windows services…) can act as the host of SignalR services. In case you want to run your host outside IIS, you can code for having Self host.

Client

The SignalR JavaScript client comes in the form of a jQuery plugin. However, SignalR does not just have JavaScript client library. It is also possible to find client components for Windows Phone, Silverlight, WinRT or generic .NET clients, which extends the range of SignalR’s applicability as a framework for real-time applications in any kind of scenario.

Different client components currently available are:

  • Javascript              
  • .NET 4.0
  • .NET 4.5
  • Silverlight 5
  • WinRT
  • Windows Phone 8

SignalR features
SignalR features

Transport

SignalR is built upon the idea of transports; every transport mode decides how data is sent /received and how it connects and disconnects. SignalR supports multiple transport modes with the assigned priority. If the client’s browser supports Web Socket transport then communication happens in this mode else it falls back to Server Sent or Forever frame; if browser does not support these two then it makes use of the long polling mode for communication. 

Multiple transport modes
Multiple transport modes

Though SignalR tries to choose the “best” connection supported by the server and client, nevertheless you can also force it to use a specific transport. At the time of starting the connection, you specify transport mode as:

//try only longpolling
connection.start({ transport: 'longPolling' });

// try longPolling then webSockets 
connection.start({ transport: ['longPolling','webSockets'] });

The Magical Connection ID

When an app is using SignalR, every connected client is assigned a connection id, which is regenerated at every page refresh. The server identifies the calling client and other connected clients with the help of this connection id only. Any connected client can invoke a server method from the script with the help of proxy handler. And at the server end, along with the relevant processing, a client side method can be invoked. That makes it a Bi-Directional RPC, where one client invokes a server method defined on the hub and from this server method you can invoke methods at all/selected/calling client.

Where can I use SignalR?

SignalR works deliciously for simple notifications where we need to update all connected users for some news feed and broadcasting. However it can handle complex scenarios such as Chat, Co-Creating, Gaming, etc. Here is the small snapshot of the usability scenario.

Notification

Chat

Co-Create

Gaming

Notify All or selected clients.

Notification could be of numerous types, like incoming message, some alert, progress bar, reminders, comment or feedback on your blog or post, etc.

It is easy to implement chat with the help of SignalR. Chat can be one-to-one or Group chat. You can try out a simple chat application as described here: http://www.asp.net/signalr/overview /getting-started/tutorial-getting-started-with-signalr

Two or more connected users can enter into co-create mode similar to the one described here: http://www.codeproject.com/Tips /417502/Online-Whiteboard-using-HTML5-and-SignalR

It enables applications like gaming, which require high frequency push from the server. You can check out the example given on the SignalR site:

http://shootr.signalr.net/

Perspective

SignalR and its usability looks very promising; it lets developers embed real-time web in the application without getting their hands dirty. It’s on GitHub and there is a dedicated team working tirelessly to make it even better. New users get instant support on the forums as well. To get hands on with this very promising library from Asp.net, read Part 2 of this article to know how to actually do that.

References

1.        Real Time Web. Wikipedia”

2.        Introduction. “SignalR

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read