AJAX - The Complete Reference

(avery) #1

PART II


Chapter 6: Networking Considerations 257


it is up and then lets the user know that this is the case. To try to keep things lightweight, a
HEAD request rather than a traditional GET is used to “ping” the server and if the ping fails
the user is warned of the connection problem. It is important to be careful with the approach
and ensure that the request is not cached. It is clear that the incessant chatter from a
continuous poll could really clutter logs and waste available connections. However, for some
types of applications, such as a chatting system, this architecture is completely appropriate
given what can commonly be done in a Web browser today.

Client Availability


Inversely, the server may care about whether a client is available or not, and the same
problem of HTTP being connectionless ensues. As an example, consider traditional Web
application design: when a user reaches a site, a session is started. The session stores various
variables on the server that helps keep track of the user’s progress through the application.
This data is typically stored on the server and is referenced via a cookie, though there are
methods to track state without cookies. Now given such an approach, note that the server
only knows the user’s session is active when it sees the cookie that was issued to them
(return/return to the site) or remove “come back” all together. Without some page refresh or
other polling mechanism, the server really doesn’t know if the user has left or not so it
assumes that after a certain period of time of inactivity, often
30 minutes, the user is gone.
With Ajax, the same issue certainly could happen, though generally the client talks more
often to the server. A simple poll will show if users are around (http://ajaxref.com/ch6/
onlinestatus/pollusers.html). Regardless, it would be easy enough to get a sense of whether
the user is truly around simply by having an event handler sense user activity such as
mouse movement, scroll, or keystroke and flag the user as alive by sending a quick request
to the server. This is no different than the previous ping example, but the idea here is that
instead of the client asking if the server is alive, the client is telling the server it is alive by
making the request. If you think about it, you could do both with one request. The example
at http://ajaxref.com/ch6/onlinestatus/pollusersactivity.html shows the previous idea but
adds in a sense of client availability by looking for user activity.

Connection Rates


Even if a connection is up and not experiencing any form of flakiness, there is the possibility
that it is just plain slow. It would be a good idea to have a sense of exactly what the user’s
connection rate is before you decide on which data you will send them. It would be easy
enough to use JavaScript to set a timer to see how long a page takes to load. For example, at
the top of an HTML document, start a script timer:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Time Test</title>
<script type="text/javascript">
var gPageStartTime = (new Date()).getTime();
</script>
Free download pdf