PHP code example of cydrickn / socketio

1. Go to this page and download the library: Download cydrickn/socketio library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cydrickn / socketio example snippets




$server = new \Cydrickn\SocketIO\Server([
    'host' => '0.0.0.0',
    'port' => 8000,
    'mode' => SWOOLE_PROCESS,
    'settings' => [
        \Swoole\Constant::OPTION_WORKER_NUM => swoole_cpu_num() * 2,
        \Swoole\Constant::OPTION_ENABLE_STATIC_HANDLER => true,
    ]
]);

$server->on('Started', function (\Cydrickn\SocketIO\Server $server) {
    echo 'Websocket is now listening in ' . $server->getHost() . ':' . $server->getPort() . PHP_EOL;
});

$server->on('connection', function (\Cydrickn\SocketIO\Socket $socket) {
    // ...
});

$server->start();

$server->on('connection', function (\Cydrickn\SocketIO\Socket $socket) {
    $socket->emit('hello', 'world');
});

$socket->emit('hello', 'world', 1, 2, 3, 'more');

// BAD
$socket->emit('hi', json_encode(['name' => 'Juan']));

// GOOD
$socket->emit('hi', ['name' => 'Juan']);

$socket->broadcast()->emit('hi', ['name' => 'Juan']);

$socket->toAll()->emit('hi', ['name' => 'Juan']);

$socket->emit('hi', function () {
    //...
});

// timeout for 5 seconds
$socket->timeout(5000)->emit('hi', function (bool $err) {
    if ($err) {
        // the other side did not acknowledge the event in the given delay
    }
});

// timeout for 5 seconds
$socket->timeout(5000, 'Juan')->emit('hi', function (bool $err, string $name) {
    var_dump($name); // if the other side did not acknowledge the event the $name will be 'Juan'
    if ($err) {
        // the other side did not acknowledge the event in the given delay
    }
});

$server->on('hello', function (\Cydrickn\SocketIO\Socket $socket, string $world) {
    // ...
});

$server->on('hi', function (\Cydrickn\SocketIO\Socket $socket, array $name) {
    // ...
});

$socket->join('room1');

$socket->to('room1')->emit('hi', ['name' => 'Juan']);

$socket->to('room1')->to('room2')->emit('hi', ['name' => 'Juan']);

$socket->leave('room1');

use Cydrickn\SocketIO\Message\Response;

$socket->on('private message', (\Cydrickn\SocketIO\Socket $socket, $anotherSocketId, $msg) => {
    $socket->($anotherSocketId, Response::TO_TYPE_SID).emit('private message', $socket->sid, $msg);
});

$server->on(function (\Cydrickn\SocketIO\Socket $socket, callable $next) {
    // ...
    $next();
});

$server->on(function (\Cydrickn\SocketIO\Socket $socket, callable $next) {
    // ...
    $next(new \Error('Something went wrong'));
});

$server->on(function (\Cydrickn\SocketIO\Socket $socket, \Swoole\Http\Response $response, callable $next) {
    // ...
}, true);

$socket->getSession();

$session = $server->getSessionStorage()->get('123456'); // This will automatically created once it does not exists
$socket->setSession($session);



class CustomeStorage implements SessionStorageInterface {
    // ...
}

$server = new \Cydrickn\SocketIO\Server([
    'host' => '0.0.0.0',
    'port' => 8000,
    'mode' => SWOOLE_PROCESS,
    'serve_http' => true,
    'settings' => [
        \Swoole\Constant::OPTION_WORKER_NUM => swoole_cpu_num() * 2,
        \Swoole\Constant::OPTION_ENABLE_STATIC_HANDLER => true,
        \Swoole\Constant::OPTION_DOCUMENT_ROOT => dirname(__DIR__).'/examples'
    ]
], sessionStorage: new CustomeStorage());
js
socket.on('hi', (callback) => {
    callback('hello');
});