Skip to content

Instantly share code, notes, and snippets.

@asm89
Created August 8, 2013 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asm89/6183370 to your computer and use it in GitHub Desktop.
Save asm89/6183370 to your computer and use it in GitHub Desktop.
Tail a file using react/socket
<?php
require_once __DIR__ . '/vendor/autoload.php';
$file = __DIR__ . '/testfile';
$client= popen('tail -f ' . $file, 'w');
stream_set_blocking($client, 0);
$loop = React\EventLoop\Factory::create();
$conn = new React\Socket\Connection($client, $loop);
$conn->pipe(new React\Stream\Stream(STDOUT, $loop));
$loop->addPeriodicTimer(1, function () {
$memory = memory_get_usage() / 1024;
$formatted = number_format($memory, 3).'K';
echo "Current memory usage: {$formatted}\n";
});
$loop->run();
@Ferreiramg
Copy link

Hello.

It would be possible to use this with WsServer sending the output to websocket client?

$server = IoServer::factory(
                new HttpServer(new WsServer(new Console($loop))), 3000
); 

Thus websocket client would receive oos terminal real-time data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment