AF
Ash Framework•3mo ago
Failz

ash notifier pubsub question

I've got a scenario where pub sub from one resource works, and then another resource where it does not. in the live view we're subscribed and handling:
def mount(_params, session, socket) do
Phoenix.PubSub.subscribe(App.PubSub, "budget:#{budget_id}")
end

def handle_info(
%Phoenix.Socket.Broadcast{
topic: "budget:" <> budget_id,
event: _event,
payload: _data
},
socket
) do
end
def mount(_params, session, socket) do
Phoenix.PubSub.subscribe(App.PubSub, "budget:#{budget_id}")
end

def handle_info(
%Phoenix.Socket.Broadcast{
topic: "budget:" <> budget_id,
event: _event,
payload: _data
},
socket
) do
end
this resource works:
pub_sub do
module AppWeb.Endpoint
prefix "budget"

publish_all :create, [:budget_id] do
transform & &1.data
end

publish_all :update, [:budget_id] do
transform & &1.data
end
end
pub_sub do
module AppWeb.Endpoint
prefix "budget"

publish_all :create, [:budget_id] do
transform & &1.data
end

publish_all :update, [:budget_id] do
transform & &1.data
end
end
this one does not:
publish_all :create, [:budget_id] do
transform fn notification ->
transfer = notification.data

case App.Finance.get_account(transfer.from_account_id, tenant: transfer.organization_id, authorize?: false) do
{:ok, account} ->
%{notification | data: Map.put(transfer, :budget_id, account.budget_id)}
{:error, error} ->
notification
end
end
end
publish_all :create, [:budget_id] do
transform fn notification ->
transfer = notification.data

case App.Finance.get_account(transfer.from_account_id, tenant: transfer.organization_id, authorize?: false) do
{:ok, account} ->
%{notification | data: Map.put(transfer, :budget_id, account.budget_id)}
{:error, error} ->
notification
end
end
end
I've gone a few rounds with the LLM and haven't been able to get this to work. The transform being the thing that's different between the two resources, so I suspect I'm doing something different.
21 Replies
ZachDaniel
ZachDaniel•3mo ago
Any info beyond not working? How is it not working?
Failz
FailzOP•3mo ago
the live view doesn't see the event i sprinked in some logging inside the transform that does not work and see this:
[info] Transfer PubSub: Publishing to topic 'budget:078f18dd-2c0c-458f-be57-23408b1a2297'
[info] Transfer PubSub: Publishing to topic 'budget:078f18dd-2c0c-458f-be57-23408b1a2297'
added some logs to the live view handle_info and for the resource where it does work I see this, which tells me things are wired up correctly.
[info] Sidebar PubSub: Received message on topic 'budget:078f18dd-2c0c-458f-be57-23408b1a2297'
[info] Sidebar PubSub: Received message on topic 'budget:078f18dd-2c0c-458f-be57-23408b1a2297'
ZachDaniel
ZachDaniel•3mo ago
There is a pubsub notifier debug option Not at all computer so I can't link you to it But it will log all sent messages maybe that will help
Failz
FailzOP•3mo ago
config :ash, :pub_sub, debug?: true that look like it?
ZachDaniel
ZachDaniel•3mo ago
Yes How are you doing the create in question?
Failz
FailzOP•3mo ago
it's the double entry transfer create, with this: create_accept [:payee, :timestamp, :transfer_date]
schnady
schnady•3mo ago
Do you see smth like
debug] Broadcasting to topics ["budget:666"] via ....

Notification:

%Ash.Notifier.Notification{resource: ...
debug] Broadcasting to topics ["budget:666"] via ....

Notification:

