Ratchet icon indicating copy to clipboard operation
Ratchet copied to clipboard

How to handle 'keepAlive' in own App

Open fx1234 opened this issue 3 years ago • 3 comments

Hi

Is there more documentation about 'enableKeepAlive'? I'm trying to understand how this works. I have 'keepAlive' enabled and I see ping/pong being sent and received.

in my app I have an array with all connected clients: $clients[$conn->resourceId] = $conn;

If a client disconnects and ping/pong fails, how do I know about it in my app so I can delete the connection from $clients?

fx1234 avatar May 01 '22 16:05 fx1234

It's a question I have too

Rafael-Abne avatar May 05 '23 01:05 Rafael-Abne

I'm having an issue where this is vital. I am prototyping a midi-usb device using a Teensy 4.1 microcontroller that connects into a Ratchet server over WS over ethernet. The problem that I am having is that when the usb cable is unplugged the device powers down immediately without a graceful TCP disconnection/termination, meaning the OS on the server running ratchet doesn't know the TCP connection is lost until it times out and of course there is not a graceful Websocket disconnection within PHP at this point. This makes heartbeat handling vital. It's been a long time since I've experimented with low-level TCP errors within PHP, and in my case the websocket heartbeat mechanism would suffice for handling hard TCP disconnects.

ClosetGeek-Git avatar Sep 09 '23 05:09 ClosetGeek-Git

The enableKeepAlive method in WsServer.php creates a timer that monitors connections for pings, however when a connection is no longer active this method does not raise an event higher within the API for users to handle the lost client. This is short sighted since most services will have resources assigned to each client.

https://github.com/ratchetphp/Ratchet/blob/5012dc954541b40c5599d286fd40653f5716a38f/src/Ratchet/WebSocket/WsServer.php#L210-L212

ClosetGeek-Git avatar Sep 11 '23 12:09 ClosetGeek-Git