JR Galia
JR Galia

Reputation: 17269

Online Users in an AppEngine Website

Users can use their Google, Facebook or Twitter account to login to a site running in Google App Engine. User's session was already implemented and the three authentication providers (Google, Facebook, Twitter) already work.

My next task is to check if a user is online when it's profile is viewed. How to implement this one? If you have some ideas, please let me know.

In addition, when a user login to the site, I saved it's email to session something like below:

session.put("email", email);

Another thing to consider is when the user's session timed out. Can we do something if a user's session automatically timed out (not using the log-out menu/button provided in the site) by having no interaction on the site for a long period of time.

The framework used is Struts2, running in Google App Engine.

Thanks.

Upvotes: 0

Views: 173

Answers (1)

Nick Johnson
Nick Johnson

Reputation: 101149

Since HTTP is request-based, not session-based, there's no way to know if a user is 'online' or not - simply put, HTTP has no concept of 'online' users.

The overly complex and high overhead way to do this is to use something like the Channel API to notify you when a user leaves a page (or your site), but this is a lot of overhead for a simple feature. You could also have the user's browser ping you on a regular basis when they have a page open, but again, this causes a lot of overhead and extra traffic.

A more common way is to store a record against the user in the datastore (or just in memcache, if it's not terribly significant if a user is erroneously marked as offline), recording when they last loaded a page, and update that whenever they fetch a page from your site. Then, you show the user as 'online' if they retrieved a page within some reasonable time interval (say, half an hour), and 'offline' if not.

Upvotes: 1

Related Questions