Skip to content

A Redis Emitter implementation for Socket.IO version 4 and up. Written in modern PHP. Independent of your Redis implementation.

License

Notifications You must be signed in to change notification settings

Turbo-Timer/socket.io-redis-emitter-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Socket.IO Redis Emitter in PHP

A Redis Emitter implementation for PHP >=8.3. This package is not dependent on a specific Redis interface.

Read more.

Installation

composer require turbo-timer/socket.io-redis-emitter-php

Usage

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'],
);

Examples

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()]
);

About

A Redis Emitter implementation for Socket.IO version 4 and up. Written in modern PHP. Independent of your Redis implementation.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages