views:

828

answers:

4

Hello,

I'm wondering how it's implemented in Gmail, that every time you receive e-mail, the list of mails is automatically refreshed. It looks like the server is sending some kind of event to the browser, but how is it possible? Or maybe it’s simle: the browser ask the server for new messages every let’s say 2 seconds? But it would probably kill the performance…

Anyone have some ideas?

EDIT: OK, so if it's the simple answer, how do they manage performance? When I send an email from an other account to the gmail account the view is "refreshed" almost instantly. You were saying about a simple function that returns true / false, but it must have some logic (db connection or reads some files). How they manage it?


See also: How is GMail Chat able to make AJAX requests without client interaction?

+3  A: 

gmail is, in fact, polling the server for updates. Not as often as every two seconds, though. That would be madness. A bit of testing with Tamper Data makes it look like maybe every 20 seconds, though there seem to be multiple events going through that confuse it a bit.

Regarding your edit, I imagine they might have a last-activity timestamp on the account tracking in their database, with the client polling query retrieving that via Ajax and comparing with its last sync to determine whether it needs to do a full update.

chaos
+2  A: 

You have right with simple answer. Google Mail checking new messages on server via AJAX.

MicTech
+2  A: 

It must be some kind of ajax listener that get informations every X seconds.

I already set something like that for one of my projects. What I was doing is calling a function that was returning true or false. True if the page needed to be refreshed, false otherwise. Then if you have an update, you do another call to get the actual update. This way you don't have to refresh everything every time... but it's still intense on the server if you have a lot of users.

In other words and like chaos said, it's polling the server.

marcgg
+3  A: 

Dont know exactly which technoloy Gmail uses, but the concept is to open a channel - using reverse AJAX, comet or sprocket based techniques.

Think of it as the client requesting the server for data, but the server does not return for one minute unless it has new mail. Using this technique, the client can almost show the results in a real time manner and it does not have to poll every 2 secs. Makes sense?

Ryan Oberoi
wow, great! Googled it and it make perfect sense!
Adam
These technologies mostly use polling to *simulate* the existence of a channel. Actually keeping a TCP connection open for the server to send update data down to to the client with is called 'server push', and it is the hot new technology of 1994.
chaos
*simulate* is the best that a browser can do at present. Server push technology is possible if you are allowed to open ports from within the application on the client machine and listen on them. There is no *real* server push in the browser context. The methodologies I mentioned provide the mechanism to *simulate* it using available polling techniques. Also this is how Facebook and other IM providers update your window when your friend types in something in a chat box.
Ryan Oberoi
That's not really correct, Ryan. There is such a thing as server push, but nobody uses it because it's spectacularly resource-wasteful on the server.
chaos