Playing
15. Detecting who’s in a channel

Transcript

00:00
In the last episode, we saw that we get these kind of events come through. Push your internal member added with a bunch of data.
00:07
Now, we don't have to listen to this event specifically with Echo to work out who's currently in the room. All of this is hidden behind Echo's API.
00:16
So what we can do, rather than listen to an event in here, we can just say here. And that will give us all of the users or all of the models or whatever you're returning who are currently in this room.
00:28
So let's go ahead and define our closure out in here. And this will give us a list of all of the users or whatever you're doing inside of this channel. So let's go and console log on users and see what we get.
00:41
OK, let's go over to our console, give this a refresh. And yeah, sure enough, we have got an object or an array of objects of all of the people who are currently in this channel.
00:51
I'm going to go ahead and create out another account within a incognito tab so we can see both of the users who are in here. So let's go and register out a new account
01:04
within our application. Let's go over to that room. And let's have a look and see what happens. So let's pull this over.
01:14
Let's give this a refresh. Now, Mabel's in there. And you can see, sure enough, we now have two users that are in that room.
01:21
So using this, what we can do is just start to assign this anywhere that we're working, so whichever framework you're working with. And we can just iterate through a list of the users.
01:34
Let's go ahead and do that now. So again, just using Alpine, we'll store some data in here. And this will just be users here. Let's set that to an empty array by default.
01:46
And let's go ahead and iterate through. So within the template section of this, let's create out a separate div here. We'll create out a header.
01:55
Let's just assign some classes to this so it looks a bit nicer. We'll make this bold with text large. And we'll say users here.
02:03
And then down here, we'll go ahead and create out a template so we can iterate over with Alpine. And we will say x for user in users here. And then we can just go ahead and dump out
02:17
any information we need about these users. So let's say x text. And let's grab the name from that user object. OK, if we go over and just give this a refresh,
02:28
nothing's working at the moment because we're not assigning this value. So we just want to say users here and assign that to the users that are currently in the room.
02:38
OK, let's go over. And there we go. You can see that both of these now show who is in the room. Now, at the moment, it's a little bit flaky
02:45
because we're only using one event. If, for example, Mabel leaves this room and goes back to the dashboard, you can see that this is still hanging around.
02:54
Here will not be fired. That event will not be fired when someone leaves a room. We need to use a couple of other events to make sure that we can keep this list up to date.
18 episodes1 hr 19 mins

Overview

New to realtime broadcasting in Laravel? This course covers the essentials with plenty of examples along the way, leaving you ready to start adding realtime functionality to any of your Laravel applications.

Broken up into channel types, we’ll cover:

  • The basics of installing, configuring and running a Reverb server
  • Broadcasting events
  • Private channels
  • Presence channels
  • Client-to-client broadcasting by whispering
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!