Laravel-websockets: Error Exception thrown undefined property Ratchet\Server\IoConnection::$app

Created on 23 Mar 2020  路  5Comments  路  Source: beyondcode/laravel-websockets

So i am trying to use Custom WebSocket Handlers in this package . I followed all the steps as stated in the documentation https://docs.beyondco.de/laravel-websockets/1.0/advanced-usage/custom-websocket-handlers.html . After that using a javascript i tried to establish connection and then send message from it , connection establishes but i receive the strange error

Starting the WebSocket server on port 4222...
New connection opened for app key .
Exception ErrorException thrown: Undefined property: Ratchet\Server\IoConnection::$app
Unknown app id: exception ErrorException thrown: Undefined property: Ratchet\Server\IoConnection::$app.

my javascript code is

`
var conn = new WebSocket('ws://127.0.0.1:4222/my-socket');
conn.onopen = function(e) {
conn.send('Hello World!');
};

conn.onmessage = function(e) {
console.log(e.data)
}
`
please Look at it ..

Most helpful comment

Hello @shahabgohar, same issue here.

I fixed it by setting $app and $socketId manually :

<?php
// ...
use BeyondCode\LaravelWebSockets\Apps\App;
// ...
public function onOpen(ConnectionInterface $connection)
{
    $socketId = sprintf('%d.%d', random_int(1, 1000000000), random_int(1, 1000000000));
    $connection->socketId = $socketId;
    $connection->app = App::findById('YOUR_APP_ID');
    // ...

All 5 comments

Hello @shahabgohar, same issue here.

I fixed it by setting $app and $socketId manually :

<?php
// ...
use BeyondCode\LaravelWebSockets\Apps\App;
// ...
public function onOpen(ConnectionInterface $connection)
{
    $socketId = sprintf('%d.%d', random_int(1, 1000000000), random_int(1, 1000000000));
    $connection->socketId = $socketId;
    $connection->app = App::findById('YOUR_APP_ID');
    // ...

Had the same issue, should be documented in the custom handler section and not just a super hard to find closed issue

@Tofandel agreed !!
Got answer after a week but by then i switched to swoole
It was way easier to implement

@maximerenou hi
I have added your this code answer in my application

<?php
// ...
use BeyondCode\LaravelWebSockets\Apps\App;
// ...
public function onOpen(ConnectionInterface $connection)
{
    $socketId = sprintf('%d.%d', random_int(1, 1000000000), random_int(1, 1000000000));
    $connection->socketId = $socketId;
    $connection->app = App::findById('YOUR_APP_ID');
    // ...

but it is still showing error this
New connection opened for app key your-pusher-key. Connection id 441865230.936318383 closed. ExceptionErrorExceptionthrown:Trying to get property 'id' of non-object``

please share how can i resolve it??

@faizan-glowlogix If you didn't declare an app because you're not using as a pusher replacement (hence App::findById('some_id') will not return anything) you can just do $connection->app = new \stdClass(); $connection->app->id = 'my_app'; to get rid of the error

Was this page helpful?
0 / 5 - 0 ratings

Related issues

connecteev picture connecteev  路  3Comments

ahmed-aliraqi picture ahmed-aliraqi  路  4Comments

sanjarani picture sanjarani  路  5Comments

semsphy picture semsphy  路  3Comments

darklight9811 picture darklight9811  路  4Comments