+1 vote
by (220 points)

Hi,
We are using Rebex for the Websoket connection and it works as expected, but I have a question regarding web socket PING after a certain time interval.
I did not find much in the Rebex forums/documents about how we will receive PING from the server and send back PONG in return.

Currently, I am calling Poll() method after specified time intervals to keep my connection alive. In order to implement poll(), I have started Timer(), so that the event will be triggered after EVERY few seconds and then we can call Poll().
As you have mentioned in one of your pages "WebSocket: Enhanced keep-alive logic. Pings are now send from Poll method as well if appropriate."

My question is: Not sure if what I have done is the correct way of sending Pings, Could you please share a better way to achieve this using Rebex Library? Or are there any callback methods available(Rebex)to receive PINGs from the server or to perform PING/PONG operations?

Looking forward to your Support.

Thanks,

Applies to: Rebex WebSocket

1 Answer

+1 vote
by (73.5k points)
selected by
 
Best answer

Please, check out my answer here. It describes how keep-alive logic works.

In short: The PING/PONG is done automatically on background when you are waiting for data to be received from the server (inside Receive() or Poll() methods). The rate of PING/PONG is determined by the WebSocketClient.Options.KeepAliveInterval.

If you want to process PING/PONG on demand for some reason, using Poll() is the best way. But it is typically not needed to do it on demand, since PING/PONG is done automatically on background during Receive(), unless it was disabled by setting the KeepAliveInterval to zero or a negative value.

by (220 points)
Hi Lukas,
Thanks for the quick reply,
PING/PONG is done automatically in the background, but the use case is a little different here.
I am connected to the server but not sending or receiving anything for a few minutes(basically idle), then after 40 seconds my server shows a "WebSocket pong timeout" and "Disconnected".

[2023-09-20 16:10:57.640][541][ActivHandler][I] client connected
[2023-09-20 16:10:57.641][541][ActivHandler][D] Status is CONNECTED
[2023-09-20 16:12:16.925][ERROR][lodis-com][#1996269600][CConnectionBase.hpp:265][operator()]Websocket pong timeout
[2023-09-20 16:12:16.934][541][ActivHandler][T] path event: connected 64
[2023-09-20 16:12:16.936][541][ActivHandler][I] client disconnected


Though I have set KeepAliveInterval to (0,0,20). I want to ping the server every 20 or 30 seconds(in an idle state) so that it won't get disconnected.



Is it correct to use poll() with Timer? coz this is the only way I found to keep my connection alive?
by (73.5k points)
Yes, if you are not communicating with the server for few minutes, the best way to PING the server is to combine Timer + Poll(1).

However, please note that Poll() cannot be called at the same time when another Poll() or Receive() operation is in progress. You have to synchronize your idle timer with the main code so only one is running at the same time.
by (220 points)
Yes, I have synchronized it, I am disabling the timer when receive() is being called.
Thanks a lot for your Support.
...