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:
https://www.youtube.com/playlist?list=PLwAKR305CRO9rlj-U9oOi4m2sQaWN6XA8

==== WRITTEN TUTORIALS ====

“Mastering Websockets in Laravel” Master Series Page:
https://devmarketer.io/learn/laravel-echo-tutorial-websockets/

==== DOWNLOAD SOURCE CODE ====
Github Code Repo for this Series:
https://github.com/DevMarketer/Laravel_Echo_Tutorial

Download Source Code for Part 4:
https://github.com/DevMarketer/Laravel_Echo_Tutorial/releases/tag/Part4

==== FOLLOW ME ====

Subscribe for New Releases!

Subscribe to DevMarketer Insider (Email)
https://confirmsubscription.com/h/d/5EDC91CF554832D1

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.

Read also >>  Tutorial CRUD Laravel Data siswa - Instalasi (part 1)

Email me hello@jacurtis.com

Thanks for all your support!

source

LEAVE A REPLY

Please enter your comment!
Please enter your name here