phoenixframework / phoenix_pubsub

Distributed PubSub and Presence platform for the Phoenix Framework
http://www.phoenixframework.org/
MIT License
641 stars 121 forks source link

RFC: Provide a way to mass update presences #145

Open AndrewDryga opened 4 years ago

AndrewDryga commented 4 years ago

ATM if you want to update all presences for a topic you need to call Tracker.list/2 and then Tracker.update/5 for each item in the list. Tracker.update/5 implemented on top of GenServer.call/3 which would be really expensive if we going to update lots of presences at a time.

What do you feel about adding a Tracker.update_metas/3:

Tracker.update_metas(tracker_name, topic, fn _pid, _key, meta ->
  Map.put(meta, :last_updated, DateTime.utc_now()
end)
AndrewDryga commented 4 years ago

Our use case: we use Phoenix.PubSub to broadcast metrics to sockets that push data into charts in our application. To make sure that we don't pull and broadcast metrics that nobody is interested in we are using Tracker. When process subscribes for metrics we are tracking it's presence and check if anyone is subscribed for metric before pulling it from DB.

But we need to persist last event timestamp which we sent to processes so that if metrics server crashes it can recover and send remaining data while it was down. We can use presence metadata to track it but we would need to update lots of presences all the time.