Skip to content

Send periodic message to connected clients? #167

@carys-cc

Description

@carys-cc

Hi,

I'd like to send periodic messages every 120s can't figure out how to tie timers and sockets together. I tried writing something like this but it's definitely not working. New to ReactPHP and would appreciate any guidance.

$socket->on('connection', function ($conn) use ($loop) {
    $func = function () use ($conn) {$conn->close();};
    $timer = $loop->addPeriodicTimer(120, function() use(&$conn));
    $conn->on('data', function ($data) use ($loop, &$timer, $func) {
       // do other stuff like connect to mysql to pull some data
       $echo 'got here';
       $connection->write('this is a periodic message');
    });
});

$loop->run();

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions