Let's add Live count for total website visitors
and Live count for visitors inside a room
.
Importantly, we will not deal with user REGISTRATION to identify a visitor. One browser session = one unique visitor with a session_id.
We will use turbo_stream_from
to plug into an ActionCable Channel and track websocket subscriptions and un-subscriptions.
Whenever a user subscribes
, we will add his browser session_id to a Kredis array.
We will Broadcast the updated Kredis array COUNT to everybody subscribed to the channel.
Sounds quite complex, but I still think of it as a very low-code solution.
Text walkthrough: https://blog.corsego.com/action-cable-broadcast-users-online
Episode source code with comments: https://github.com/corsego/109-live-visitor-count-action-cable-kredis-turbo/commit/645c08eb981edaaad919f070e300a2ad24d28b08
0:00 Demo - Track visitors on a website and in a room
1:51 Track all website visitors with Kredis and ActionCable
12:25 Update live vistiors count with Turbo Stream Broadcasts
14:29 Track visitors in a Room
20:40 Summary and final demo