{"id":1960,"date":"2022-05-11T21:56:16","date_gmt":"2022-05-11T20:56:16","guid":{"rendered":"https:\/\/www.codingfriends.com\/?p=1960"},"modified":"2022-05-11T21:56:34","modified_gmt":"2022-05-11T20:56:34","slug":"laravel-websockets-some-gotchas","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2022\/05\/11\/laravel-websockets-some-gotchas\/","title":{"rendered":"Laravel Websockets, some gotchas!"},"content":{"rendered":"\n<p>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 <a rel=\"noreferrer noopener\" href=\"https:\/\/beyondco.de\/docs\/laravel-websockets\/getting-started\/introduction\" target=\"_blank\">here<\/a>.  A really nice package, but here are some gotchas that I came across.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>403 Forbidden error on the auth URL<\/li><li>401 Unauthorized error on the auth URL<\/li><li>Using namespaces within the listen to Echo method<\/li><\/ol>\n\n\n\n<p>So, the first 2 are kinder within the manual but I didn&#8217;t read it and in turn came up with the issues!. So going to split the 3 issues into separate areas below<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">403 Forbidden error on the auth URL<\/h2>\n\n\n\n<p>When you aren&#8217;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 <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>protected function registerDashboardGate() { Gate::define(&#8216;viewWebSocketsDashboard&#8217;, function ($user = null) { return app()->environment(&#8216;local&#8217;); });<\/p><\/blockquote>\n\n\n\n<p>and this as you can tell, is checking if the current application is running within the &#8220;local&#8221; environment setting and hence why you didn&#8217;t notice it whilst testing locally, the problem is within the config\/websockets.php file<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p> &#8216;middleware&#8217; => <\/p><p>[ &#8216;web&#8217;, Authorize::class, ],<\/p><\/blockquote>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">401 Unauthorized error on the auth URL<\/h2>\n\n\n\n<p>The next issue I noticed was an error similar to <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$key cannot obtain from null<\/p><\/blockquote>\n\n\n\n<p>This is because of the AuthenticateDashboard method that has<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$app = App::findById($request->header(&#8216;x-app-id&#8217;));<br>$broadcaster = new PusherBroadcaster(new Pusher( $app->key,<\/p><\/blockquote>\n\n\n\n<p>This is because the x-app-id wasn&#8217;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 &#8220;websockets&#8221;<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>window.Echo =  new Echo({  &#8230;&#8230;. <\/p><p>authorizer : (channel, options) => {<br>return {<br>authorize: (socketId, callback) => {<br>axios.post(&#8216;\/websockets\/auth&#8217;, {<br>socket_id : socketId,<br>channel_name: channel.name,<br>},<br>{ headers: { &#8216;x-app-id&#8217;: &#8216;&lt;your ID from within the websockets.php configuration file, normally this is apps>&#8217; } })<br>.then((response) => {<br>\/\/ error report<br>callback(false, response.data)<br>})<br>.catch((error) => {<br>\/\/ error report<br>callback(true, error)<br>\/\/ throw new Error(error)<br>})<br>},<br>}<br>},<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Using namespaces within the listen to Echo method<\/h2>\n\n\n\n<p>The last part was the namespace issue within the echo listen to the method, where I don&#8217;t want to define the namespace of the broadcast every time like  (where the App.Events was the namespace within php)<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>window.Echo.private(`dashboard.${dashboardID}&#8217;) .listen(&#8216;App.Events.DashboardUpdate&#8217;, (e) => { e.message.value.forEach(function (value) { vm.$set(vm.data, value.key, value.value) }) })<\/p><\/blockquote>\n\n\n\n<p>So, like the above fix, we just need to add an option to the new Echo object within the javascript<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>window.Echo =  new Echo({  &#8230;&#8230;. <\/p><p>namespace : &#8216;App.Events&#8217;,<\/p><\/blockquote>\n\n\n\n<p>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!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. 403 Forbidden error on the auth URL 401 Unauthorized error &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2022\/05\/11\/laravel-websockets-some-gotchas\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Laravel Websockets, some gotchas!<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[430,17],"tags":[428,429],"class_list":["post-1960","post","type-post","status-publish","format-standard","hentry","category-laravel","category-php","tag-lavarvel","tag-websockets"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1960","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1960"}],"version-history":[{"count":2,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1960\/revisions"}],"predecessor-version":[{"id":1962,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1960\/revisions\/1962"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}