1 year ago

#368785

test-img

X 47 48 - IR

Laravel - Unlike the public channels, private channels don't work

I'm using Laravel 9.1, and trying to broadcast on private channels, but unlike the public channels, it doesn't work for some reason!

I have an event like this:

class newMessage implements ShouldBroadcast
{
   use Dispatchable, InteractsWithSockets, SerializesModels;

   public string $message;

   public function __construct($message = null)
   {
       $this->message = $message;
   }

   public function broadcastOn()
   {
       return new PrivateChannel('notification.' . auth()->user()->username);
   }
}

When in broadcastOn I return an instance of Channel, everything works pretty well, but when I change it to PrivateChannel, it doesn't work anymore.

channels.php

Broadcast::channel('notification.{username}', function ($user, $username) {
    return true;
});

I've tried both soketi and websockets; In both, I had the same result.

Configs

In the the BroadcastServiceProvider.php file, I've tried all these possibilities:

  • Broadcast::routes();
  • Broadcast::routes(['middleware' => ['auth:api']]);

none of them worked.

config/broadcasting.php

      'pusher' => [
          'driver' => 'pusher',
          'key' => env('PUSHER_APP_KEY'),
          'secret' => env('PUSHER_APP_SECRET'),
          'app_id' => env('PUSHER_APP_ID'),
          'options' => [
              'cluster' => env('PUSHER_APP_CLUSTER'),
              'host' => env('PUSHER_HOST', '127.0.0.1'),
              'port' => env('PUSHER_PORT', 6001),
              'scheme' => env('PUSHER_SCHEME', 'http'),
              'useTLS' => false,
              'encrypted' => false,
          ],
      ],

Note

Authentication Routes Don't Execute

When I try to log something in the authentication scope in the channels.php file before returning a boolean using \Illuminate\Support\Facades\Log::info($user); it doesn't log anything!

Broadcast::channel('notification.{username}', function ($user, $username) {
    \Illuminate\Support\Facades\Log::info($user);
    return true;
});

Socket Servers

When I broadcast on public channels, websockets console returns:

Connection id 76490676.561659290 sending message {"channel":"notification.admin","event":"App\\Events\\newMessage","data":"{\"message\":\"[2022\\\/04\\\/03 06:26:53]: Pigeon had finished. 'As if it wasn't very civil of you to learn?' 'Well, there was mouth enough for it now, I suppose, by being drowned in my size; and as Alice could not be denied, so she tried.\",\"date\":\"2022-04-03T06:26:53.988364Z\"}"}

But in private channels, when I try to broadcast something, it doesn't return or log anything at all; so it looks for some reason, Laravel even doesn't send the request to the socket server.

php

laravel

sockets

websocket

pusher

0 Answers

Your Answer

Accepted video resources