One of the highlights for Phoenix WebSockets came from the insights of one of my co-workers. I was having a problem where I only wanted to send websocket traffic up to 1 time every 3s (debounced essentially). I was struggling with this solution and came up with a hacky solution that would debounce on the event broadcast side (vs the socket side). This means it would be up to 1 time every N seconds per server.
Co-worker showed me that sockets are just processes, and that once the process exists, I can treat it like any other process. We wrote a fairly simple state machine for debouncing, and made it debounced on the socket side. It's awesome because it's debounced even in a distributed setup. Less than 25 lines of code for all of that!
For some channels / topics I let the clients provide a `refresh_interval` parameter upon joining to tackle similar problems. I'm also curious to see your state machine, maybe it's something worth extracting to a middleware.
Co-worker showed me that sockets are just processes, and that once the process exists, I can treat it like any other process. We wrote a fairly simple state machine for debouncing, and made it debounced on the socket side. It's awesome because it's debounced even in a distributed setup. Less than 25 lines of code for all of that!