I'm implementing a Soketi solution with Pusher (I'm not using Echo). I'm able to send notifications to Private channels, however, I'm not able to validate if the user has access to the channel / endpoint, using the channels.php
. It seems that channels.php
is never triggered.
routes/channels.php
Broadcast::channel('test.{id}', function ($user, $id)
{
\Log::debug('testing this endpoint'); // Never executed
return false; // Also never executed, doesn't matter if true or false
});
config/app.php
'providers' => ServiceProvider::defaultProviders()->merge([
\hisorange\BrowserDetect\ServiceProvider::class,
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\BroadcastServiceProvider::class, // <--- uncommented this line
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
])->toArray(),
app/providers/BroadcastServiceProvider.php
class BroadcastServiceProvider extends ServiceProvider
{
public function boot(): void
{
Broadcast::routes();
require base_path('routes/channels.php');
}
}
TestEvent.php
class TestEvent implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct(public string $text)
{
//
}
public function broadcastOn(): array
{
return [
new PrivateChannel('test.3')
];
}
public function broadcastAs(): string
{
return 'message';
}
}
Pusher JS configuration
const pusher = new Pusher('app-key',
{
wsHost: '127.0.0.1',
wsPort: 6001,
forceTLS: false,
encrypted: true,
disableStats: true,
enabledTransports: ['ws', 'wss'],
authEndpoint: 'api/auth',
auth:
{
headers:
{
'Authorization': `Bearer ${token}`,
},
}
});
const channel = pusher.subscribe('private-test.3');
channel.bind('message', function(message)
{
console.log(message);
});
Testing the event:
broadcast(new TestEvent('Hello World!'));
What happens:
broadcast/channel/test.{id}
, meaning that the channels.php
is never triggeredWhat am I missing?
The Pusher Log
Pusher : ["State changed","initialized -> connecting"]
Pusher: ["Connecting",{"transport":"ws","url":"ws://127.0.0.1:6001/app/app-key?protocol=7&client=js&version=8.3.0&flash=false"}]
Pusher: ["State changed","connecting -> connected with new socket ID 8035059900.2497835364"]
Pusher: ["Event sent",{"event":"pusher:subscribe","data":{"auth":"app-key:8623bcc00ba6c28dea66645316bd2eb2943aa5c8115f1cac7cede17e4994809a","channel":"private-test.3"}}]
Pusher: ["Event recd",{"event":"pusher_internal:subscription_succeeded","channel":"private-test.3"}]
Pusher: ["No callbacks on private-test.3 for pusher:subscription_succeeded"]
Pusher: ["Event recd",{"event":"message","channel":"private-test.3","data":{"text":"Hello World!"}}]
my-js.js {text: 'Hello World!'}
Solved.
It seems that I'm forced to use the endpoint api/broadcasting/auth
in order to channels.php
be called / executed.
On the Pusher JS configuration all I had to do was changing this line:
authEndpoint: 'api/auth',
to:
authEndpoint: 'api/broadcasting/auth',