How often can I have ajax ask for new data?
I am working on designing a collaborative system where multiple users can edit the same document, much like googledocs, and I guess 'search suggestions', but in no way as advanced.
My question is this, will my design work? I know enough php/ajax to design this, using jQuery for the 开发者_如何学Pythonajax part. But I have never made such a system, and therefore the design may have huge flaws.
The editable part of the webpage is a table, where certain cells can be edited (again much like google docs spreadsheets).
In theory this will work, whenever a tables cell is changed, the changes are stored in a history, and every 1000ms (after the last post returned successfully) I post the cell changes to a server
Simultaneously, another ajax function polls the server (also every 1000ms after the last call return successfull) and asks for a list of changes done to all the cells.
Is this the simple way of how google does it?
This is a debatable subject and most solutions depend on requirements and use cases rather than what's "correct". One of the more complex solutions is to use Semaphore:
http://en.wikipedia.org/wiki/Semaphore_(programming)
The railroad semaphore flag: http://en.wikipedia.org/wiki/Railway_semaphore_signal
That said, in your case, I suspect the "last value written" should be used. Simply have every edit queued on the server and then applied to it's cell. Obviously, there will be cases where data is inappropriately overwritten, but that's where human's come into play.
You could choose to detect when two users have focused and are editing the same cell -- when this occurs, send a notice to each use that their changes may conflict with another user. The least-aggressive user will back off and go do something else. Obviously there are more cases such as mass-edits but again, this all depends on your requirements.
Well you can cut down those two queries to one by posting the list of changes and receiving a list of changes all in one hit. You can optimise it by only checking when the user is actually using the page (e.g. if you've received a mouseover event in the last 30 seconds). And you might decide hitting the server every second isn't necessary; maybe 10 seconds will do. After all, it's realistic it might take longer than 1s to complete one call if your network is being slow.
After that, it's a question of what kind of scalability you need out of your server and only you can answer that. The more you poll, the fewer users your web server can support.
Google Docs (I think) uses a constantly-open push connection for receiving updates rather than polling, but that kind of architecture is something that shouldn't be taken on lightly. And of course, they have many thousands of servers.
精彩评论