A Redis Emitter implementation for PHP >=8.3. This package is not dependent on a specific Redis interface.
composer require turbo-timer/socket.io-redis-emitter-php
Setup:
$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);
Emitting events:
/** @var \TurboTimer\SocketIO\Emitter $emitter **/
$emitter->emit(
event: 'my_custom_event',
data: ['a' => 1, 'b' => 'c'],
);
Most of the following functions allow you to exclude specific rooms.
Emit event to specific rooms
/** @var \TurboTimer\SocketIO\Emitter $emitter **/
$emitter->emit(
event: 'my_custom_event',
rooms: ['my_room']
);
Emit event to every room, except one
/** @var \TurboTimer\SocketIO\Emitter $emitter **/
$emitter->emit(
event: 'my_custom_event',
exceptRooms: ['my_room']
);
Make socket join a room
/** @var \TurboTimer\SocketIO\Emitter $emitter **/
$emitter->joinRooms(
roomsToJoin: ['my_new_room'],
rooms: ['socket_id_or_room'],
);
Make socket leave a room
/** @var \TurboTimer\SocketIO\Emitter $emitter **/
$emitter->leaveRooms(
roomsToLeave: ['my_old_room'],
rooms: ['socket_id_or_room'],
);
Disconnect sockets
/** @var \TurboTimer\SocketIO\Emitter $emitter **/
$emitter->disconnectSockets(
rooms: ['socket_id_or_room'],
);
Emit server-side messages
/** @var \TurboTimer\SocketIO\Emitter $emitter **/
$emitter->emitToServers(
data: ['server_time' => time()]
);