Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

salfel's avatar

Laravel websockets crash

When using a custom laravel websockets handler, once i dispatch a event within the handler, it crashes the whole websocket server, it doesnt exit nor throw an error, but it doesnt accept messages anymore

class CustomWebsocketHandler extends WebSocketHandler
{
    public function onMessage(ConnectionInterface $conn, MessageInterface $msg): void
    {
        if ($msg->isBinary()) {
            $payload = $msg->getPayload();

            $path = 'images/'.\Str::random().'.JPEG';
            Storage::disk('public')->put($path, $payload);
            $image = Image::create(['path' => '/storage/'.$path]);
            FileUpload::dispatch($image);

            return;
        }
        parent::onMessage($conn, $msg);
    }
}

I've binded the class to the websockethandler in order to overwrite her

class BroadcastServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->bind(WebSocketHandler::class, CustomWebsocketHandler::class);
    }

    public function boot(): void
    {
        Broadcast::routes();

        require base_path('routes/channels.php');
    }
}
0 likes
0 replies

Please or to participate in this conversation.