In this episode, we're diving into how to catch client-side events on the backend when using websockets—in this case, with the Reverb server. We start by comparing this approach to the more traditional AJAX way (which would be simple for tracking checkbox changes). But since we're aiming for real-time, efficient client-to-client communication, we want to avoid sending AJAX requests for everything and instead leverage websockets for speed.
You’ll see how we listen for events sent between clients that pass through the Reverb websocket server. The goal is to catch, on the server side, whenever a checkbox is checked in the UI by any client. We walk through creating a listener (HandleStateUpdate
) that subscribes to the relevant event from the websocket server. We'll set up some logging, restart the server, and see the events ping back as checkboxes get checked.
To make things more selective, we parse the payload and filter down to just the events we're interested in—namely, when a checkbox state changes. Once confirmed, we can then log that info and, as a next step, get ready to persist it to Redis. This is so that checked state can be remembered and re-populated for any user!
It’s a hands-on look at how you can have the backend react in real-time to changes on the client without AJAX, all thanks to bidirectional websocket communication. By the end, you'll see how to wire everything up so your backend knows exactly when a user checks a box, and how we can store that update efficiently for later use.