Does HTTP long polling support heartbeat message?

1.6k views Asked by At

I am using HTTP long polling for pushing server events to a client.

On the client side, I send a long polling request to the server and block there waiting for a event from the server.

On the server side, we used the cometd framework (I am on the client side, do not really know much about the server side).

The problem is, after sometime, the connection is broken and the client can not detect this, so it blocks there forever. We are trying to implement some kind of heartbeat message, which will be sent every N minutes to keep the connection active. But this does not seem to work.

My question is: does HTTP long polling support heartbeat messages? As far as I understand, HTTP long polling only allows the server to send one event and will close the connection immediately thereafter. The client must reconnect and send a new request in order to receive the next event. Is it possible that the server sends heartbeat messages every N minutes while still keep the connection open until a real server event happens?

1

There are 1 answers

2
sbordet On

If you use the CometD framework, then it takes care of notifying the application (both on client and on server) about when the connection is broken, and it does send heartbeat messages.

What you call "HTTP long polling" is just a normal HTTP request, so in itself does not support heartbeat messages. You can use HTTP long polling requests to implement heartbeat messages, and this is what CometD does for you under the covers.

In CometD, the response to a HTTP long poll request may deliver multiple messages, and the connection will not be closed afterwards. The client will send another HTTP long poll request without the need to reconnect, possibly reusing the previous connection.

CometD offers to your application a higher level API that is independent from the transport, so you can use WebSocket rather than HTTP, which is way more efficient, without changing a single line in your application.

You need to use the CometD libraries both on client (javascript and java) and on server, and everything will just work.