Commanded Projections - how do I notify my system when ready

I’m writing an ES system with commanded.

I can dispatch commands, and they are getting handled, events are being published, and I have a read model being created (using commanded.ecto.projections).

Where i am struggling is in deciding when / how to tell other parts of my system that the read model is updated. In my example, it’s a LiveView, but it could be any other part of the system.

I can broadcast a Pub/Sub event in the project method, like this:

project(%BudgetCreated{} = event, fn multi ->
    multi
    |> Ecto.Multi.insert(:budget, %Budget{
      uuid: event.budget_id,
      name: event.name
    })

    # I can broadcast the event to the live view here

    multi
  end)

But that may result in the LiveView responding to the Pub/Sub message before the db is finished updating, which would prevent the user from seeing the update.

What I would like to do is wait till the read model is finished writing to the db and then send broadcast the pub/sub message

Is there a best practice for doing this?

1 Like

Somewhat embarrassingly, I realised that despite staring at the docs for a while, I had missed the after_update/3 callback, which does exactly what I was looking for

https://hexdocs.pm/commanded_ecto_projections/Commanded.Projections.Ecto.html#c:after_update/3

4 Likes

Formulating a good question, is often half the solution.

Welcome to the forum! :wave: