Handling PubSub messages from within a LiveComponent?


I have a LiveView that subscribes to a Phoenix.PubSub topic and shows some logs in real time. Now I want to move this part into a stateful LiveComponent, so that all logic regarding subscribing and reacting to messages in the topic lies within it.

But, I think this can’t be done - I can subscribe to the topic from within the LiveComponent, but it’s the parent LiveView process who is going to receive the messages, so I still need to have the handle_info logic in the parent LV process.

Is there something similar to phx-target="@myself" that can be used for PubSub like it’s done for events, so I can achieve this?

1 Like

The parent LV will always handle the messages, and then you can use those PubSub event handlers to pass the relevant changes to the child component(s).


Thanks, that’s what i thought… it would have been great to be have a LiveComponent that could be embedded into different LiveViews without needing them to know how to handle those messages!