AF
Ash Frameworkโ€ข4mo ago
VK

Can't publish the same action twice

Hello, I have a challenge with notifications that I don't know how to solve. Would appreciate any advice! When an interaction that is part of the session is updated, I want to sent notification to both the interaction and the session. The below doesn't work:
pub_sub do
module FiveWhysWeb.Endpoint
prefix "interaction"

publish :process_response, ["response", :id]
publish :process_response, ["updated", :session_id]
publish :handle_error, ["error", :id]
publish :handle_error, ["error", :session_id]
end
pub_sub do
module FiveWhysWeb.Endpoint
prefix "interaction"

publish :process_response, ["response", :id]
publish :process_response, ["updated", :session_id]
publish :handle_error, ["error", :id]
publish :handle_error, ["error", :session_id]
end
None of the notifications are sent. If I comment out the repeated lines, the notifications are sent as usual. A hack would be to do something like
pub_sub do
...
publish :process_response, [["response", "updated"], [:id, :session_id]]
...
end
pub_sub do
...
publish :process_response, [["response", "updated"], [:id, :session_id]]
...
end
but that would send out 4 notifications to of them with wrong ids. Any suggestions how I should approach this? Or maybe it's a feature request for Ash Notifications to support more complex templates and/or allow duplicate publish entries?
6 Replies
ZachDaniel
ZachDanielโ€ข4mo ago
๐Ÿค” that should work please open a bug report
VK
VKOPโ€ข4mo ago
Thanks Zach, will do. And yes, I expected this to work as it's natural complement to the powerful template syntax.
VK
VKOPโ€ข4mo ago
GitHub
Can't publish the same action twice ยท Issue #2150 ยท ash-project/ash
Versions ash 3.5.23 ash_admin 0.13.10 ash_authentication 4.9.4 ash_authentication_phoenix 2.10.1 ash_graphql 1.7.15 ash_phoenix 2.3.7 ash_postgres 2.6.8 ash_sql 0.2.82 Operating system MacOS Sequoi...
ZachDaniel
ZachDanielโ€ข4mo ago
If you have a chance to provide a reproduction that would be excellent as well like a project I can run etc.
VK
VKOPโ€ข4mo ago
Yes, working on this. I also asked Claude Code to investigate - will let you know if it finds anything interesting ๐Ÿ™‚ Hm, Claude Code thinks that it should work:
The Issue

The pub_sub system in Ash does support multiple publish declarations for the same action. When you declare:

publish :process_response, ["response", :id]
publish :process_response, ["updated", :session_id]

Both publications should trigger when the :process_response action is executed. The system:

1. Retrieves all publications via Ash.Notifier.PubSub.Info.publications/1
2. Filters them to match the action being performed
3. Executes each matching publication separately

The test at /Users/victor/src/ash/test/notifier/pubsub_test.exs:193 demonstrates this working correctly - multiple publish :update declarations all trigger.
The Issue

The pub_sub system in Ash does support multiple publish declarations for the same action. When you declare:

publish :process_response, ["response", :id]
publish :process_response, ["updated", :session_id]

Both publications should trigger when the :process_response action is executed. The system:

1. Retrieves all publications via Ash.Notifier.PubSub.Info.publications/1
2. Filters them to match the action being performed
3. Executes each matching publication separately

The test at /Users/victor/src/ash/test/notifier/pubsub_test.exs:193 demonstrates this working correctly - multiple publish :update declarations all trigger.
Working on a repro Ok, so maybe it was a false alarm, although it's complicated. I was running my test script mix run my-script.exs at the same time I was running mix -S iex phx.server in another terminal window (to make tidewave MCP available for Claude). Turns out this somehow interfers with notifications: 1/ Without the second command running, I get all the notifications as expected. 2/ With the second command running, the notifications log output is printed to the iex phx.server terminal window for other notifications, but the :process_response is never triggered it seems - I don't see the log output for it in any of the windows. It might be that somehow Endpoint broadcasting is not working - but the logs printed by setting config :ash, :pub_sub, debug?: true should be printed before the Endpoint.broadcast is called, correct? Any thoughts on how to test this further? Ok, nevermind. I figured it out! 1/ I had two different Elixir processes running, they were not talking to each other 2/ The first process, mix run, used Oban to schedule some LLM jobs; once they were complete, they would call an update action on a resource 3/ That resource is backed by Ets (during testing), so it only existed in the memory of the mix run process 4/ It seems that the Oban job was picked up by the second process, mix -S iex phx.server, since Oban is backed by the same database that both processes had access to 5/ So once the Oban job was complete in the second process, it tried to find a Resource to update and failed (since that object doesn't exist in that process) 5/ A failed update action, therefore, was not triggering the notification So all is good with Ash notifications - it was a complete unrelated issue. Sorry for the false alarm!
ZachDaniel
ZachDanielโ€ข4mo ago
No problem ๐Ÿ˜„ Put that write up in the issue and close it if you can, maybe it will help someone in a similar situation ๐Ÿ˜„

Did you find this page helpful?