AjaxPatterns
This is the original book draft version for the Periodic Refresh Ajax pattern, offered for historical purposes and as an anti-spam measure.
You're probably better off visiting the Live Wiki Version for Periodic Refresh instead, where you can make changes and see what others have written.

Periodic Refresh

Auto-Update, Polling, Sync, Synchronise, Sychronize, Real-Time

Developer Story

Devi's coding up a ticket sales website and for each event, she wants to keep the browser updated with the number of tickets remaining. Thus, she introduces a timer so that every thirty seconds, it calls a web service to pull down the latest sales stats.

Problem

How can the application keep users informed of changes occurring on the server?

Forces

  • The state of many web apps is inherently volatile. Changes can come from numerous sources, such as other users, external news and data, results of complex calculations, triggers based on the current time and date.

  • HTTP requests can only emerge from the client. When a state change occurs, there's no way for a server to open connections to interested clients.

  • One way to keep the browser updated is “HTTP Streaming”, but, as the Alternatives section for that pattern explains, it's not always ideal. In particular, it's not very scaleable.

Solution

The browser periodically issues an “XMLHttpRequest Call” to gain new information, e.g. one call every five seconds. The solution makes use of the browser's “Scheduling” capabilities to provide a means of keeping the user informed of latest changes.

In its simplest form, a loop can be established to run the refresh indefinitely, by continuously issuing “XMLHttpRequest Call”s:

    setInterval(callServer, REFRESH_PERIOD_MILLIS);

Here, the callServer function will invoke the server, having registered a callback function to get the new information. That callback function will be responsible for updating the DOM according to the server's latest report. Conventional web apps, even most of those using “XMLHttpRequest Call”s, operate under a paradigm of one-way communication: the client can initiate communication with the server, but not vice-versa. Periodic Refresh fakes a back-channel: it approximates a situation where the server pushes data to the client, so the server can effectively receive new information from the browser. Indeed, as some of the examples show, the server can also mediate between users in almost real-time. So Periodic Refresh can be used for peer-to-peer communication too.

But before we get too carried away with “Periodic Refresh”, it's important to note that it's a serious compromise, for two key reasons:

  • The period between refreshes would ideally be zero, meaning instant updates. But due to latency effects, the browser will always lag behind. Latency is particularly problematic when the user is interacting with a representation of volatile server-side data. For instance, a user might be editing an object without knowing that another user has already deleted it.

  • There is a significant cost attached to Periodic Refresh. Each request, no matter how tiny, demands resources at both ends, all the way down to operating system level. Traffic-wise, each request also entails some bandwidth cost, and that can add up if refreshes are occurring once every few seconds.

So a key design objective must be to increase the average refresh period and reduce the content per refresh, while maintaining a happy user experience. One optimisation is a timeout: the refreshes cease when the system detects the user is no longer active, according to a “Timeout” mechanism. You also want to make sure each refresh counts: it's wasteful to demand lots of updates if the network isn't capable of delivering them. Thus, the browser script can do some monitoring and dynamically adjust the period so it's at least long enough to cope with all incoming updates. Many of the ??? patterns can also be applied to Periodic Refresh - see the Decisions below.

Decisions

How long will the refresh period be?

The refresh period can differ widely, depending on usage context. Broadly speaking, we can identify three categories of activity level:

Real-Time Interaction (Milliseconds)

The user is actively interacting with the system, and their input relies on the server's output. For example: a chat user needs to see what others are saying, a trader needs to see current prices, a game player needs to see the state of the game. Here, the interval could be as low as a millisecond on a local machine or local network, or perhaps 20-100 milliseonds on a global network.

Active monitoring (Seconds)

The user relies on the server state for work outside the system. For example: a security officer watches sensor displays for suspicious activity, a manager occasionally watches the fluctuation of sales figures during a particular window of time. In some cases, timeliness may be critical, making sub-second responses desirable. In other cases, a few seconds is sufficient feedback.

Casual monitoring (Minutes)

Some applications are designed for the user to leave in a window or separate browser tab and view throughout the day. The information does not change often, and it's no drama if the user finds out a little later. Prime candidates here are portals and RSS aggregators. Refresh periods of 10 minutes or more are often acceptable for such content. A manual "Refresh Now" mechanism is worth including where the refresh period is longer than a few seconds. It can relate to the entire application or to specific components.

Sometimes, the best solution uses multiple “Periodic Refresh” cycles in parallel, each with a frequency reflecting the user's needs. An interactive wiki, for example, might update news headlines every 10 minutes, online statuses of other users every minute, and content being edited every second.

Real-World Examples

Lace Chat

Instant messaging (or "online chat") applications pre-date the web, and, unlike e-mail and other services, web interfaces have never quite worked out, partly due to the fact that people don't enjoy frequent full-page refreshes. Ajax makes it possible to avoid a complete refresh by pulling down messages with an “XMLHttpRequest Call”. Only the new messages need to be sent in each periodic response. In fact, there are several applications under development. Lace Chat, for example, is only a proof-of-concept, but provides good evidence that web-based chat is feasible. Every few seconds the messages update to show any new messages other users may have entered. When you post a message, it's also handled as an “XMLHttpRequest Call”.

