Up to this point we have always worked with public channels in Laravel Echo. Public channels can be useful for websites with public facing frontends and with notifications like comments that don’t provide sensitive or private information.
So what do you do when your channels are distributing information that is only intended for certain users (like admins or managers) or the author of a blog post, or the information being sent is more sensitive? Well that is when we use private channels, which require users to authenticate in order to subscribe to a channel.
There are three basic steps needed to convert a channel from a public channel to a private one:
1. In your AppEvents file we need to change return new Channel(‘post’) to return new PrivateChannel(‘post’) inside the broadcastOn() method.
2. On the client side (which for us is on the posts.show blade file) we need to tell laravel echo that we are subscribing to a private channel now. This means changing Echo.channel() to Echo.private(). Everything else can stay the same.
3. Lastly, in our routes/channels.php file we can define our private channel with authentication rules by ensuring that the function returns true or false to determine the user’s eligibility for that private channel.
Next Up: Working with Presence Channels
==== MORE FROM THIS SERIES . ====
Full Playlist for the “Mastering Websockets in Laravel” Series:
==== WRITTEN TUTORIALS ====
“Mastering Websockets in Laravel” Master Series Page:
==== DOWNLOAD SOURCE CODE ====
Github Code Repo for this Series:
Download Source Code for Part 4:
==== FOLLOW ME ====
Subscribe for New Releases!
Subscribe to DevMarketer Insider (Email)
Twitter – http://twitter.com/_jacurtis
(ask me questions!)
==== QUESTIONS? ====
Leave a comment below and I or someone else can help you.
For quick questions you may also want to ask me on Twitter, I respond almost immediately.
Email me email@example.com
Thanks for all your support!