Laravel Websockets, some gotchas!

There is a really nice websocket composer package for laravel but a couple of issues that you may hit along the way! So the package is by beyondcode and it is here. A really nice package, but here are some gotchas that I came across.

  1. 403 Forbidden error on the auth URL
  2. 401 Unauthorized error on the auth URL
  3. Using namespaces within the listen to Echo method

So, the first 2 are kinder within the manual but I didn’t read it and in turn came up with the issues!. So going to split the 3 issues into separate areas below

403 Forbidden error on the auth URL

When you aren’t using the websocket locally, then you may hit request error of 403 and this is because of the gate defined within the Beyondcode class WebSocketsServiceProvider and method called registerDashboardGate

protected function registerDashboardGate() { Gate::define(‘viewWebSocketsDashboard’, function ($user = null) { return app()->environment(‘local’); });

and this as you can tell, is checking if the current application is running within the “local” environment setting and hence why you didn’t notice it whilst testing locally, the problem is within the config/websockets.php file

‘middleware’ =>

[ ‘web’, Authorize::class, ],

where the Authorize::class is the beyondcode class calling the above method, so we just need to replace with our authorization middleware e.g. jwt.auth.

401 Unauthorized error on the auth URL

The next issue I noticed was an error similar to

$key cannot obtain from null

This is because of the AuthenticateDashboard method that has

$app = App::findById($request->header(‘x-app-id’));
$broadcaster = new PusherBroadcaster(new Pusher( $app->key,

This is because the x-app-id wasn’t being passed in within the auth request, so altering the laravel Echo javascript object creation to include the following key value pair (please note I change the websockets.php path configuration to “websockets”

window.Echo = new Echo({ …….

authorizer : (channel, options) => {
return {
authorize: (socketId, callback) => {
axios.post(‘/websockets/auth’, {
socket_id : socketId,
channel_name: channel.name,
},
{ headers: { ‘x-app-id’: ‘<your ID from within the websockets.php configuration file, normally this is apps>’ } })
.then((response) => {
// error report
callback(false, response.data)
})
.catch((error) => {
// error report
callback(true, error)
// throw new Error(error)
})
},
}
},

Using namespaces within the listen to Echo method

The last part was the namespace issue within the echo listen to the method, where I don’t want to define the namespace of the broadcast every time like (where the App.Events was the namespace within php)

window.Echo.private(`dashboard.${dashboardID}’) .listen(‘App.Events.DashboardUpdate’, (e) => { e.message.value.forEach(function (value) { vm.$set(vm.data, value.key, value.value) }) })

So, like the above fix, we just need to add an option to the new Echo object within the javascript

window.Echo = new Echo({ …….

namespace : ‘App.Events’,

Those were the things that caused me to think, so thought it may be a good idea to post about it encase anyone else has the same issue, if you need more information on each please say!