How to do live updating similar to Google Docs?

2020-05-12 04:53发布

I want to do something very similar to Google Doc's live updating - where all users can "immediately" see the actions of the other users in the doc.

To achieve this, my ideas so far:

  • Continuous AJAX requests being done in the background (this would seem performance-intensive)?
  • Surely there's not a way for the server to push notifications to all its clients and update them accordingly?
  • AJAX requests every X seconds with a buffer/time-lapse of actions to be accomplished in those X seconds (simulating a real-time effect)?

I would like to know others experience in trying to achieve this effect. What is the best way to do this?

All help is appreciated.

NOTE: I'm not specifically looking for a real-time document editing solution. I'm looking for a solution to the same concept of what Google does with their Docs. I will actually be using that solution in a slightly different manner.

11条回答
等我变得足够好
2楼-- · 2020-05-12 05:27

If you don't want to use Web Sockets because they're not widely supported, you'll want to look up Comet. That's how Google Docs probably does it.

查看更多
可以哭但决不认输i
3楼-- · 2020-05-12 05:28

EtherPad has been open-sourced, if you're looking for a realtime collaborative rich text editor.

查看更多
Lonely孤独者°
4楼-- · 2020-05-12 05:32

If your users are only using modern browsers than i would try out the websocket standard coming with HTML 5. More and more browser will support it in the future and companies like Google and Apple are working on this. Here a getting started tutorial: http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/

查看更多
啃猪蹄的小仙女
5楼-- · 2020-05-12 05:33

Check out google mobwrite. It's a drop-in library that enables collaborative editing of html forms via operational transformation.

Getting events pushed back from the server is the easy part, there are many ways to do it. Ensuring that the state is consistent across all clients, that's the hard part. That's where the operational transformation algorithm comes in.

查看更多
神经病院院长
6楼-- · 2020-05-12 05:38

A Ajax approach is one way to go. You could implement it like the chat applications. The actual way will depend on the data being view. In short

  1. Create a session. It could house the users sharing the doc (eg excel file)
  2. When user A make a change (eg. update cell A5), use Ajax to send the changes to the server. The change can be stored with the date of arrival or some index value.
  3. A background Ajax call is fired by the page from time to time. As part of the request, the last date of access is passed as well.
  4. Upon receiving the request, you simply serve all the changes made from the last time or index. You could include the new date or index value as part of the response so that it can be use for the next request.
  5. When you are sure that the top X elements will not be access, you could remove them

Whether it will be performance intensive or not will depend largely on how to structure everything.

Your other option is web sockets. Haven't used it personally but if you have control over what browser your users will use, you could give it a shot. It allows the server to push data to the browser. Some Links: Web Sockets JS and Web Sockets in Firefox

查看更多
来,给爷笑一个
7楼-- · 2020-05-12 05:41

I recommend using a Comet framework like Atmosphere. It will automatically choose a transport mechanism for your messages, which can be websockets if you're lucky (but as this is abstracted away, you do not have to worry). Anyway it is great value if you do not have to deal with individual requests / responses and all the error sources and browser bugs that you will encounter. I've been there. There be dragons. :-)

查看更多
登录 后发表回答