-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do I automatically retry when there is a connection error? #13
Comments
The client will automatically (re)connect when used to send or receive messages, or when connect() method is explicitly called. So it would be fairly easy to implement a retry strategy when setting up the client interaction. I will consider an internal retry strategy for future version. |
Oops, is there a good case for this? It seems to be all I can do right now:
This doesn't seem very convenient. |
An exception thrown by the constructor indicates setup error. This can never be restored by retry attempt, so keep the constructor outside the try/catch block. On the other hand, exceptions thrown by the send/receive methods might be fully recoverable and should probably not count towards a max limit. The |
I get it, but that doesn't seem convenient either, and I'm very much looking forward to the internal retry strategy. |
For now I'm going to
Is this a better way to use it? |
Given above snippet, I'm not sure what issue you're trying to solve. So instead I can give you an explanation what's going on and what errors might occur. 1$client = new Client($url); Initializes the client but does not connect to server
2$client->connect() Attempts to connect to server and perform handshake
3$client->receive(); Attempts to read message sent by server, will call
What to retry
|
I keep forgetting to add that my production environment is php 7.4, so I can only use version 1.7.0.
|
Reading your 3) I think there is a misunderstanding how websockets work. Unlike HTTP, which always perform a connect/send/receive/disconnect series of operation (that could easily be retried on failure), a websocket connection will be kept open until the client or server explicitly close it. Once connected, you may perform any number of send and receive operations independently.
|
I understand it in general, thanks for the answer. |
In my case: |
Thank you for great package! We are waiting for automatic reconnection functionality. |
Hi @indigoram89 This library will automatically connect/reconnect when sending, receiving or starting the listener. However, subscribing to a websocket server typically means that the client send some initial messages for identification and/or configuration. So we can't provide a generic restart of a listening session - a client application need to handle the communication logic for the current service itself. So if above code works for your application, you should stick with it. Some notes though;
|
Thank you so much for you reply! About reconnection - I imagined a method like:
|
When building subscriber applications, I typically place those initial calls in the $client
->onConnect(function ($client, $connection) {
$client->text($setup);
})
->onText(function ($client, $connection, $message) {
// Act on incoming message
})
->onError(function ($client, $connection, $exception) {
// Evaluate error
$client->start(); // Restart listening session if stopped, which will reconnect if disconnected
})
->start(); |
Yes, but I told about automatic reconnection functionality in this package. |
@sirn-se Hello! Could you tell me how to stop process rightly? I do the following in Laravel:
And I get Phrity\Net\StreamException (Failed to select streams for reading) after I send SIGTERM to this process. |
That depends on what you're attempting to do
So you should only call one of these methods at any given point. |
@sirn-se Thank you for your answer! It is useful information, but I try to understand why I get error (Failed to select streams for reading) after I call any of these methods? |
This error occurs when listening to a stream (as per So it appears you have a race condition in your code. Can not tell why based on your code snippet, I suggest you use the log function to find out exactly what's going on when you run your code. |
@sirn-se Hello! How can I prevent send message from server for current or some connection? |
@e-sau You can call the If you're inside one of the message listener methods you will get current Connection as second argument. If you need to send to a specific collection of Connections outside the listeners, you need to collect them in your code and send the same Message on each Connection in list. |
Describe your issue
How to observe the link status after creating the first
new WebSocket\Client
. Automatically try to reconnect after a delay of a few seconds when there is a link error, and stop the connection when a certain threshold is exceeded? (Similar tocurl
'sCURLOPT_RETRY
)The text was updated successfully, but these errors were encountered: