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