Alma Z
Alma Z

Reputation: 294

Do periodic job in React Socket Client

I want to do periodic job(check row in DB) inside socket client I tried to do like in this issue https://github.com/reactphp/socket/issues/167

My code

$loop = \React\EventLoop\Loop::get();
$connector = new \React\Socket\Connector();

$connector->connect($host)->then(function (\React\Socket\ConnectionInterface $connection) use($loop){

    $connection->on('connection', function () use($loop){
        $loop->addPeriodicTimer(2, function () {
            var_dump('test - periodic job');
          
        });
    });

    $connection->write('test write');

    $connection->on('data', function ($data) {
        var_dump('test - data event');
    });
});

So 'data event' works fine, but periodic job doesnt work

I tried second variant:

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


$loop->addPeriodicTimer(2, function () {
    echo 'periodic event';
});


$connector = new \React\Socket\Connector([], $loop);
$connector->connect($host)->then(function (\React\Socket\ConnectionInterface $connection){
    $connection->write($action);

    $connection->on('data', function ($data) {
    echo 'data event';
    });
});

But in this case only periodic event works and I cannot receive any "data event"

Upvotes: 0

Views: 45

Answers (1)

Simon Frings
Simon Frings

Reputation: 142

I already gave you an answer in https://github.com/orgs/reactphp/discussions/500 but I will also post my answer here:

It seems like you mix up client-side and server-side in your first example. In your first lines of code you create a new $connector in order to connect to $host afterwards. Once you're connected, you can use$connection to write something on the connection, you can find similar examples in our Connector documentation:

$connector->connect($uri)->then(function (React\Socket\ConnectionInterface $connection) {
    $connection->write('...');
    $connection->end();
}, function (Exception $e) {
    echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

What you're actually doing with $connection->on('connection', function () use($loop){...} is waiting for new connection events to happen, which if you think about it doesn't make much sense, because why would you wait for connection events on your already functional connection. This event is normally used on the server-side for new incoming connections from client, more on this in our connection event documentation.

The problem in your second example seems to be your $action variable. From what I can see, it seems like you never defined $action in your code, thus PHP will throw and error at this point. This all is happening inside a promise and promises are swallowing exceptions and errors in the current version. We're aware of this behavior and are working towards better error handling for this with ReactPHP v3, you can read more about this in our ReactPHP v3 wish list discussion.

Also worth noticing: you will only see your 'data event' once you receive data from the server side. This event will not be triggered by writing data on you client side to the server.

I hope this helps.

Upvotes: 1

Related Questions