PHP code example of turbo-timer / socket.io-redis-emitter-php

1. Go to this page and download the library: Download turbo-timer/socket.io-redis-emitter-php 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/ */

    

turbo-timer / socket.io-redis-emitter-php example snippets


$redis = new Redis(...) // some Redis implementation.

$onPublish = function (string $channel, string $message) use ($redis): void {
    $redis->publish($channel, $message);
}

$emitter = new \TurboTimer\SocketIO\Emitter($onPublish);

/** @var \TurboTimer\SocketIO\Emitter $emitter **/
$emitter->emit(
    event: 'my_custom_event', 
    data: ['a' => 1, 'b' => 'c'],
);

/** @var \TurboTimer\SocketIO\Emitter $emitter **/
$emitter->emit(
    event: 'my_custom_event',
    rooms: ['my_room']
);

/** @var \TurboTimer\SocketIO\Emitter $emitter **/
$emitter->emit(
    event: 'my_custom_event',
    exceptRooms: ['my_room']
);

/** @var \TurboTimer\SocketIO\Emitter $emitter **/
$emitter->joinRooms(
    roomsToJoin: ['my_new_room'],
    rooms: ['socket_id_or_room'],
);

/** @var \TurboTimer\SocketIO\Emitter $emitter **/
$emitter->leaveRooms(
    roomsToLeave: ['my_old_room'],
    rooms: ['socket_id_or_room'],
);

/** @var \TurboTimer\SocketIO\Emitter $emitter **/
$emitter->disconnectSockets(
    rooms: ['socket_id_or_room'],
);

/** @var \TurboTimer\SocketIO\Emitter $emitter **/
$emitter->emitToServers(
    data: ['server_time' => time()]
);