Skip to content

Add examples to ease getting started #99

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

Merged
merged 1 commit into from
Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ $loop->addPeriodicTimer(5, function () {
$loop->run();
```

See also the [examples](examples).

## Usage

Typical applications use a single event loop which is created at the beginning
Expand Down
15 changes: 15 additions & 0 deletions examples/01-timers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

$loop = React\EventLoop\Factory::create();

$loop->addTimer(0.8, function () {
echo 'world!' . PHP_EOL;
});

$loop->addTimer(0.3, function () {
echo 'hello ';
});

$loop->run();
16 changes: 16 additions & 0 deletions examples/02-periodic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

$loop = React\EventLoop\Factory::create();

$timer = $loop->addPeriodicTimer(0.1, function () {
echo 'tick!' . PHP_EOL;
});

$loop->addTimer(1.0, function () use ($loop, $timer) {
$loop->cancelTimer($timer);
echo 'Done' . PHP_EOL;
});

$loop->run();
26 changes: 26 additions & 0 deletions examples/11-consume-stdin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use React\EventLoop\Factory;

require __DIR__ . '/../vendor/autoload.php';

if (stream_set_blocking(STDIN, false) !== true) {
fwrite(STDERR, 'ERROR: Unable to set STDIN non-blocking' . PHP_EOL);
exit(1);
}

$loop = Factory::create();

$loop->addReadStream(STDIN, function ($stream) use ($loop) {
$chunk = fread($stream, 64 * 1024);

// reading nothing means we reached EOF
if ($chunk === '') {
$loop->removeReadStream($stream);
return;
}

echo strlen($chunk) . ' bytes' . PHP_EOL;
});

$loop->run();
39 changes: 39 additions & 0 deletions examples/12-generate-yes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

// data can be given as first argument or defaults to "y"
$data = (isset($argv[1]) ? $argv[1] : 'y') . "\n";

// repeat data X times in order to fill around 200 KB
$data = str_repeat($data, round(200000 / strlen($data)));

$loop = React\EventLoop\Factory::create();

$stdout = STDOUT;
if (stream_set_blocking($stdout, false) !== true) {
fwrite(STDERR, 'ERROR: Unable to set STDOUT non-blocking' . PHP_EOL);
exit(1);
}

$loop->addWriteStream($stdout, function () use ($loop, $stdout, &$data) {
// try to write data
$r = fwrite($stdout, $data);

// nothing could be written despite being writable => closed
if ($r === 0) {
$loop->removeWriteStream($stdout);
fclose($stdout);
fwrite(STDERR, 'Stopped because STDOUT closed' . PHP_EOL);

return;
}

// implement a very simple ring buffer, unless everything has been written at once:
// everything written in this iteration will be appended for next iteration
if (isset($data[$r])) {
$data = substr($data, $r) . substr($data, 0, $r);
}
});

$loop->run();
30 changes: 30 additions & 0 deletions examples/21-http-server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

$loop = React\EventLoop\Factory::create();

$server = stream_socket_server('tcp://127.0.0.1:8080');
stream_set_blocking($server, 0);

$loop->addReadStream($server, function ($server) use ($loop) {
$conn = stream_socket_accept($server);
$data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
$loop->addWriteStream($conn, function ($conn) use (&$data, $loop) {
$written = fwrite($conn, $data);
if ($written === strlen($data)) {
fclose($conn);
$loop->removeStream($conn);
} else {
$data = substr($data, $written);
}
});
});

$loop->addPeriodicTimer(5, function () {
$memory = memory_get_usage() / 1024;
$formatted = number_format($memory, 3).'K';
echo "Current memory usage: {$formatted}\n";
});

$loop->run();