EvanPerreau's avatar

Laravel + VueJs don't receive Reverb event

Problem Description: I’m facing an issue with broadcasting WebSocket events in my Laravel application, using Reverb as the WebSocket server and Laravel Echo on the client side. Here’s the situation:

Backend:

I’m using a public channel named ticket-chat. The event is successfully broadcasted on the server. Logs confirm that the event is dispatched with the correct data. Frontend:

The WebSocket connection is successfully established, as evidenced by logs like pusher:ping and pusher:subscribe. However, I’m not receiving the NewChat event in the client console, despite listening on the channel. Current Setup:

Environment variables for Reverb (app_key, host, port, scheme) are defined and used via import.meta.env. Laravel Echo is configured with these variables to connect to Reverb.

class NewChat implements ShouldBroadcast
{
    use Dispatchable;
    use InteractsWithSockets;
    use SerializesModels;

    /**
     * @inheritDoc
     */
    public function broadcastOn(): array
    {
        return [new Channel('ticket-chat')];
    }

    /**
     * Get the data to broadcast.
     *
     * @return array<string, mixed>
     */
    public function broadcastWith(): array
    {
        Log::info('Broadcasting NewChat event', ['channel' => 'ticket-chat', 'data' => ['id' => 1]]);
        return ['id' => 1];
    }
}
onMounted(() => {

    console.log(window.Echo);
    window.Echo.channel('ticket-chat')
        .listen('NewChat', (e: any) => {
            console.log("edadaz");
            console.log(e);
        })
        .error((error: any) => {
            console.error('Erreur Echo :', error);
        });
});

final class NotificationController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        broadcast(new NewChat());
        return redirect()->back()->with([
            'message' => [
                'type' => 'success',
                'title' => 'Notification sent successfully',
                'message' => 'The notification has been sent successfully.'
            ]
        ]);
    }
}

please help me, thanks

0 likes
3 replies
JussiMannisto's avatar
Level 50

Is NewChat in the App\Events namespace? If it's not, you need to set the namespace in echo.js:

window.Echo = new Echo({
	// ...
	namespace: 'App.MyEvents',
});

Alternatively, you can use the fully-qualified class name prefixed with a dot:

Echo.channel('orders')
    .listen('.App\\MyEvents\\NewChat', (e: any) => {
        // ...
    });

https://laravel.com/docs/11.x/broadcasting#namespaces

1 like
MOEN-ERAK's avatar

.env in Laravel project change

# QUEUE_CONNECTION=database
to
QUEUE_CONNECTION=sync

it work for me

Please or to participate in this conversation.