views:

87

answers:

4

Is there a simple way to detect if the visitor is still viewing a page on a web page that does not require polling the server every X seconds?

If there was something less taxing on the server, that would be ideal.

+2  A: 

Use

Reverse AJAX

Reverse Ajax refers to an Ajax design pattern that uses long-lived HTTP connections to enable low-latency communication between a web server and a browser. Basically it is a way of sending data from client to server and a mechanism for pushing server data back to the browser.

Comet (programming)

In web development, Comet is a neologism to describe a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it. Comet is an umbrella term for multiple techniques for achieving this interaction. All these methods rely on features included by default in browsers, such as JavaScript, rather than on non-default plugins.

rahul
So you're saying that the server will immediately notice the lack of HTTP connection when the window is closed? Does the Reverse AJAX connection have to poll the server to keep the connection open? If so, it doesn't quite answer the question...
Paul McMillan
I have a LAMP stack. Is Reverse AJAX possible with LAMP? (assuming a large number of visitors)
chris
yes, Reverse AJAX is possible with LAMP.
Paul McMillan
Great! How can I set up a page to do Reverse AJAX or COMET? I was previously avoiding it because I thought Apache would be strained after a couple hundred visitors.
chris
Also, is Reverse Ajax the same thing as COMET?
chris
@chris: keep in mind that while it provides LESS strain than fully loading a page, COMET techniques are still going to use sockets and resources on your server by polling a keepalive signal regularly for every open page.
Paul McMillan
+1  A: 

You could use a javascript on_exit event to fire off an ajax event, open a popup window with a tracker, or do something else similar. These only work if your user clicks away from your page, rather than closing the window.

Paul McMillan
+4  A: 

The technical solution proposed in the other answers are fine, as far as they go, but I wonder how well they answer the underlying issue.

Right now, I have 4 Safari windows open, with a total of 17 tabs; and a Goggle Chrome one, with 4 tabs. Not sure exactly why I don't currently have any Firefox open (probably just forgot to restart it after upgrading to 3.5.4 recently) but normally there would be 2-3 windows there with another 8-10 tabs. Am I "still viewing" these 30 or so web pages -- at the same time...? Technically, I guess every programming-based solution you'll find will say that, why, yes, of course I am... does that make ANY sense to you?!

In real life, I may (perhaps, unless I'm doing something else again on my computer) be viewing ONE of those web pages at a given time -- maybe 2-3 if I have multiple or very large screens, but generally, 1 is the real-life-sensible answer. Which one? You're not going to find out without substantial Javascript magic -- if then.

It's such a hard problem, that adding the weird constraint of not having the magical Javascript periodically tell the server "I'm still open and it looks like the user is viewing me [[some interaction has happened within the last XX seconds, or...]]" vs "I'm open but I suspect I'm in the background or the user has gone away to have lunch leaving the browser open because there has been no interaction since YYY seconds ago" vs "I'm being navigated away from" (and no message means the whole browser crashed or something) -- adding that weird constraint basically ensures the problem is totally insoluble.

If you truly care about whether the user is actually viewing the page, a periodic every-few-second Ajax interaction from Javascript back to the server is a miniscule price to pay, and really the least of your problems. If you don't truly care, it's different, of course. Maybe tell us what actual problem you're really trying to solve...?!

Alex Martelli
this is for a social network - so it's important to know if the person is currently "available"
chris
@chris, so whether the webpage is still open in some long-forgotten tab in a deeply buried window is not really meaningful -- so you DO need JS magic-wannabe **and** therefore, inevitably, JS sending status updates to the server on a periodic or as-needed basis (and the server must notice how long ago it was since JS last sent it a "user's actually **active** on this page" status update).
Alex Martelli
+1  A: 

the simplest (and he most lightweight in terms of traffic) would be to track mousemove/keyup events on the page. Within, say, 1 minute since the mouse was last moved you can be sure the user is actually doing something on the site (and not for example went for lunch leaving your window open).

stereofrog