In my Laravel 8 project, I use laravel-websockets to send messages over websocket, laravel echo to receive messages. If I use Channel class for broadcasting, everything works great whereas I use PrivateChannel, socket connection can be established, yet no subscription is done. Hence I cannot send/receive message over websocket, but ping/pong messages can be seen on inspector of browser.
Most of stackoverflow questions were replied according to static named channels, but I have dynamic named channels that belongs to each users.
You can see my related code blocks below.
<app.php>
AppProvidersBroadcastServiceProvider::class //ADDED-to providers
<BroadcastServiceProvider.php>
Broadcast::routes(['middleware' => 'auth:sanctum']); //ADDED-auth by sanctum
<channels.php>
Broadcast::channel('task-emmiter{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
//ADDED
<MessageManager.php>
public function broadcastOn()
{
//Channel works, PrivateChannel not
return new PrivateChannel('task-emmiter'.$this->receiver);
}
<websocket.js>
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'ABCDEFG',
wsHost: window.location.hostname,
wsPort: 6001,
forceTLS: false,
disableStats: true,
}).private('task-emmiter' + mainUser.id)
.listen('MessageManager', (e) => {
console.log(e.message);
});