Sending Private Real Time Data with Reverb — Transcript

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

In this lesson, we're going to make our chat app a little bit more realistic by allowing chats only between logged in users. In the process, you'll see how we can limit our broadcasts, that is our real time data sending, based on certain conditions. All right, since we're using Laravel Breeze, login and register is already set up for us in our app. I have gone ahead off camera.

and created an account for Shaggy Rogers and for Velma Dinkley. Let's see how we can adjust the code to make the chat work between these two logged in users. Back in the message received event, now I'd like to get the name of the logged in user and pass it down as data. So I'll add a new property here called who and then inside of our constructor, I'll set who equal to the currently logged in user's name.

Now, I only want to broadcast to logged in users. So instead of using channel to broadcast absolutely anybody, I'm going to change this to private channel. php file, we need to define the channel name with a callback function. to determine what the rules are for subscribing to this channel.

I'll just copy the existing one here, change out the name of this channel to messages to match what was defined over in message received. And then for the callback function, we no longer need an ID passed in. So notice up here, we're accepting a dynamic ID within the channel name. We aren't doing that here.

Therefore, the ID is not passed into the callback function. Now what I want to do is I want to return true or false based on whether or not the user can actually subscribe or not. I could check to see if the user exists, meaning this is a logged in person and so they are allowed to subscribe to the channel and that would actually be not empty, right? However, since private channels are scoped to logged in users anyway, I can really just always return true from this.

So any logged in user is always able to subscribe to this channel. The last thing I should do now is over in our view code. view. And when listening to a private channel, you now want to call the private method on Echo.

and instead of hard coding who now as them, we can just get the who that will come down in the event data. So I'll get rid of this and just pass E directly. All right, let's give it a try. So remember Shaggy's logged in over here and Velma is logged in over here.

Acting as Velma, let's say I found a clue, send the message, and sure enough, the message shows up for Shaggy. We'll respond, Shaggy says, I'm afraid, sends the message back and we get that appropriately as well. Now notice I am in an incognito browser here so that I could have two different active sessions with two different users. So if you try to test this out yourself, make sure one of your browsers is an incognito browser.

Now I do want to introduce one more session type into this mix. That is no session at all. One, whether is no logged in user. So I'm going to open up Safari and let's see if any of the messages that I pass back and forth between Shaggy and Velma are exposed to this non-logged in user.

Nope, sure enough here in Safari, nothing comes through, but it does for those logged in users. php file and providing a callback function to further clarify who all has access, and then using the private method on Echo instead of the channel method.