Sending Private Scoped Real Time Data with Reverb — Transcript

Transcript of the free Vue.js lesson Sending Private Scoped Real Time Data with Reverbwatch the video lesson.

In this video, let's see how we can scope our channels down even further and make it so that only the currently logged in user can see their own messages. Yes, it doesn't make a lot of sense to only message yourself, but there are plenty of other applications where you'll use exactly this method. Okay, so how does it work? Well, what I want to do is I want to name our private channel here, not just messages, but messages dot whatever the ID is of the current user.

php file, much like we have right up here, I'll add a dot and then provide the ID here. Now I want to gather that ID in the second argument of the function. And then just like we have this check here, I want to check is the current user ID the same as the ID passed in the channel. Now over in my visit count file, I need to start listening for messages dot whatever my user ID is.

Okay, so right now we don't have access to the logged in user, but that's easy enough to get. The way we do that is by defining a prop. We'll call it user. We'll say this user is going to be an object.

From these props, we will get the props here. And then I'll use back ticks so I can interpolate. id. php, I'll find the route that renders that page.

And for the inertia render function, I'll pass in the user, which is the current authenticated user. So now we have a channel callback function here, checking to see if we're actually subscribed to the channel named after our user's ID. And in messages received, we are broadcasting our event on that channel. Over in the browser, now I have Shaggy logged into both of these windows here that are stacked, and Velma is logged into the one on the right-hand side.

Let's see what happens when I send a message from Shaggy. It should not show up to Velma, but only to himself. Let's see what happens. Perfect.

It works exactly as expected. And if I send a message from Velma, neither instances of the Shaggy window get it. In this video, we did not look at a realistic scenario for broadcasting data to a single user. However, in a real world app, there are plenty of use cases for this.

Things like updating their shipping status after they've ordered something from an e-commerce store. or sending them a notification in a project management app whenever somebody mentions them in a task. The possibilities are endless.