I have an app where users can change a toggle value between true
and false
using a little toggle switch/button and the change is automatically sent to the server without requiring the user to explicitly press something like "save".
Now if the user presses the toggle switch twice in rapid succession, is it possible that the server will receive the packets out of order and thereby end up storing the wrong value? And by server I mean the app logic running on the server, not the OS networking stack.
I know TCP packets are numbered so the receiving OS sorts them before passing them to the application, and HTTP runs on top of TCP. However the part I'm curious about is under which circumstances HTTP creates/doesn't create a new TCP connection.
N.B. I'm using jQuery to send the AJAX requests and I'm assuming they are guaranteed to be sent in the same order as the calls to the jQuery library. Is this assumption valid?
There is no such guarantee in the http protocol. Even if some implementations may actually guarantee it, you should not rely on it.
You could do this: maintain a click count (or call it version number if you prefer), and send it to the server. On the server, store the last version number when you change the state, and use this number to check that you never overwrite a more recent value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With