valeriitropin/socketio-redis-adapter

1.0.0 2017-12-18 20:30 UTC

This package is not auto-updated.

Last update: 2022-08-06 17:24:14 UTC


README

Partial port of socket.io redis adapter, async, allows to get clients/rooms and to manage them. The main goal of this project is possibility to communicate between php and socket.io applications. Built on top of ReactPHP components.

Installation

composer require valeriitropin/socketio-redis-adapter

How to use

use React\EventLoop\Factory as ReactFactory;
use ValeriiTropin\Socketio\RedisAdapter;
use Clue\React\Block;
use React\Promise;

$loop = ReactFactory::create();
$client = new RedisAdapter($loop);

$promise = $client->allRooms()->then(function ($rooms) use ($client) {
    $promises = [];
    foreach ($rooms as $room) {
        $promises[] = $client->clients([$room])->then(function ($clients) use ($room) {
            foreach ($clients as $client) {
                echo $room . ' ' . $client . "\n";
            }
        });
    }
    return Promise\all($promises);
})->otherwise(function ($error) {
    echo ($error->getMessage()) . "\n";
});

Block\await($promise, $loop);

API

RedisAdapter

__construct(React\EventLoop\LoopInterface $loop, $options = [])

$options:
  • prefix: pub/sub events prefix (socket.io)
  • requestsTimeout: timeout in milliseconds, float (5)
  • namespace: socket.io namespace (/)
  • pubClient: pub client
  • subClient: pub client
  • customHook: callable
  • uri: Redis connection string, see docs (localhost)

clients($rooms = []): React\Promise\Promise

Returns the list of client IDs connected to rooms across all nodes.

$adapter->clients($rooms)
    ->then(function ($clients) {
        var_dump($clients);
    })
    ->otherwise(function ($error) {
        echo ($error->getMessage()) . "\n";
    });

clientRooms($id): React\Promise\Promise

Returns the list of rooms the client with the given ID has joined (even on another node).

$adapter->clients($id)
    ->then(function ($rooms) {
        var_dump($rooms);
    })
    ->otherwise(function ($error) {
        echo ($error->getMessage()) . "\n";
    });

allRooms(): React\Promise\Promise

Returns the list of all rooms from all nodes.

$adapter->allRooms()
    ->then(function ($allRooms) {
        var_dump($allRooms);
    })
    ->otherwise(function ($error) {
        echo ($error->getMessage()) . "\n";
    });

remoteJoin($id, $room): React\Promise\Promise

$adapter->remoteJoin($id, $room)
    ->then(function () {})
    ->otherwise(function ($error) {
        echo ($error->getMessage()) . "\n";
    });

remoteLeave($id, $room): React\Promise\Promise

$adapter->remoteLeave($id, $room)
    ->then(function () {})
    ->otherwise(function ($error) {
        echo ($error->getMessage()) . "\n";
    });

remoteDisconnect($id, $close): React\Promise\Promise

$adapter->remoteDisconnect($id, $close)
    ->then(function () {})
    ->otherwise(function ($error) {
        echo ($error->getMessage()) . "\n";
    });

customRequest($data): React\Promise\Promise

Sends a request to every nodes, that will respond through the customHook method.

$adapter->customRequest($data)
    ->then(function ($replies) {})
    ->otherwise(function ($error) {
        echo ($error->getMessage()) . "\n";
    });

getLoop(): React\EventLoop\LoopInterface

Returns loop instance.

getPub(): Clue\React\Redis\StreamingClient

Returns pub client.

getSub(): Clue\React\Redis\StreamingClient

Returns sub client.

unsubscribeFromRequestChannel(): React\Promise\Promise

Unsubscribes the adapter instance from request channel.

Links