In November, Microsoft announced a number of features coming to its ASP.NET Web application framework over the next few months. One of the more prominent announcements was SignalR, a feature that adds “real-time functionality to Web applications using WebSockets and other down-level transports.” It’s available as a Release Candidate , but a Microsoft blogger is already showing off some tips and tricks on how to get the most out of it.
Microsoft blogger, Mike Brind, recently posted a pretty extensive tutorial on his blog that goes into how developers will want to use SignalR in building ASP.NET Web pages with WebMatrix. First things first, however, he explains what SignalR is and what the new library can to accomplish:
The web works on a Request-Response model. Browsers and other user agents make requests and web server provide a response to that request.The response is sent to the delivery address provided in the request by the user agent. And that is the natural order of things on the Web – servers can’t make responses without a request. For the most part, this is not an issue, but if you want to display real-time updates on your web page, you have needed to resort to techniques like repeatedly polling the server using AJAX to see if any changes had been made to data. Alternatively, you could use Comet technology, which keeps a persistent connection open between the server and the client. HTML5 introduced two new techniques – Server Sent Events and WebSockets. SignalR is a user-friendly wrapper around all these technologies that makes it a lot easier to create applications that require the real-time display of data. SignalR utilises HTML5 Web Sockets API where it is available, and falls back onto other technologies where they are not – Server Sent Events, Forever Frames or Long Polling, the last two of which are Comet techniques.
Brind suggests that any interested developer obtain the prerelease version of SignalR via Visual Studio. He suggests using the Package Manager Console and installing it via the Install-Package Microsoft.AspNet.SignalR -Pre command.
From there, Brind explains the basics of SignalR including how you set up an application that uses the library. He says that an application is powered by a hub, which holds some similarities with a Controller in ASP.NET MVC. Those familiar with that should have little to no problem adopting SignalR into their own applications.
For the rest of the post, Brind gets into the nitty gritty of the code required to implement SignalR into your ASP.NET Web pages. His first example shows how to create a Chat window on a Web page using SignalR. The second example is a little more complicated as he combines SignalR with Knockout to create a simple product listing page where users can view details about individual products.
You can check out both of Brind’s examples at his GitHub page, and get refreshed on all the latest changes to SignalR at the official Web site.