Magnetic Poetry

Like a wiki, Magnetic Poetry involves a shared workspace (Figure 1.26, “Magnetic Poetry”). In this case, users move tiles through the space, and the application updates once a second to reflect the new space. As of version 1.7, the entire tile set is sent each time, but there's the potential to compress the information by only sending recent tile positions. This enables two users to work on the area simultaneously, and one can even see a tile being dragged by a different user, like a low-frequency animation.

Figure 1.26. Magnetic Poetry

Magnetic Poetry

Claude Hussnet's Portal

Portals, by definition, display various kinds of information. And often that information is of a dynamic nature, requiring periodic updates. Claude Hussenet's portal contains several portlets:

  • World news headlines (taken from Moreover and Yahoo! News). The server appears to generate these from an RSS feed.

  • New online articles appearing TheServerSide.com and DevX.com. Again, these are taken from RSS feeds.

  • Customised stock portal. The server appears to maintain a record of all current stock prices, and is capable of delivering those requested by the browser.

In each case, the information is volatile and needs to be periodically updated, as is typical for many portlet. Also characteristic of portal applications is the relatively long refresh period, 15 minutes in this case. Each portlet contains a manual refresh too, for immediate results.

Code Examples

Lace

Lace Chat handles Periodic Refreshes to show all users' chat messages.

The timer is set on startup to periodically call the get() function, which initiates the “XMLHttpRequest Call” for new messages:

    this.timerID = setInterval(function () { thisObj.get(true); }, interval);

get() performs a straightforward query of the server:

    this.httpGetObj.open('POST', this.url, true);
    this.httpGetObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');

    var thisObj = this;
    this.httpGetObj.onreadystatechange = function () { thisObj.handleGet(system); };
    this.httpGetObj.send(param);

If the server has changed at all, the entire contents are returned. How does Lace know if the server has changed? Each time the server responds, it generates a hash for the contents. And when the browser next calls for a refresh, it includes the last hash as a parameter to the call. The server only sends a full response if the current hash differs from that specified by the browser:

    [lib_lace.php]
    function getMessageHash() {
      ...
      return md5(implode(file(LACE_FILE)));
    }
 
    [lace.php]
    $_hash = getMessageHash(); if ($_hash == $hash) exit; // no change exit($_hash.'||||'.getFileContentsRaw());

Code Refactoring [AjaxPatterns Periodic Time]

The Basic Time Demo requires the user to manually update the time display by clicking a button. We'll perform a quick refactoring to re-fetch the time from the server every 5 seconds.

First, we'll create the function that initiates the server calls. Here, we already have two functions like that, one for each display. So, let's ensure we call both of those periodically:

    function requestBothTimes() { requestDefaultTime(); requestCustomTime(); }

Then, the Periodic Refresh simply a matter of running the request every five seconds:

    function onLoad() { ...  setInterval(requestBothTimes, 5000); ...  }

One more niceity: the function in setTimeout only begins to run after the initial delay period. So we're left with empty time displays for five seconds. To rectify that, requestBothTimes is also called on startup:

    function onLoad() { ...  requestBothTimes(); setInterval(requestBothTimes, 5000); ...  }

Now, the user sees the time almost as soon as the page is loaded.

Alternatives

One of the forces for this pattern is that HTTP connections tend to be short-lived. That's a tendency, not a requirement. As “HTTP Streaming” explains, there are some circumstances where it's actually feasible to leave the connection open. Streaming allows for a sequence of messages to be downloaded into the browser without the need for explicit polling. (Low-level polling still happens at the operating system and network levels, but that doesn't have to be handled by the web developer, and there's no overhead of starting and stopping an HTTP connection for each refresh, nor of starting and stopping the web service.)

Related Patterns

You can use “Distributed Events” to co-ordinate browser activity following a response.

A little work on performance issues can help make the system feel more responsive. Some of the performance optimisation patterns help:

“Fat Client”

Reduces the need for server processing by pushing functionality into the browser.

“Browser-Side Cache”

Reduces queries by retaining query data locally.

“Guesstimate”

Gives the user a sense of what's happening without actually performing any query.

“Submission Throttling” also involves a periodic call to the server. The emphasis there is on uploading browser-side changes, whereas the present pattern focuses on downloading server-side changes. The two may be combined to form a general-purpose "synchronise" operation, as long as the update frequency is sufficient in both directions. This would improve performance by reducing the amount of overall traffic. The Wiki Demo takes this approach.

It's inevitable that users will leave their browser pointing at websites they're not actually using. The rising popularity of tabbed browsing - now supported by all the major browsers - only exacerbates the problem. You don't want to keep refreshing the page for idle users, so use “Timeout” to detect if the user is still paying attention.

Metaphor

A movie is refreshed at sub-second intervals to provide the illusion of real-time activity.

Live Wiki Version for Periodic Refresh