Parameterizing notifier topic with generic actions

I have a couple of generic actions, and I’d like to use the pubsub notifier to publish a notification when each is run. I have my notifier working with create/update/destroy actions. The create/update/destroy actions can use an :attribute of the associated resource to parameterize the topic–is there an equivalent way to use data from the arguments or return value of a generic action? I wasn’t able to find any documentation describing the use case.

Thanks in advance for any guidance, and I appreciate all the work folks have put into Ash. :slight_smile:

Edit: I found the relevant snippet of code (I think) here: ash/lib/ash/actions/action.ex at v3.4.51 · ash-project/ash · GitHub. It looks to me like if my action were running outside of a transaction, then I could return {:ok, value, notifications}. However, the transaction branch doesn’t appear to support that; maybe I’m missing some hidden way to pass the notifications along in that branch?

Currently you can’t emit notifications on generic actions, only for create/update/destroy actions. The logic inside of the generic action handling that you are seeing is for emitting notifications triggered by nested action calls.

We could potentially support notifications on generic actions, but for backwards compatibility it would be off by default, opt-in on the generic action or something along those lines.

Could you tell me a bit more about your use case?

Thanks for the explanation. :+1:

Sure, here’s my use case. I have a resource CardGroup representing a collection of Card resources with a bit of extra metadata. The API I expose allows the user to group two cards together or remove a card from a group. I’ve exposed those as two generic actions. The first takes two card ids, and the second takes one. Under the hood I need to do some bookkeeping to manage the groups themselves.

When card groupings change, I need to send out a pub sub notification so other users interacting with those cards in a live view can refresh card membership and maintain an up to date view. The cards belong to a parent resource that each live view is also associated with. The live view subscribes to pub sub topics named with the parent resource id. I’d like to publish a notification to such a topic when either generic action is run.

Does that make sense?

Edit: writing it out, I suppose I could define those actions as update actions on the card resource, though they are more about managing the memberships of cards in the CardGroup resource. From a code organization perspective that’s where I would prefer for them to live.

Having update actions that don’t modify the record in question is perfectly acceptable, and is probably what I’d do. I’m open to supporting notifications from generic actions eventually but it wouldn’t happen in the short term.

In the meantime, you could manually emit pubsub events from the actions in question, although it would be up to you to handle any transaction semantics etc.

1 Like