Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are subsequent AJAX requests guaranteed to be received in order?

Tags:

jquery

http

tcp

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?

like image 761
lmirosevic Avatar asked Jul 03 '12 10:07

lmirosevic


1 Answers

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.

like image 184
Samuel Rossille Avatar answered Sep 28 '22 15:09

Samuel Rossille