0

I recently read about long-polling concept which can be used with http. I want to understand, if any http protocol standard itself supports long-polling concept, so that depending on how client configures the http connection, the server can behave in short-polling manner or long-polling manner, so in effect some clients can connect in short-polling mode and some clients can connect in long-polling mode.

Any sample code esp in java would be helpful.

BioLogic
  • 3
  • 2
  • It's up to the server's discretion when it wants to kill the connection. So long polling timeout in one application might be different from somewhere else. – oligofren Dec 12 '21 at 08:49
  • @oligofren : So how would client know that previous connection was killed and it needs to again establish the connection from scratch?. -- Thanks. – BioLogic Dec 12 '21 at 08:52
  • When you create a connection you usually also get event handlers associated with the connection. Typically an interface like `connection.on(eventName, eventHandler)` would let the client be informed when the socket closes/is reset, etc. So if the server decides to kill a connection (because it might be a zombie process) to cleanup/preserve resources, the client will know immediately. This is not at the application (HTTP) level, but lower. Look at network sockets: https://en.wikipedia.org/wiki/Network_socket – oligofren Dec 12 '21 at 09:22
  • Do note that a connection and a request is two different things. One connection could handle multiple requests over a long period of time. Long polling is not about the connection, but the HTTP requests. – oligofren Dec 12 '21 at 09:23

2 Answers2

1

Long polling means that the response to request is not sent immediately, only when it is available. HTTP as a protocol has no requirements about the time response generation should take.

The client decides how long it is willing to wait for server's response once it makes a HTTP request.

Similarly, the HTTP server has freedom to send response at any time after HTTP request has been sent.

For example:

Client sends an HTTP request, and waits for response for 5 minutes. If it doesn't receive a response within five minutes, the request times out. In long-polling scenario, client sends another HTTP request and so on.

Respectively on server side, the request processing loop reads the HTTP request and then waits for some entity to generate the response payload, which it then sends back to the client.

Clients cannot control how servers operate. There is no "long-polling requests" as a concrete concept on clients.

There are only HTTP requests, where server decides when to send the answer, and clients who decide how long they are willing to wait.

Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58
  • Thanks. IS there any API which client can use to know server parameters, for example: if server has implemented long-polling, for how much duration, the server holds the connection on average etc for client to fine tune its parameters for better performance. – BioLogic Dec 12 '21 at 08:57
  • I am not aware of such an API. In the use cases I have seen, it is simply an implicit contract between client and server. – Tero Kilkanen Dec 12 '21 at 09:00
  • Could you please take a look at my another question on this forum. I am new to this forum. Thanks a ton! – BioLogic Dec 12 '21 at 09:06
  • @BioLogic Remember to accept the answer, if it answers your question. – oligofren Dec 12 '21 at 09:19
0

There is no such thing as long polling in HTTP. Long polling is a clever (ab)use of HTTP, not a feature of HTTP.

Since HTTP does not know anything of long polling, there cannot be a standard way to check for support.

Jörg W Mittag
  • 2,108
  • 1
  • 18
  • 17