elephant.io icon indicating copy to clipboard operation
elephant.io copied to clipboard

Using Version2X but getting a Version1X error ?

Open ThibaultMagy opened this issue 3 years ago • 4 comments

Hi, I'm trying to emit an event from a php controller with elephant io by using version2X but I get a version1x error. Here is what my code looks like :

$socket = env('HTTP_PROTOCOL').'://'.env('URL_SOCKET').':'.env('PORT_SOCKET_MESSAGES');
                        $client = new Client(new Version2X($socket));
                        $client->initialize();
                        $client->emit('message', [
                            'message' => 'some message',
                        ]);
                        $client->close();

It works on my local machine (using wamp) but does not on the dev server.

It return me this error :

"name": "PHP Warning",
    "message": "file_get_contents(<my_url>/socket.io/?use_b64=0&amp;EIO=3&amp;transport=polling): failed to open stream: Connection refused",
    "code": 2,
    "type": "yii\\base\\ErrorException",
    "file": "<app_url>/vendor/wisembly/elephant.io/src/Engine/SocketIO/Version1X.php",
    "line": 177,
    "stack-trace": [
        "#0 [internal function]: yii\\base\\ErrorHandler->handleError(2, 'file_get_conten...', '<root_url>/...', 177, Array)",
        "#1 <app_url>/vendor/wisembly/elephant.io/src/Engine/SocketIO/Version1X.php(177): file_get_contents('<url>', false, Resource id #39)",
        "#2 <app_url>/vendor/wisembly/elephant.io/src/Engine/SocketIO/Version1X.php(48): ElephantIO\\Engine\\SocketIO\\Version1X->handshake()",
        "#3 <app_url>/application/vendor/wisembly/elephant.io/src/Client.php(60): ElephantIO\\Engine\\SocketIO\\Version1X->connect()",
        "#4 <app_url>/application/controllers/ApiController.php(382): ElephantIO\\Client->initialize()",
        "#5 [internal function]: app\\controllers\\ApiController->actionMessages('940833739', '701165609')",
        "#6 <app_url>/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)",
        "#7 <app_url>/vendor/yiisoft/yii2/base/Controller.php(178): yii\\base\\InlineAction->runWithParams(Array)",
        "#8<app_url>/vendor/yiisoft/yii2/base/Module.php(552): yii\\base\\Controller->runAction('messages', Array)",
        "#9<app_url>/vendor/yiisoft/yii2/web/Application.php(103): yii\\base\\Module->runAction('api/messages', Array)",
        "#10<app_url>/vendor/yiisoft/yii2/base/Application.php(384): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))",
        "#11 <root_url>/index.php(24): yii\\base\\Application->run()",
        "#12 {main}"
    ]

Do you know why it would use Version1X although I'm using Version2X and why this error happens ?

ThibaultMagy avatar Mar 31 '22 13:03 ThibaultMagy

Hum, IIRC the version 2 is an extension of the Version 1. So it overwrite a few things, and fallbacks on version 1. Now that I think about it, we should maybe change the naming of these classes or the structure.

Taluu avatar Apr 01 '22 12:04 Taluu

Thanks for the answer. I also read some people telling to use curl to avoid any problem with file_get_content() in Version1X, which seems to be an issue on many hosts. Would you recommand me to do so. I don't really like to change modules' code. Or is there an available fork already implementing the curl method ?

ThibaultMagy avatar Apr 01 '22 12:04 ThibaultMagy

As far as I know, there isn't a fork like that. You can make a PR and I'll squeeze a release with it if you'd like.

Taluu avatar Apr 01 '22 12:04 Taluu

Alright then, I'll do so. I have to finish some tasks and I'll make that PR, thanks again.

ThibaultMagy avatar Apr 01 '22 12:04 ThibaultMagy