Enuzo
What is the right way to broadcast a message with PubSub and receive with Channel
I have an Elixir 1.54 and Phoenix umbrella app containing:
[AdminWeb]
[Admin]
[DisplayWeb]
The [AdminWeb] calls a function defined in [Admin] that pulls information from a
Database, then converts it to a list of maps and should broadcast it to DisplayWeb channel:
[AdminWeb] → [Admin] → [DisplayWeb]
The code in [Admin] looks like this:
# news.ex
def news_sort(id_list) do
news_payload = news_items_from_db() |> struct_to_map()
PubSub.broadcast(Admin.PubSub, "cast:news", news_payload)
end
The code in [DisplayWeb] looks like this:
#news_channel.ex
def join("cast:news", _payload, socket) do
self() |> send({:after_join, "cast:news"})
{:ok,socket}
end
def handle_info({:after_join, "cast:news"}, socket) do
broadcast!(socket, "newscast", %{data: news_payload})
{:noreply, socket}
end
The PubSub.broadcast in [Admin] returns :ok message but the [DisplayWeb] does not receive any message. It neither works not returns any error message.
Please, what’s wrong with the code? What’s the right way to implement this solution?
Most Liked
satom99
You must use Endpoint.broadcast/3 instead. Where Endpoint in this case is your application’s endpoint.
If you have a look on the source code for this broadcast/3 function I mentioned, you’ll notice it ends up calling PubSub.broadcast/4 too. But the catch here is that it is broadcasting a %Phoenix.Socket.Broadcast{} struct instead of plain data like you are doing. And this struct is most likely the language phoenix channels speak internally. Which, in other words, means that you are broadcasting a message that channels cannot understand. And so this explains why your clients receive no message.
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










