anandgeorge
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.
Marked As Solved
Sebb
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.
Also Liked
dnsbty
I have a tutorial that shows you how to get PubSub and LiveView working together here: https://dennisbeatty.com/how-to-create-a-todo-list-with-phoenix-liveview/
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.
derek-zhou
You need to make sure the subscribe is protected by connected?/1 as described here:
[Phoenix.LiveView — Phoenix LiveView v1.2.5]
Use
connected?/1to conditionally perform stateful work, such as subscribing to pubsub topics, sending messages, etc.
anandgeorge
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








