LiveView server as subscriber to a PubSub service

Hi,

I have an implementation in which the backend is in two parts. One handles business logic and the other is a LiveView server.
The Business Logic publishes data at regular intervals over PubSub to multiple consumers including LiveView.
How do I set up my LiveView server to subscribe to this data.
I have evaluated handle_info in LiveView but it seems to be designed for sending updates to connected clients, not to handle subscription from another service.

Thanks.

I have a tutorial that shows you how to get PubSub and LiveView working together here: How to Create a Todo List with Phoenix LiveView | Dennis Beatty

But if you’ve already got the handle_info callback in place, you’ll probably just need to call Phoenix.PubSub.subscribe/2 in your live view’s init callback.

2 Likes

Hi,

Thanks for taking the trouble to answer and sharing the link. However it does not seem to help.

Just to give further context. It’s an umbrella app with a publisher, subscriber (for business logic), liveview.

This is the process I’ve followed.

Got the publisher and subscriber working over PubSub.

Copied the Pubsub.subscribe function to the mount implementation in LiveView.

Copied the handle_info implementation in PubSub just after the handle_events in the liveview app.

One thing I noticed that handle_info in pubsub takes state as the second argument while handle_info in liveview takes socket as the second argument. Tried both combinations but they don’t seem to talk.

Also checked observer and there is no link between the PubSub subscription process and the LiveView subscriber process.

Thanks.

When the live-view process subscribes to a topic (eg in mount) it should receive messages broadcasted to that topic in handle_info. handle_info in the live view process gets the live-socket as second argument.

Try a minimal example, if that still not works post it here.

You need to make sure the subscribe is protected by connected?/1 as described here:
[Phoenix.LiveView — Phoenix LiveView v0.20.2]

Use connected?/1 to conditionally perform stateful work, such as subscribing to pubsub topics, sending messages, etc.

1 Like

Thanks. It works now.

Could you please tell what you changed, could be useful for other people finding this topic with similar problems.

The "not-connected-subscribe’ would just be useless then, but it would still subscribe connected (ie in the live-view process) and work, right?

Sure. The issue was more conceptual.

Coming from a Javascript / Nodejs background I presumed that LiveView works like a server. So when I run iex -S mix phx.server it starts up the server, runs mount and configures it’s responses based on implementations of handle_event, handle_info etc. I was hoping to broadcast messages to clients that then connected. In short the server runs before a connection is made. I was therefore watching out for the subscription to run and get a log of the messages, which obviously didn’t happen.

As I can see LiveView spins up a process for each connection. Mount doesn’t run and therefore subscription doesn’t initiate till the client connects.

Once connected the subscription was initiated. There was a small issue with implementation of handle_info which logged an error. Corrected that and now it works.

Thanks once again.

1 Like