array( 'local_cert' => isset($argv[2]) ? $argv[2] : (__DIR__ . '/localhost.pem') ) )); $server = new LimitingServer($server, null); $server->on('connection', function (ConnectionInterface $client) use ($server) { // whenever a new message comes in $client->on('data', function ($data) use ($client, $server) { // remove any non-word characters (just for the demo) $data = trim(preg_replace('/[^\w\d \.\,\-\!\?]/u', '', $data)); // ignore empty messages if ($data === '') { return; } // prefix with client IP and broadcast to all connected clients $data = trim(parse_url($client->getRemoteAddress(), PHP_URL_HOST), '[]') . ': ' . $data . PHP_EOL; foreach ($server->getConnections() as $connection) { $connection->write($data); } }); }); $server->on('error', 'printf'); echo 'Listening on ' . $server->getAddress() . PHP_EOL; $loop->run();