How to manage notifications
I am trying to understand how to listen to notifications emitted from my resource actions and pass it down to a React client using Channels.
Currently this is what I've got:
I am able to successfully join to my channel from my React client. But I get this error when a new event is triggered from my resource:
The error is gone when I transform the notification
But I am still unsure of what is going on in here. How is the notification being sent to the client? I tried to inspect on the
handle_out/2
callback:
But nothing seems to be happening thereSolution:Jump to solution
Okay, figured it out thanks to the realworld-phoenix-inertia-react project.
Turns out I shouldn't have created a channel to connect from my client with the same topic the resources emit the events to. They should be different — that's what was causing the weird behavior and the "ghost" and uncontrolled sending of data through my channel.
Now my action is publishing to the topic
notes
instead of workspace
...4 Replies
Hm...try not matching on a specific event name to get all outgoing messages?
But the way you're doing it is generally right, to encore it in some way either in your channel or in the resource
As for how it's getting to the channel that is mostly Phoenixs domain. We just call YourMod.broadcast(...)
Did it, and didn't work until I subscribed in the join callback
Now I am able to see the transformed notification printed to the terminal by the
handle_out/3
callback
Still, this is very strange, this feels like it is just working, but why was it throwing a serialization error before, like it was being sent to the client without explicitly subscribing?The fact that it was failing in
create_note
( the bread crumbs in the error) makes me think that it wasn't in any kind of receive or anything. Maybe a red herring? If it was happening in the client or in the channel/socket it wouldn't have any idea of what action triggered itSolution
Okay, figured it out thanks to the realworld-phoenix-inertia-react project.
Turns out I shouldn't have created a channel to connect from my client with the same topic the resources emit the events to. They should be different — that's what was causing the weird behavior and the "ghost" and uncontrolled sending of data through my channel.
Now my action is publishing to the topic
notes
instead of workspace
I am subscribing to that topic from within the join/3
channel callback
And listening for events in handle_info/2
callbacks
This works!