Explain what is Long Polling?

Technology CommunityCategory: WebSocketsExplain what is Long Polling?
VietMX Staff asked 3 years ago

Using Long polling (or Ajax Long Polling) method the server receives a request, but doesn’t respond to it until it gets new data from another request.

Long polling is more efficient than pinging the server repeatedly since it saves the hassle of parsing request headers, querying for new data, and sending often-empty responses.

However

  • the server must now keep track of multiple requests and their order,
  • requests can time out, and new requests need to be issued periodically.

Steps:

  1. A client requests a webpage from a server using regular HTTP (see HTTP above).
  2. The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server.
  3. The server does not immediately respond with the requested information but waits until there’s new information available.
  4. When there’s new information available, the server responds with the new information.
  5. The client receives the new information and immediately sends another request to the server, re-starting the process.