Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Synchronous nature

I have read that HTTP is a synchronous protocol. Client sends a request and wait for a response. Client has wait for the first response before sending the next request. Ajax uses HTTP protocol but is asynchronous in contrast. I also read that Synchronous request blocks the client until operation complete from here. I am confused and my quesetion are:

  1. what is definition of synchronous when talking about HTTP Protocol?
  2. Does synchronous associated with blocking?
like image 581
HKIT Avatar asked Mar 05 '23 11:03

HKIT


1 Answers

HTTP as a protocol is synchronous. You send a request, you wait for a response. As opposed to other protocols where you can send data in rapid succession over the same connection without waiting for a response to your previous data. Note that HTTP/2 is more along those lines actually.

Having said that, you can send multiple independent HTTP requests in parallel over separate connections. There's no "global" lock for HTTP requests, it's just a single HTTP request/response per open connection. (And again, HTTP/2 remedies that limit.)

Now, from the point of view of a Javascript application, an HTTP request is asynchronous. Meaning, Javascript will send the HTTP request to the server, and its response will arrive sometime later. In the meantime, Javascript can continue to work on other things, and when the HTTP response comes in, it will continue working on that. That is asynchronous Javascript execution. Javascript could opt to wait until the HTTP response comes back, blocking everything else in the meantime; but that is pretty bad, since an HTTP response can take a relative eternity compared to all the other things you could get done in the meantime (like keeping the UI responsive).

like image 53
deceze Avatar answered Mar 11 '23 16:03

deceze