You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Correct, the receive() operation is blocking. So if you are waiting for input but the server doesn't write anything for 200 seconds the connection will close.
And thats the text part which makes problems:
Make a WebSocket connection using the obtained URL. Since the WebSocket is closed automatically after 300 seconds of inactivity, we recommend sending ping messages every 150 seconds to keep the connection open.
@mrsnut
I tried a similar code but this can't work because the server doesn't send data the most time.
So the while loop stops at "$data = $client->receive();" until the timeout closes the connection.
I found an easier method. When you set up the client, set a timeout for the connection (one of the options parameters), like this: $client = new \WebSocket\Client(getenv('WS_REALTIME_URL_SPOT'), ['timeout' => 20]);
After that, in the catch where all the websocket errors are caught, you will get a timeout after not receiving data, every 20 seconds. Use this timeout to send a heartbeat.
I found an easier method. When you set up the client, set a timeout for the connection (one of the options parameters), like this: $client = new \WebSocket\Client(getenv('WS_REALTIME_URL_SPOT'), ['timeout' => 20]);
After that, in the catch where all the websocket errors are caught, you will get a timeout after not receiving data, every 20 seconds. Use this timeout to send a heartbeat.
The use case for ping is to tell the server not to close a connection due to inactivity.
But example code is sent after a read timeout, which is not the same thing. Neither does is send a ping as specified by websocket protocol.
First of all, I've continued this repo as phrity/websocket (textual repo is abandoned).
With later versions you can simply add the PingInterval middleware to activate ping mechanism.
@sirn-se
Thanks.
I'm using version 3.5.0, and I didn't see that I was on the old github, sorry.
I followed the link and so I have to add $client->addMiddleware(new WebSocket\Middleware\PingInterval(interval: 10));
Last question: With the standard server settings, what 'interval' value should I use to avoid disconnections? Is 10 optimized?
Thanks for your help.
@Cadkey By default the interval will be the same as client timeout, which in turn defaults to 60 seconds.
But it depends on the server you're connecting to. The ping-interval should be somewhat lower than the server. If server documentation do not specify timeout you'll need to try some values, and if you get timeout then lower the value.
I got the error three times, and the script restarted each time.
Before each restart, my PHP decrements the value of PingInterval($x) until this exception no longer occurs.
My problem should be resolved soon with the correct value of $x.
WebSocket is so much more efficient than making multiple API requests.
Activity
[-]How go send a ping every x seconds?[/-][+]How to send a ping every x seconds?[/+]sirn-se commentedon Jun 26, 2020
Second parameter in send() is opcode. When sending
ping, the text message doesn't matter.chrispri commentedon Jun 26, 2020
Thanks for answer.
But this isn´t possible when I am receiving data without async? Am I wrong?
sirn-se commentedon Jun 26, 2020
Correct, the
receive()operation is blocking. So if you are waiting for input but the server doesn't write anything for 200 seconds the connection will close.chrispri commentedon Jun 26, 2020
Thats bad.
Do you have any ideas how i can get this work?
Here is the manual of Gardena/Husquvarna: https://developer.husqvarnagroup.cloud/apis/GARDENA+smart+system+API#/readme
And thats the text part which makes problems:
Make a WebSocket connection using the obtained URL. Since the WebSocket is closed automatically after 300 seconds of inactivity, we recommend sending ping messages every 150 seconds to keep the connection open.
sirn-se commentedon Jun 26, 2020
Only thing to do is to expect socket to close and have it re-connect. As that API allows http-requests as well it should not be a problem.
I will consider this use case for further development, though.
apeman76 commentedon May 10, 2021
What about this:
chrispri commentedon May 11, 2021
@mrsnut
I tried a similar code but this can't work because the server doesn't send data the most time.
So the while loop stops at "$data = $client->receive();" until the timeout closes the connection.
marcroberts commentedon Jun 14, 2021
I've just opened a PR that helps with this - #140
amazesecurity-dev commentedon Oct 9, 2022
I found an easier method. When you set up the client, set a timeout for the connection (one of the options parameters), like this:
$client = new \WebSocket\Client(getenv('WS_REALTIME_URL_SPOT'), ['timeout' => 20]);After that, in the catch where all the websocket errors are caught, you will get a timeout after not receiving data, every 20 seconds. Use this timeout to send a heartbeat.
Cadkey commentedon Jun 30, 2025
Hello, does this solution work?
sirn-se commentedon Jun 30, 2025
@Cadkey Short answer, no.
The use case for ping is to tell the server not to close a connection due to inactivity.
But example code is sent after a read timeout, which is not the same thing. Neither does is send a ping as specified by websocket protocol.
First of all, I've continued this repo as phrity/websocket (textual repo is abandoned).
With later versions you can simply add the PingInterval middleware to activate ping mechanism.
Cadkey commentedon Jun 30, 2025
@sirn-se
Thanks.
I'm using version 3.5.0, and I didn't see that I was on the old github, sorry.
I followed the link and so I have to add
$client->addMiddleware(new WebSocket\Middleware\PingInterval(interval: 10));Last question: With the standard server settings, what 'interval' value should I use to avoid disconnections? Is 10 optimized?
Thanks for your help.
sirn-se commentedon Jun 30, 2025
@Cadkey By default the interval will be the same as client timeout, which in turn defaults to 60 seconds.
But it depends on the server you're connecting to. The ping-interval should be somewhat lower than the server. If server documentation do not specify timeout you'll need to try some values, and if you get timeout then lower the value.
Cadkey commentedon Jul 1, 2025
@sirn-se
So I get this exception with the message
Connection operation timeoutwhich I capture with:
I got the error three times, and the script restarted each time.
Before each restart, my PHP decrements the value of PingInterval($x) until this exception no longer occurs.
My problem should be resolved soon with the correct value of $x.
WebSocket is so much more efficient than making multiple API requests.
Thanks for your help and for this websocket