164
votes

Can anyone let me know how SignalR works internally in a high level way?

I am guessing it is flushing the data using Response.Flush and at client side it is sending Ajax requests at certain intervals. Is it correct?

2
@dfowler did an interview with Scott Hansleman recently that was very informative hanselminutes.com/291/….AlignedDev
Good article about signal r architecture and how it works lostechies.com/erichexter/2012/11/05/… asp.net/signalr/overview/getting-started/…Mahesh
I know its a long time ago but microsoftvirtualacademy.com/Content/…lordkain
Also see this presentation at NDC 2013 from David Fowler and Damian Edwards called 'Under the covers with ASP.NET SignalR' where they build a lite version of SignalR live on stage. It's very informative. It's pre 2.0 but that should not matter much. vimeo.com/68383353Johan B

2 Answers

251
votes

No, SignalR is an abstraction over a connection. It gives you two programming models over that connection (hubs and persistent connections). SignalR has a concept of transports, each transport decides how data is sent/received and how it connects and disconnects.

SignalR has a few built in transports:

  1. WebSockets
  2. Server Sent Events
  3. Forever Frame
  4. Long polling

SignalR tries to choose the "best" connection supported by server and client (you can also force it to use a specific transport).

That's the high level. If you want to see how each transport is implemented, you can look at the source code.

There's also client code for each transport: https://github.com/SignalR/SignalR/tree/master/src/Microsoft.AspNet.SignalR.Client.JS

If you're asking about how the long polling transport works in particular:

It sends an ajax request to the server that's waiting asynchronously for a signal to respond. When there is a signal or the request times out, it returns from the server and sends another request and the process continues. (I left some details out about how the client it keeps track of what it saw so it doesn't miss messages)

Hopefully that answers most of your question.

9
votes

@davidfowl has already answered the major portion. However, to provide some more details regarding the difference in behavior of transports, specifically between WebSocket and other transports; below are some points.

  • WebSocket is the only transport that establishes a true persistent, two-way connection between client and server. However, WebSocket is supported only by IIS 8 or above, and the latest versions of Internet Explorer, Google Chrome and Mozilla Firefox.
  • While Server Sent Events, Forever Frame and Long polling, all three follow a one-way communication, and are supported by most of the browsers.