168

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?

Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322
  • 16
    @dfowler did an interview with Scott Hansleman recently that was very informative http://hanselminutes.com/291/damian-edwards-explains-the-realtime-web-for-aspnet-with-signalr. – AlignedDev Apr 02 '12 at 20:31
  • 1
    Good article about signal r architecture and how it works http://lostechies.com/erichexter/2012/11/05/architecture-for-a-signalr-synchronized-webpage-application-part-2/ http://www.asp.net/signalr/overview/getting-started/introduction-to-signalr – Mahesh Jul 29 '13 at 03:01
  • I know its a long time ago but http://www.microsoftvirtualacademy.com/Content/ViewContent.aspx?et=4099&m=4097&ct=19609 – lordkain Nov 07 '13 at 07:24
  • 1
    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. http://vimeo.com/68383353 – Johan B Apr 28 '14 at 07:17

2 Answers2

253

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.

splattne
  • 102,760
  • 52
  • 202
  • 249
davidfowl
  • 37,120
  • 7
  • 93
  • 103
  • 3
    can you tell me how many connections it is able to support at one time? – Farhad-Taran Jul 30 '12 at 09:34
  • 1
    Regarding how many connections the Signalr is going to support depends on the request limit of the IIS. It can be increased using config entries or shell scripts. Usually Signalr caches 1000 connectiosn in memory. – Thanigainathan Jan 28 '14 at 21:24
  • 2
    As already mentioned, the limit rally is at the server level. Damnien Edwards (co-creator of SignalR) has got 150,000 connections from a single 10GB server though: https://twitter.com/DamianEdwards/status/486642486350061568 – LDJ Sep 01 '14 at 08:11
9

@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.
shivam
  • 161
  • 1
  • 11