SignalR - A small Introduction

Few months back, I read a post from Scott Hanselman on developing applications where-in server can push the data to the clients rather than clients requesting it. In fact, I did not pay much attention to it till yesterday as I had read something about web sockets already.

In next two posts, I will be talking on that possibility with an example but let me first explain a small problem and the usual solutions to it. It’s a lengthy post but hope you will not regret after reading.

Say, there is a web application with a screen for managing employees with add/delete/update functionalities and displays the updated list of employees in a grid after every operation.

Assume, if this screen is being used by multiple users simultaneously, how do all the users see the much updated list of employees?

User1 adds an employee but user 2 cannot see the employee it till he refreshes the page. And if User 2 deletes an employee, that employee will still exist on other users list till they refresh the page. Well, this is not a big puzzle I know but there were quite possible solutions.

1)      Automatically refresh the page every few seconds to get latest data.
a.      Well, you lose so much of data and there is a visible post-back!!
b.      If you were in the middle of typing something, it’s gone as the page refreshes!!

2)      Have a timer in JavaScript and repeatedly call a function and using XML Http, keep checking at the server, if there is any change in data.
a.      Now, the problem with this approach is, it’s very expensive, non realistic, determining the interval for reloading a part of page is too difficult. Also, what if there are many hits to the server!!

3)      Long Polling: Open a connection to server and keep waiting without closing it and from the server and then return data without a open connection. However, doing this is not easy especially in!

To address these kinds of scenarios, many things came up. Comet, Reverse Ajax and what not. However, none of them found to be easy as it sounded to be. This is just an example problem but how do you develop real-time multi user applications?

Use SignalR.

SignalR is an asynchronous signaling library for ASP.NET to help build real-time multi-user web application. It is a complete solution to build real-time applications with JavaScript on client and ASP.NET on the back end. Well, you can just have an html file, use some JavaScript’s and few dlls at server and develop a real-time app. It’s as simple as that!

What are the possibilities with SignalR?

a.      You can just build a chat application in just few lines of code
b.      Develop multiplayer games and allow them to play realtime
c.      You can develop real-time live applications to shoot data to few/all users at runtime.
d.      Show live stock data to your users. Your users don’t have to click a refresh button!
e.      Show a live chart or a live hit counter on the number of current users on your website
f.        Display advertisements to all users who are active on your website
g.      Show progress of their file up-loads
h.      Use it for Online bidding, Sports Scores
i.        Use it to make your application very real. Show data changes to all users at real time.

I would say, the possibilities are infinite.

How to use SignalR? & Do you have samples/code?

Please see my next post for code and a Proof of concept application.

Popular posts from this blog

Facebook Javascript API : Feed and Share Dialog for Beginners

Real time Push Notifications with SignalR & PNotify (Pines Notify)

What's new and expected in .NET Framework 4.5