Skip to content
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 to detect connection lost? #120

Closed
jupitern opened this issue Dec 13, 2021 · 2 comments
Closed

how to detect connection lost? #120

jupitern opened this issue Dec 13, 2021 · 2 comments
Labels

Comments

@jupitern
Copy link

jupitern commented Dec 13, 2021

Hi,
using the code bellow when I restart redis server the script terminates without calling close or error event.

$loop = \React\EventLoop\Loop::get();

$factory = new Factory($loop, new \React\Socket\Connector($loop));
$client = $factory->createLazyClient($conStr);

$client->on('message', function ($channel, $message) {
            $message = (object)unserialize(gzuncompress($message));

            echo date("Ymd H:i:s")." => ".$message->text.PHP_EOL;
});

$client->on('error', function (\Exception $e) {
    echo $e->getMessage().PHP_EOL;
});

$client->on('close', function () {
    echo "CLOSED".PHP_EOL;
});

$this->subscribeChannel($client, "pubsub_teste");

combining reactphp-redis with ratchet socket server adding the code bellow and using the same loop, when I restart redis server, the script continues to run undefinetly but no more messages are received.

$socket = new SocketServer("{$this->bindIp}:{$this->bindPort}", [], $loop);
$wsServer = new WsServer($this->chatServer);
$httpServer = new HttpServer($wsServer);
$server     = new IoServer($httpServer, $socket, $loop);

$server->run();

can you help understand what to change to be able to reconnect to redis server.
thanks

@clue clue added the question label Dec 20, 2021
@clue
Copy link
Owner

clue commented Dec 20, 2021

@jupitern Thanks for bringing this up, excellent question!

In the current version, the lazy client is responsible for re-establishing the connection when needed, so a close event will only be emitted when the connection is ultimately closed by calling close().

However, the lazy client will automatically emit the unsubscribe/punsubscribe event when the connection is lost. This can be used to automatically re-subscribe to any channels:

$redis->on('unsubscribe', function ($channel, $total) {
    // unsubscribed from given $channel
});

See also https://github.com/clue/reactphp-redis#pubsub

There are plans to revisit the API via #118. In the future, we will likely use a simpler RedisClient API that will simply emit open and close events throughout its lifetime.

I hope this helps! 👍 If so, consider supporting this project, e.g. by becoming a sponsor ❤️

@clue clue closed this as completed Dec 20, 2021
@clue
Copy link
Owner

clue commented Apr 28, 2023

See also #142 as a follow-up to detect dead socket connection 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants