HTML and CSS Reference
Long polling allows for an increased chance of instantaneous updates by being purpose-
fully slow in responding to a request. Instead of responding to a request immediately, the
server holds the connection open and waits until there's new data. As soon as the browser
receives the new data, another long poll is initiated.
The forever frame is a way of loading a web page slowly. The web page is loaded into a
hidden iframe element. Instead of delivering all the content as quickly as possible, the serv-
er sends a chunk at a time, as updates become available. In the main page, the iframe is
repeatedly scanned for new content.
Long polling approximates event-driven communication, but each request still requires a
full set of HTTP headers. The forever frame approach requires the headers to be sent only
once, but it still requires a lot of messing around in client code to check the contents of the
frame to see if they've been updated.
Server-sent events (SSE) work along the same lines as forever frames, except the browser
has a convenient API that's similar to the cross-document and channel-messaging APIs
you've already seen.
D.5. Server-side choices for event-driven web applications
The two new event-driven, client-server APIs in HTML5 are SSEs and WebSocket. Event-
driven, client-server approaches are ideal for applications that need to send small amounts
of data quickly to many clients; for example, stock-trading applications, where a few mil-
liseconds' delay in updating can have measurable financial impact, or network gaming
where delays (or lag) can make the game unplayable.
On a traditional web server, each connection is allocated a dedicated thread or process (a
flow of execution within a program), which suits the model where each connection is data-
intensive but short lived, such as when a web page and its linked resources are being down-
loaded. Event-driven communication expects the connections to be long lived but with re-
latively little activity. When each thread is assigned a connection, the maximum limit is
soon reached and the server becomes unable to respond to new requests.
This can be a problem for traditional web servers like Apache, which allocate a process or
thread per connection. The number of processes or threads that can be created is limited,