views:

203

answers:

2

What is the best way to implement server push for more than one thing.

Lets say I want to just update user status, so I can periodicaly poll the server for status in like 1000ms and update the page.

The other way I can found is that server waits for like 30 sec, while it checks if there was any change and if one is found, server push response back to client, which update page and then make another poll.

But how would I implement this to check for like 10 things on website? For example if I want stackoverflow to refresh question votes when someone vote, but the only way how to do this that I can think of is

ask server for votes for each question -> server replies with votes of every question on the page

but how can I find out which question votes did change? I could send all current votes and then let server compare the values and reply only with those that did change, but I think that it would be very uneffective to do this while checking for like 30 values.

One example for all would be Facebook, where almost everything is refreshed by server push, but how can server find out what did change and what didnt?

Everything that I found (including my book "Ajax Patterns") explains only how to poll for one value, but I didn't find anything how to poll for many values at a time (like more than 10).

A: 

I'd suggest to tag your data with revision numbers at the server side, so the client knows what revision of the data it has; create a composite query whereby the client can send a set of revisions, and the server can respond to that list of revisions with any updated revisions it may have for them. In that way, the client is only making one server query to see if there are any updates; you're just batching together all the data the client is interested in into one query. This method also has the strength of allowing the client to change the set of data that it's interested in; if your implementation server-side is flexible enough, you can use the same implementation for all your dynamic data needs.

McWafflestix
+2  A: 

If you are prepared to use sessions, the easiest way would be to have a serial request ID, and let the server keep track of the latest information sent to the client on the session. Then the client asks if there is newer data than the last time it refreshed it.

e.g: Lets say the client sends ajax request #5001 after nothing has changed, the server might reply 'false'. Then someone posts a message, or there is a change of some kind, and so in request #5002 the server sends a list of changed elements (whatever these are). Then in request #5003 it would reply false again, because nothing has changed since request #5002.

A JSON client/server architecture would be perfect for this. It allows easy serialisation of object hierarchies/maps. I prefer jQuery for the client in javascript, and the server side is trivial to spit out.

jgubby
but how does the server know that something changed? the only way i can think of is to log all the changes and then reply all those that client doesn't have ... like if client has #5000, then two other people "add comment", so its #5002, and when first client polls for news, server has to get all changes since #5000, but isnt that uneffective?
Darth
Ah yes, there is a difference between a global version number and what im suggesting. A global version number would be kept by the server, and incremented when something (anything) changed. I think thats a bit messy, since you couldnt have subsets of data.On the user's session on the server, id store the timestamp of the last request according to the server, and its id number, as provided by the client. Then so long as the timestamp of the last update is stored somewhere on the server (let's say the timestamp of the new message), you can easily see from an incoming request what has changed.
jgubby