Designing and coding

Menu

Learning ASP.NET SignalR – Part 1

Today I am going to discuss one of the new technologies that changed the way we used to think about How the web works. I’ll be writing a series on this technology and this post is first part of it.

You must have been working on the ASP.NET for long or might be since inception. And till date (or till this technology got introduced) , You got to know from every where that Web is stateless. A webpage is recreated every time from scratch it is posted back to server. In traditional web programming, all the information within the page and control gets wiped off on every postback.

I have taken the above lines from one of my post which I had written long back.

The web page used to work on PULL approach means you have to pull the data from the server. It means whenever you want to get some data from server, you require to send some request to server so that it can send the updated data along with the response. Remember, many times we required to update the UI as soon as the data gets updated to the server and then we used some polling method which keeps sending some request to server and checks if the data is updated and if yes, it refreshes the page or you have done something similar. So it works like

But things are changing rapidly. Web Sockets introduced in HTML5 provides us a capability to create a full duplex channel over web.

But SignalR provides an abstraction over persistent connection. It makes you tension free about the type of browser and web server versions.It works in a consistent way in all the environment.

Although all the latest web browsers supports the new technology WebSockets but this is not the case every time. Some of your user might be still using IE6/IE7 etc and you cannot simply say that I cannot support this. Ultimately this is buyer’s world. So you have to support as per your Client’s requirement and here SignalR wont let you down. So How does SignalR work on all the browsers? It actually checks the browser’s capability and accordingly takes the decision as

So I think you all must have got an Idea the way SignalR works. but let’s try to dig into it. As you can see in the pic, it starts from top to bottom as if the environment supports Web Sockets else server sent event else forever frame else polling, means it has fall back one step further until your browser supports it.

So in simple words, What is ASP.NET SignalR?

We can say it is Real time – Means updates the web page Real time
Push Approach – Server pushes the data to all the connected Clients.

So let’s see How it works pictorially?

As you can see in the above pic the An event generator generates event and notifies to SignalR hub. Signal R Hub process it and forwards the the message to the appropriate Client(s).

You also can see that Client could be any like any .NET windows, web, console client, WP, Windows store apps, Android etc. Similarly, event generator could be any Client initiates notification on some logic, database, any other server based on some logic. You can imagine the reach and simplicity of it. And I can assure you that it is very fast as well.