In this episode, we're diving into how to detect and display who's currently in a channel when using Laravel Echo (and Alpine.js in this example). First, we talk about how you don't need to listen to lower-level events like internal:member_added
directly for this; Echo's API handles all that under the hood for you.
We'll walk through setting up a closure that fetches all current users in the channel, then log that list to the console so you can see all the users (or models) currently present. To make things more interesting, we create a new account in an incognito window to demonstrate multiple users in the same channel – and, sure enough, the list updates to show both users.
Next, we switch gears a bit and use Alpine.js to display the users list directly in the UI. We go through setting up the initial data, creating a simple header, and iterating over the list to show each user's name. You'll see it in action as the list updates in real time when users join.
At the end, we point out a limitation: right now, if a user leaves the channel (like by navigating away), the list doesn't automatically update, because we're not listening to the leave events yet. That'll be the next thing we fix, so stay tuned!