views:

56

answers:

3

In SO, when your question got answer. or you got new badge, event is triggered. Or when you got new PM in forum, it also lets you know by alerting message.

You see message that something happened with your account when you enter site for first time after this event.

How is this implemented? How do scripts know, that they have something new to show you?

A: 

The event happens when another person does something like vote on your answer or question. This is recorded on the server side.

When you log on to the site the fact that some events occured while you were away can be determined from looking up the database for these records.

While you are on the site it is possible for the page to periodically poll the server for changes. So its not really the server that the event is triggered on.

Vincent Ramdhanie
How is this recorded in the server side? For example, there are two new answers on my question. How this event is recorded?
Qiao
The questions and answers, comments and votes are all stored in a database of some sort. As you interact with the site your actions send data back to the server which is then stored in the database. As you browse the site the page can make periodic checks in the background using AJAX by sending a request to the server. The server side program will look up the database find changes and send the result back to the browser which then displays the notices etc.
Vincent Ramdhanie
+3  A: 

The programming technique you are looking for is called Comet. The link to wikipedia describes some implementations of that, but the easiest way is to make an XMLHttpRequest with a long timeout and only return data on change.

Residuum
Never knew about such things, thanks.
MInner
+1 because this is really the best solution, although with StackOverflow they're actually just polling.
jvenema
A: 

As an answer to your last comment
There are several tecniques to mark events as a 'new'. It can be another field in the database table, of boolean type: telling if event was shown to user or not. Or - easiest one - just a time of last user's visit being recorded in the session, and then al upcoming event's time being compared with it.

Col. Shrapnel