Laravel-websockets: Unable to register custom WebSocket Handler

Created on 5 Dec 2018  路  10Comments  路  Source: beyondcode/laravel-websockets

Hi,
First of, thanks for your great work!

When registrering the route for a custom WebSocket handler using:
WebSocketsRouter::webSocket('/socket', \App\WS\Handlers\Handler::class)
in reoutes/web.php laravel is unable to boot with the following exception:
Target [Symfony\Component\Console\Output\OutputInterface] is not instantiable while building [BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger].

My workaround for the issue is to call:

use Symfony\Component\Console\Output\NullOutput;
app()->singleton(WebsocketsLogger::class, function () {
    return (new WebsocketsLogger(new NullOutput()))->enable(false);
});

before the route registering. I guess that defering the route registering to after the the server has setup the loggers would be a solution?

All 10 comments

I get the same problem and I haven't had time to dig into it.
Thanks for your temporary solution.

I have same issue even if just extending:

<?php

namespace App\WebSocket;

use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler as BaseWebSocketHandler;

class WebSocketHandler extends BaseWebSocketHandler
{
}

same issue here.. even the temp solution is not working for me !

Any updates on this? The solution from xclose also doesn't work for me. Now the custom handlers are not working

I've extended the Router class and added my own WebSocketsHandler (that extends the existing one) into the echo() function, then extended websockets.router in IoC by doing the following in my own service provider:

$this->app->extend('websockets.router', function () { return new CustomRouter(); });

@stefandanaita could you share the code that you think it is necessary to address this issue?

@xclose the solution i found based on your post was;

use Symfony\Component\Console\Output\NullOutput;
use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger;

app()->singleton(WebsocketsLogger::class, function () {
    return (new WebsocketsLogger(new NullOutput()))->enable(false);
});

use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter;

WebSocketsRouter::webSocket('/ws-method', \App\MyWebSocketHandler::class);

It works, but it does not solve the problem, the problem is related to @xclose comment....

I have a PR open to fix this but if anyone needs this temp.

<?php

namespace App\Providers;

use App\Services\AssetService;
use App\Services\GuestService;
use Illuminate\Support\ServiceProvider;
use App\WebSocketHandlers\ClientSocketHandler;
use Symfony\Component\Console\Output\ConsoleOutput;
use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter;
use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(AssetService::class, AssetService::class);
        $this->app->bind(GuestService::class, GuestService::class);

        // https://github.com/beyondcode/laravel-websockets/issues/21
        app()->singleton(WebsocketsLogger::class, function () {
            return (new WebsocketsLogger(new ConsoleOutput()))->enable(true);
        });

        if ($this->app->environment() !== 'production') {
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
        }
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        WebSocketsRouter::webSocket('/app/{appKey}/{apiKey}', ClientSocketHandler::class);
    }
}

@mpociot Can we get a release tagged for this? I am unable to use the current release of this package with a custom handler without the workaround above.

AppServiceProvider.php

    public function register()
    {
        ...
        $this->app->singleton('websockets.router', function () {
            return new Router();
        });
        ...
    }

Create MySocketHandler.php and see: Custom WebSocket Handlers

namespace App\WebSockets;

use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler;
use Ratchet\ConnectionInterface;

class MySocketHandler extends WebSocketHandler
{

    public function onOpen(ConnectionInterface $connection)
    {
        parent::onOpen($connection);

        \Log::info('Has connection');
        // TODO: Implement onOpen() method.
    }
}

Add this line into web.php

WebSocketsRouter::webSocket('/my-websocket', App\WebSockets\MySocketHandler::class);

Restart WebSockets

php artisan websockets:serve

Then, you can try to use : Firecamp Plugin
Try to send connect to: ws://localhost:6001/my-websocket

Was this page helpful?
0 / 5 - 0 ratings