Why should ASP.NET developers consider SignalR for ALL projects?

The concept of the “static” web went away a long time ago.  When web developers started looking towards jQuery to build “ajax” web applications, we hit a wall where an instance of a page was a dynamic object to be manipulated.

This turned even more when the first HTML5 specs came down the line, and we were introduced to WebSockets.  An amazing idea: a client application being able to connect directly to a server for communication purposes.  I’ve said it before: software development works in a circle.  If you’ve ever written a client-server application, you know what I mean.  In my 9th grade computer science class, I wrote a version of Battleship that used Berkley sockets to communicate peer to peer with a computer over the network.  Fourteen years later, I’m doing the same thing again but on the web.

The problem we face when dealing with technologies like WebSockets is that it’s not universally supported across all browser and server stacks.  If we focus on .NET developers, the majority of us are running web applications on IIS version 6 or 7.  WebSockets are only officially supported on IIS version 8.

If your users are using IE6-9, you’re also at a loss.  Internet Explorer 10 is the only Microsoft browser to support WebSockets out of the box.  Chrome, Firefox, Safari, and Opera users don’t need to worry – they’re covered.

So if we can’t have WebSockets, can we still have a rich peer to peer communication stack like WebSockets? YES.

There are various ways to implement solutions.  For example, a long time ago I used to implement ‘Interval Polling’ on my applications.  On a standard interval, say 60 seconds, I would go to the server and request new data.  If the server has data to return, that was the time to do it.  If not, the cycles are wasted.  Obviously, this is a huge bandwidth drain and the overhead of each call is cumbersome.

Other solutions include long polling, which is the same as interval polling, except the request remains open until the server decides to return data or until the connection times out.  Then there is Server Sent Events, which don’t even ask me about.  I understand it’s based off some old Netscape technology, but that’s about it.

There are a lot of solutions.  Some only work on newer browsers.  It’s a chore: how do you support multiple clients without tons of code rewrite?

The answer my friends, is SignalR

SignalR is a framework for building asynchronous applications.  For web developers, that means I can build applications that break the request->response cycle of the web and move to more of a one-on-one connection that old client server architectures used to offer.

The mistake a lot of developers make is assuming that SignalR is strictly for web.  That’s incorrect.  For you server infrastructure, ASP.NET is the most common way to building your applications.  However, you can also self host SignalR and also host using the OWIN standard.

As far as clients are concerned, JavaScript is the main way.  In fact, when I demo SignalR it’s 100% all JavaScript.  I do make a point of touching on the fact that SignalR is also support in Silverlight, Windows Phone, iOS, OSX, and even in regular ol’ C#.  If you have a single server implementation, you can use multiple clients to connect to it.

Ok, Griff.  This must complicated to code, right?

I would be a bad blogger if I just left you hanging.  Here’s my 5 minute SignalR demo for you:

The video does a really good job of showing you all the steps.  There are two parts to a basic SignalR application.  First is the server side, which we build a construct called a Hub.  Think of a Hub as a central location for all connections for your application.

Like code?  Here is ALL the C# I write to implement a server-side function:

The most confusing thing here might be this mysterious Clients object.  The Clients object is dynamic, meaning you can set properties or call methods on it that might not really exist physically.  SignalR will take all those calls and translate them into messages that are sent to each client currently connected to the hub.

Here’s a look at the code over on the client:

The request to the SignalR JavaScript file is straight forward, but then there is this magical request to SignalR/Hubs that you might be wondering about.  This file is dynamically generated by SignalR.  It gives your page all the information it needs to talk back to any Hubs you might have created.  Because of this, I can use the object $.connection.chatHub and JavaScript doesn’t throw a fit.  It’s a real object!

I use the chatHub object in two different ways.  First, I create a new function off of it called writeMessage.  This is called from the server.  WAT?  Yes!  If you look at the hub code above, I’m calling Clients.writeMessage and the client has a method called writeMessage!

Just below that is a call to chat.broadcastMessage.  Look back at the hub, the method broadcastMessage is defined!  The circle of life is complete.

Ok.  LASTLY, there is this call to $.connection.hub.start()This method kicks off the transport negotiation between your server and client.  Websockets?  Long pollings?  Blah blah blah.  You can override all this stuff, but just let SignalR do what it does best.

And there you have it!

SignalR is extremely easy to use, and I’m definitely recommending it as a replacement for 90% of the .NET AJAX work I’m seeing done in the community.  Take a couple minutes, download the package via NuGet and try it out for yourself.  You won’t be disappointed.

Feel free to ask any questions, and hit me up on the Twitters @1kevgriff.


Do your Windows Servers need an easy monitoring solution? Try Winsitter today!

Don't forget to follow me on Twitter! I'm @1kevgriff

