I have a system that emits events, such as:
"record_updated", id: 6, company_id: 1
"record_updated", id: 2, company_id: 2
"record_updated", id: 4, company_id: 1
These events are currently dispatched through Phoenix PubSub, from nodes in an Elixir cluster, and either through Channels or GraphQL subscriptions (Absinthe) get delivered to clients who react appropriately to these updates.
The problem is the amount of traffic, and number of updates.
I would like to debounce these events and group by company_id and only emit them when the updates stop or a configured time period is reached.
So, if the events happen constantly, we would emit one event per company_id, having a list of IDs, rather than single ID in their payload, once every 10s or so.
If there was just one event, I think I’d like to wait like a second and emit it to the client as soon as possible.
For example, given the above 3 events, I would like to transform them and emit just two:
"records_updated", ids: [2], company_id: 2
"records_updated", ids: [6, 4], company_id: 3
Any hints on libraries/tools I could use to simplify this task? Should I be looking at GenStage/Broadway and implement this from scratch or there are some tools out there that do something similar to my requirements that you can recommend?