In this episode, we're diving into how to show which users are currently online in a chat room by making use of Laravel's presence channels.
We start off by tweaking our channel authorization logic so that, instead of just returning a static value, we actually return some important info about the current user (in this case, their user ID). This way, when users join the channel, their identifiers are broadcasted and we can track who's present. The logic stays simple—return false if a user isn't allowed, and otherwise return their ID.
Instead of sending big blobs of user data through the channel, we're deliberately keeping it slim by just sending the ID. On the client side, we'll use these IDs to fetch the actual user details directly from the database. This has the added benefit that we can keep using all the relationships and methods on the User model, rather than dealing with a bunch of plain data.
Next, we spin up a new Livewire component called ChatUsers. This component listens for presence events (using Echo) and keeps track of which user IDs are in the room. We add a computed property that fetches the user records by their IDs, making it really easy to output detailed user info like their name, avatar, and more.
We finish by updating the component template to display a nice list of users who are currently in the channel, complete with some basic styling. You’ll spot that we still need to listen for join/leave events to keep things perfectly in sync (e.g., when someone leaves or joins in real time), and we’ll get to that in the next steps!
So by the end of this video, you’ll have set up the backend and frontend needed to show a live, updating list of online users in a chat room using presence channels and Livewire.