Chapter 1 ■ IntroduCtIon to the real-tIme Web and aSp.net SIgnalr
12
Transport Options
As discussed in the previous sections, ASP.NET SignalR relies on transport layers to make communication possible
between client(s) and server(s). These four transport layers are categorized in two groups: Comet approaches and
modern HTML5 approaches. Each transport option is discussed in turn, followed by a discussion of how SignalR
implements these transport layers.
Long Polling
This famous Comet approach is one of the most common ways to achieve real–time web development in today’s
Internet. It relies on using JavaScript (or other technologies and techniques) to make a lightweight HTTP connection
to a server and keep it open for a period of time (e.g., 30 seconds). In this interval, if new data becomes available on
the server, it puts the data in the currently open HTTP connection and then closes it. The client receives this data and
opens a new long poll connection immediately. If there is no data in that period of time, the client establishes a new
long poll connection and continues this as long as the page is open and active in a web browser.
Long polling follows this simple model and reduces the overhead of opening several connections to servers that
could be introduced by an interval polling approach. Interval polling is the traditional approach in which the server
is checked at regular intervals (e.g., every 10 seconds) for new content. This approach is not efficient; opening and
closing HTTP connections to web servers create overhead. Long polling tries to reduce this overhead.
Forever Frame
Another Comet approach that is less common than long polling is Forever Frame (also known as hidden iframe),
which is an approach specific to Internet Explorer. In this approach, a hidden iframe element is attached to each web
page that is kept open for the whole duration of the request (the period of time that a user stays on the page). As data
becomes available on the server, it is filled with JavaScript codes in a stacked manner, and because browsers execute
these scripts in order, they can provide the desired behavior needed.
Server-Sent Event
This approach is a modern HTML5 transport that enables web browsers to receive events from servers through an
HTTP connection.
The main limitation of server-sent events (like any other approach based on HTML5) is browser support.
Although the most recent versions of common browsers support server-sent events, Internet Explorer does not
support them, which leaves a big gap for those interested in using these events for real–time web development.
WebSockets
The other HTML5 approach that has better browser support is WebSockets, which provides two-way communication
channels over a TCP connection. WebSockets is not limited to HTTP, although it can also be used in that context.
The use of WebSockets is dependent on the support by the underlying operating system (among Microsoft server
technologies, only Windows Server 2012 supports them, although it is also available on Windows 8 and 8.1) and
the whole network infrastructure between the client and server. Of course, browser support is also needed to take
advantage of WebSockets, and recent versions of most common web browsers support it.