views:

18

answers:

2

Hey there,

I'm currently fooling around with AJAX. Right now, I created a Markdown previewer that updates on change of a textarea. (I guess you know that from somewhere... ;-) ).

Now, I'm trying to figure out, how to update a page upon an event is fired from another client. So to say an asynchron message board. A user writes something, an event is called, the post is written.

But on the other clients' pages, the new post is of course not yet available until they reload and get the updated list of posts from the database.

Now, how can you get this to work asynchronously? So in that moment when one client does something, the other clients all get to know that he did something?

I don't think this can be done completely in AJAX, but I also have no idea whatsoever how to implement this on server-side, as it would require a page reload to inform the other clients of the event.

I'm thinking of creating a file or database entry that hashes the current state of data. Whenever a client loads the page, he saves this hash. Then, a timer (does this exist in JavaScript?) checks for the hash every few seconds.

As soon as anyone changes the databse, the hash is recalculated. If the script sees that the hash was changed and is different to the one saved, it reloads the contents form the database and saves the new hash.

Is that even going to work?

+1  A: 

You could use polling. For example each client might be sending continuous AJAX requests to the server say each 30 seconds to see if new posts are available and if yes, show them:

setInterval(function() {
    // TODO: Send an AJAX request here to the server and fetch new posts.
    // if new posts are available update the DOM
}, 30 * 1000);

On the other hand when someone decides to write a new post you send an AJAX (or not AJAX) request to the server to store this post in the database.

Another less commonly used approach is the concept of Comet and the HTML 5 WebSockets implementation which allow the clients to be notified by the server of changes using push.

Darin Dimitrov
The first approach is pretty much what I had in mind if I understand it correctly? But this will query every 30 seconds - if no one makes a post for, say 10 minutes, I will have made 20 unnecessary queries...
ApoY2k
Minute queries. I don't think you'd need to worry too much about queries that have no value. Unless you were running thousands of queries a second, your server should easily handle it (depending on the server of course)
Alex
`if no one makes a post for, say 10 minutes, I will have made 20 unnecessary queries` - yes that's correct.
Darin Dimitrov
+2  A: 

Polling that is light as possible is really the best solution here. Even if you did use a socket or something... That's still basically a live connection waiting around that will likely have to poll itself (albeit in a more effecient way).

20 queries in 10 minutes that have responses like {"updates":false} shouldn't even be putting a dent in your application. I mean imagine someone browsing your site requesting 20 pages and the related images/scripts/etc (even if some caching is involved), there could easily be hundreds of requests requiring all sorts of wasted database queries to information to be displayed on the page they don't actually care about.

Bob