%Ash.Notifier.Notification{resource: ...
for your second / not working broadcast, to check if its a broadcast or handle info issue?
Failz
FailzOP•3mo ago
for the that works I see this:
Broadcasting to topics ["budget:078f18dd-2c0c-458f-be57-23408b1a2297"]
Broadcasting to topics ["budget:078f18dd-2c0c-458f-be57-23408b1a2297"]
and the one that does not:
Broadcasting to topics []
Broadcasting to topics []
hrm
schnady
schnady•3mo ago
oki, so the issue is the missing topic could you show more of the pubsub resource code? (the one not working)
Failz
FailzOP•3mo ago
pub_sub do
module AppWeb.Endpoint
prefix "budget"

publish_all :create, [:budget_id] do
transform fn notification ->
transfer = notification.data

case App.Finance.get_account(transfer.from_account_id, tenant: transfer.organization_id, authorize?: false) do
{:ok, account} ->
%{notification | data: Map.put(transfer, :budget_id, account.budget_id)}
{:error, error} ->
notification
end
end
end
end
pub_sub do
module AppWeb.Endpoint
prefix "budget"

publish_all :create, [:budget_id] do
transform fn notification ->
transfer = notification.data

case App.Finance.get_account(transfer.from_account_id, tenant: transfer.organization_id, authorize?: false) do
{:ok, account} ->
%{notification | data: Map.put(transfer, :budget_id, account.budget_id)}
{:error, error} ->
notification
end
end
end
end
let me remove the logging to make it easier to read there is no budget_id on the transfer resource, so I fetch the account which does have budget_id this logging statement happens before my pubsub transform logging statement: [debug] Broadcasting to topics [] via AppWeb.Endpoint.broadcast [info] Transfer PubSub: Processing CREATE for budget 078f18dd-2c0c-458f-be57-23408b1a2297
schnady
schnady•3mo ago
hm dont see anything strange you could try rewriting it to
pub_sub do
prefix "budget"
module AppWeb.Endpoint

publish :create, ["078f18dd-2c0c-458f-be57-23408b1a2297"]
end
pub_sub do
prefix "budget"
module AppWeb.Endpoint

publish :create, ["078f18dd-2c0c-458f-be57-23408b1a2297"]
end
just to check plain stupid if its the pubsub the broadcast should not be empty than
Failz
FailzOP•3mo ago
that works: [debug] Broadcasting to topics ["budget:078f18dd-2c0c-458f-be57-23408b1a2297"] via AppWeb.Endpoint.broadcast
schnady
schnady•3mo ago
so you have no budget_id on the resource creating the not working pubsub? not sure if this is an issue or the transform function but i think i would try debugging it stepwise
pub_sub do
prefix "budget"
module AppWeb.Endpoint

transform fn notification ->
... Transform func ....
end

publish :create, ["static key"]
end
pub_sub do
prefix "budget"
module AppWeb.Endpoint

transform fn notification ->
... Transform func ....
end

publish :create, ["static key"]
end
if its empty again, try without transform func if its working test your transform func via iex etc
Failz
FailzOP•3mo ago
i think it's because :budget_id does not exist on transfer if i switch it to a key that exists, then I see a topic broadcasted
schnady
schnady•3mo ago
you dont have to use it, it can be anything, just subscribe accordingly where you want to have a handle_info
Failz
FailzOP•3mo ago
aye, I think the issue is mostly with my architecture. i was trying to reduce the number of topics I subscribe to in the live view
schnady
schnady•3mo ago
if connected?(socket) do
"budget:#{budget_id}" |> IsdCrmWeb.Endpoint.subscribe()
"smth_different:#{someother_key}" |> IsdCrmWeb.Endpoint.subscribe()
end
if connected?(socket) do
"budget:#{budget_id}" |> IsdCrmWeb.Endpoint.subscribe()
"smth_different:#{someother_key}" |> IsdCrmWeb.Endpoint.subscribe()
end
ps: what also works is
pub_sub do
module AppWeb.Endpoint
prefix "budget"

publish_all :create, [:budget_id] do
transform & &1.data
end

publish_all :update, [:budget_id] do
transform & &1.data
end
end
pub_sub do
module AppWeb.Endpoint
prefix "budget"

publish_all :create, [:budget_id] do
transform & &1.data
end

publish_all :update, [:budget_id] do
transform & &1.data
end
end
to
pub_sub do
module AppWeb.Endpoint
prefix "budget"

transform & &1.data

publish :create, [:budget_id]
publish :update, [:budget_id]
end
pub_sub do
module AppWeb.Endpoint
prefix "budget"

transform & &1.data

publish :create, [:budget_id]
publish :update, [:budget_id]
end
Failz
FailzOP•3mo ago
ty both for the help this morning time to pack up and head to tennessee to pick up a new puppy 🥰
schnady
schnady•3mo ago
enjoy the weekend
Failz
FailzOP•3mo ago
No description
Failz
FailzOP•3mo ago
check out that floof ball!

Did you find this page helpful?