walkor / phpsocket.io

A server side alternative implementation of socket.io in PHP based on workerman.
2.3k stars 508 forks source link

Compatibility with clue/redis-react #277

Closed LoSunny closed 2 years ago

LoSunny commented 2 years ago

I have tried to follow the instructions listed in #227, however the code is no longer working. My composer.json

{
    "require": {
        "workerman/phpsocket.io": "^1.1",
        "clue/redis-react": "^2.6"
    }
}

Part of my index.php

<?php
require __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
use Workerman\Autoloader;
use PHPSocketIO\SocketIO;
use Clue\React\Redis\Factory;
use Clue\React\Redis\Client;
$io = new SocketIO(8080);
$io->on('connection', function ($socket) {
    global $factory;
    // var_dump($factory);
    $factory->createClient('127.0.0.1:6379')->then(function (Client $redis) use ($socket) {
        $redis->ping()->then(function () {
            echo 'Connected to redis: ' . PHP_EOL;
        }, function (Exception $e) {
            echo 'Unable to connect to redis: ' . $e->getMessage() . PHP_EOL;
        });
    });
});
$io->on('workerStart', function () {
    global $factory;
    $loop = Worker::getEventLoop();
    var_dump($loop);
    $factory = new Factory($loop); // Here is line 42
});
Worker::runAll();

Error

TypeError: Argument 1 passed to Clue\React\Redis\Factory::__construct() must be an instance of React\EventLoop\LoopInterface or null, instance of Workerman\Events\Select given, called in /home/sunnylo/index.php on line 42 and defined in /home/sunnylo/vendor/clue/redis-react/src/Factory.php:30
Stack trace:
#0 /home/sunnylo/index.php(42): Clue\React\Redis\Factory->__construct()
#1 /home/sunnylo/vendor/workerman/workerman/Worker.php(2434): {closure}()
#2 /home/sunnylo/vendor/workerman/workerman/Worker.php(1567): Workerman\Worker->run()
#3 /home/sunnylo/vendor/workerman/workerman/Worker.php(1409): Workerman\Worker::forkOneWorkerForLinux()
#4 /home/sunnylo/vendor/workerman/workerman/Worker.php(1383): Workerman\Worker::forkWorkersForLinux()
#5 /home/sunnylo/vendor/workerman/workerman/Worker.php(550): Workerman\Worker::forkWorkers()
#6 /home/sunnylo/index.php(45): Workerman\Worker::runAll()
#7 {main}
walkor commented 2 years ago

This feature is not supported now.

LoSunny commented 2 years ago

Sorry, I'm new to PHP and I'm not sure about what do you mean by "this feature". My goal is using the redis pub/sub feature, I can listen/send notification in my browser with the help of socket.io, and using PHP as my backend. If that is the case, what should I do or start my research?

EDIT If I want to use this feature, what version should I downgrade to?

walkor commented 2 years ago

If you want to use redis try https://github.com/walkor/redis.

Run command composer require workerman/redis to install it.

The codes like this .

require __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
use Workerman\Autoloader;
use PHPSocketIO\SocketIO;
use Clue\React\Redis\Factory;
use Clue\React\Redis\Client;
$io = new SocketIO(8080);
$io->on('connection', function ($socket) {

});
$io->on('workerStart', function () {
    global $redis;
    $redis = new Client('redis://127.0.0.1:6379');
    $redis->subscribe(['news', 'blog'], function ($channel, $message) {
        echo "$channel, $message"; // news, news content
    });
});
Worker::runAll();
LoSunny commented 2 years ago

Thank you, it works WHEN unused dependency are removed. 😆