Have the bytes gotten you bit? When in doubt, Consult with Griff
  • Pingback: Why should ASP.NET developers consider SignalR for ALL projects? | Kevin W. Griffin | Web tools and technologies | Scoop.it

  • http://twitter.com/kimrossey Kim Rossey

    Excellent. Thanks for the post and taking a look straight away.

    • http://www.kevgriffin.com/ Kevin Griffin

      Good luck!  Thanks for reading!

  • Luis

    Hey! Grat article. Im guessing that 90% of yhe ajax work you are talking about is the simple crud actions we do every day… In this secenario, can we take advantages using signalr?

    • http://www.kevgriffin.com/ Kevin Griffin

      Exactly.  SignalR is a simpler syntax, and I believe enforces a better workflow.  Issue a request, and the server will initiate the response when it’s ready to.   

      • Luis

        Hmm, i’m not sure about the simpler syntax. With signalr, you have to maintain the user id across pages, assure every response reaches only the specific user and stuff like that. 
        I read your post about maintaining the userId across pages with signalr. Despite being simple, i don’t have to do this with the ajax work.
        Please, don’t get me wrong. I really want to adopt the SignalR, but can’t see the light yet :(

  • Tudor

    I would say that 90% of the ASP.NET web applications written today by regular developers, are still CRUD, ERP-style applications, forms-over-data style – how exactly can SignalR help in this area?

    Sure, there are cases when there is some long processing being done on the server which needs to send status updates to the client, or some fancy grid that has to be updated from the server-side but other than that it’s still the old request-response cycle..

    • Jeremychild

      +1 Tudor.

  • Joseph Cooney

    What about scale-out/NLB?

    • http://www.kevgriffin.com/ Kevin Griffin

      Supported.  Version .5 had a radical jump in support for working in farms.  I believe there is official support for Azure Service Bus and Redis at the moment.

  • Pingback: The Morning Brew - Chris Alcock » The Morning Brew #1143

  • Eduardo

    Every sample I’ve seen it’s the same broadcast example.
    Where is that facebook-notifications-style sample? :)
    I’m still a bit confused about handling single-user messages.

    • http://www.kevgriffin.com/ Kevin Griffin

      Absolutely.  I have samples in the works that would demo this.  Keep an eye out.

  • Ralph

    So what if you wanted to talk back to just ONE client. I tried to use Caller instead of Clients but the callback still went to both clients.

    • http://www.kevgriffin.com/ Kevin Griffin

      Caller will only send to the client which made the call.  Additionally, you can use Clients[Context.ConnectionId] to send to a particular client that’s not the Caller.

  • Pingback: Biztalk Musings | SignalR

  • Driggs

    I think it’s a very bad idea to recommend this in place of standard REST calls. You lose header data, http status codes, cors support and more. SignalR, socket.io, and this style of communication is super useful, but should be confined to situations where it makes sense, and im my personal view that is certainly not 90% of ajax work being done.

    • http://www.kevgriffin.com/ Kevin Griffin

      Thanks for noting the other 10%.

      • Jeremychild

        Rest is clearly the right way to do it when you have other applications that need to integrate.

        • http://www.kevgriffin.com/ Kevin Griffin

          I’m not denying that.  This article was meant to discuss general uses.  If you’re building an API, that’s another topic all together,

  • Sobin Thomas

    Thanks for this excellent article!. My doubts,Can I do the same with webforms?  Can I host it in iis 6?

  • Pingback: Friday Links #211 | Blue Onion Software *

  • Ruhollah Heidarpour

    Which  Framework versions does it support? Can we use it in VS 2010?

  • Pingback: Why should ASP.NET developers consider SignalR for ALL projects? | Kevin W. Griffin | Programming Development Architecture | Scoop.it

  • Pingback: Cheatsheet: 2012 07.11 ~ 07.18 - gOODiDEA.NET

  • Pingback: Our ComponentOne | Blog | A ViewModel Walks Into a Bar and Gets Knocked Out

  • http://twitter.com/CuriousDevelopr Curious Developer

    Thanks. I appreciated the article and the discussion much. I hope to see more discussions about this. I’m experimenting with using SignalR instead of standard request over rest on a intranet site with pretty standard data (ajax) functionality, and I still fail to see the drawbacks (not building api’s here). I pretty much get the same productive workflow as I have when I do native apps (or better, since it’s multi-user ootb) and thats a really nice thing.

  • kamranayub

    The other thing to consider with SignalR, depending on your user base, is the extra load generated on the server. I think you’d use SignalR when it makes sense, and there’s no stopping you from combining AJAX or SignalR for specific pieces of your application (for example, use SignalR for live build/deploy status on your build server, but use a normal forms POST [or AJAX] for the admin section that’s used 5% of the time by one team). Have to weigh the benefits and make an informed decision, it shouldn’t be a blanket “SignalR runs everything” type thing.

  • Pingback: Using SignalR to build an Api (in Umbraco, WebMatrix or MVC) | Coding in the cloud

  • Pingback: Getting Started with SignalR | As Simple As Possible, But No Less