SignalR – Real Time Web

Communication between the server and the client via JavaScript has been around on the web for a long time. Prior to AJAX there were ways to use HTML elements in ways they were not intended to allow client server communication without refreshing the page. The img, script, and iframe tags could all be used to retrieve data from the server without having to perform a full page load. After this we moved on to AJAX (Asynchronous JavaScript and XML). The XHR object was introduced into JavaScript to allow client server communication. This provided a neat way to provide quality web applications, where we could load in data without having to perform an entire page load.

However AJAX is not perfect. Real time web is still a dream with AJAX. When using HTTP requests, we must poll the server repeatedly to check for new information. Imagine an application where you have a user awaiting another user to upload a document. The first user registers an interest in the document that another user is currently uploading. The client then sets up a poll where we repeatedly ask the server whether the document exists yet, and the server must constantly respond with an answer of no, until the document is uploaded, when it finally responds in the affirmative, and the process stops, the user is notified the document exists, and everyone is happy.

There are a number of problems with this approach. Firstly we have a lot of overheads, we are sending the full HTTP headers with every request and response. Because of this, we then have to decide how regularly we want to poll. The document may take several minutes to upload, so polling every 30 seconds may be fine, however the document may be quick to upload so perhaps a check every second would be better. But we shouldn’t have to make these sort of decisions. What we really are trying to do here is get the server to notify us when the document is uploaded. This is exactly the sort of issue SignalR can address

With SignalR we do not have to make repeated requests. SignalR provides an open channel of communication between the client and the server. With SignalR in this situation, a user registers an interest in the document just once, the server would take note of this, and when the document finishes uploading, the server sends a message back to the user that registered an interest. The only communication the client had to make to the server was telling the server it wanted to be notified when the document was uploaded, and then as there is an open channel of communication, it can just receive a message from the server when the document is ready.

So you are probably wondering how to use this magnificent tool in your applications. It sounds really complicated? Actually SignalR is really easy to use. It’s a simple case of adding a nuget package to your application, and setting up a couple of key configuration stages and you are ready to go. I will go into more detail in a future post, but if you are interested in starting now, you can take a look at the official SignalR page from Microsoft that has some examples to get you started with the future of client server communication.

For a basic tutorial on using SignalR, see my post on a basic chat application with SignalR

Leave a Reply