i'm designing a client-server system via web broswser and i have this problem: I send the data to the client via JSON, then the javascript view shows the stuff. Then the user takes actions and commands are sent back to the server. Usual stuff, the problem is what if the view's data is corrupted or old? One solution i thought is making a hash via SHA1 or some other algorithm of the full objects in the client's model, but JSON data is not ordered, also this adds delays to the system. How do google docs manages this kind of thing? Also, sending ALL THE DATA, every time the model is updated doesn't make sense, but how do i keep consistency by only sending changes? I think the whole problem is just one.

  • You should never trust user input, so whatever happens you'll have to re-validate it. How much data are we talking about? What's the size of request when sending "all data" ? – CodeART Apr 29 '12 at 9:24
  • What i mean is that i don't want to send all the View's data with each request, it will be very resource intensive and slow, much more than sending just what's needed. – alfa64 Apr 29 '12 at 18:04

In most cases, I would probably do it like this:

  • Store an initial state plus all changes made since that state (all timestamped).
  • Poll only for changes since the last timestamp retrieved.
  • If someone posts an update, send the last-seen timestamp with it. If there have been updates since then, deal with that accordingly. (See below)
  • Periodically, calculate the state of the data at a given time.
  • When you load the state fresh, only load the last-calculated state plus changes made since that.

When I say "deal with that accordingly," I mean that it very much depends on the case. If I can safely make the second update and send the missing update back to the client, I will do. But sometimes you will want to inform the user of a double update and allow them to make the decision what to do next.

  • Your approach seems good, but there's a problem, what if the client's data is wrong? Not just the last update but something else. – alfa64 Apr 29 '12 at 7:08
  • @alfa64: What situation are you envisioning? Can you be more specific? – pdr Apr 29 '12 at 10:40
  • Example: A list of items gets one item removed or duplicated but only locally, and then the user sends a request for deletion. I don't want to inform the user that there's nothing to delete since he/she sees it clearly, but the problem is that the UI is corrupted. – alfa64 Apr 29 '12 at 18:02
  • @alfa64: If a change happens only locally then there must have been a failure to communicate with the server. That is, the server must never have confirmed a change. In that situation, the user of that client should be informed, or their change must go second in the queue. – pdr Apr 29 '12 at 20:19
  • Yes, but what method are you suggesting to check if there was a problem? – alfa64 Apr 29 '12 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.