This episode is for members only

Level up with a premium membership

Join now
Already a member? Sign in to continue
Playing
11. User joining and leaving events

Transcript

00:00
So we saw the issue in the last episode where when we join the room or when we hit the page, it will give us a list of the users who are currently here, but it won't set or remove them when they're leaving or joining. So let's go ahead and create a couple more events on our presence channel to include this. OK, so when a user joins, we want to push them to this list. So let's go ahead and create out a method to deal with when a user joins.
00:29
So we're going to say set user joining and into here we are going to get the user or the object of that ID of the user who is joining. OK, so we can pretty much copy and paste the event here because we just really need to change the event name, which is joining. And now we just want to push to that list of IDs. So let's go and just push on that user ID.
00:56
So remember, this contains an array now of just the ID. OK, now that we've done this, let's take a look at this in action. So I'm going to go back over to the dashboard on Mabel's account, refresh here, and then I'm going to join this room. So let's go over to chat and Laravel.
01:12
And there we go. You can see that Mabel has been pushed to the stack. OK, so now that we've done that, let's look at the leaving event. So I'm going to go and paste this down.
01:20
And sure enough, you guessed it. We're going to have a leaving event. Let's set set user leaving and we'll get that user in there. Now, this one's slightly more complex because we want to remove this item from this list.
01:34
There's a few ways to do this, but let's go ahead and reassign IDs to a filtered version of the current IDs. So we'll pass in this IDs. This is just a native PHP function. We'll have a closure just in here, which we can pull the current ID.
01:52
We're iterating through. Let's bring user into scope and then let's just return the condition that we need to remove that user. And that is just that the ID that we're iterating through is equal to the user ID who has left. And that will just get rid of it from this array.
02:10
OK, let's try this out and see what happens. So let's refresh both pages just to make sure. And yeah, we use the wrong condition here. So it's got rid of the user who didn't leave or all of the users who didn't leave.
02:23
So we need to say doesn't equal and we won't use a strict type there just in case they are different types. OK, let's try this again. So I'm going to go back over to chat, refresh both pages. Mabel is going to leave.
02:37
And there we go. She has been removed from the list of users. If we go back over to that room, sure enough, she's added back in. So all of this is now kept nicely up to date with all of the users who are in the room, whether they join or whether they leave.
14 episodes1 hr 23 mins

Course overview

Join a room and start chatting! This course covers building a multi-room text chat app with Livewire using Laravel Reverb for real-time updates.

Using presence channels and client-to-client whispering, we’ll also show who’s online, and who’s currently typing.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.