In this episode, we dive into how to add real-time typing indicators to our chat app using WebSockets, specifically leveraging Echo's whisper feature. Rather than sending every single keystroke to the backend (which would be overkill and inefficient), we use whispering to broadcast typing events directly to other connected clients.
First, we set up our client to send "typing" and "not typing" events via Echo whenever a user starts or stops typing. These events are sent directly through the WebSocket server, bypassing the need for a backend event, making it super lightweight and fast. We connect to a private channel for each chat room and whisper out the user's typing status, including their user ID.
On the receiving side, we listen for these typing events and update an array of typing user IDs in real time. This lets us display to everyone in the room exactly who is currently typing. We make sure to avoid duplicate entries to keep our data clean, and handle removing users from the typing list when they stop.
Finally, we update the UI so that when someone is typing, an indicator (either some text or a fun SVG icon) appears next to their name and disappears once they stop. We even throw in some transitions and animations for a little extra flair!
By the end of this video, we’ve got a smooth, live typing indication system, making our chat feel a lot more responsive and engaging.