dfalling

dfalling

Tips for troubleshooting PubSub messages not received?

I’m trying to follow LiveBeat’s example of using PubSub to send messages to LiveViews. I’ve also read through a ton of topics here, so I’m not sure what I’m doing wrong…

I’m broadcasting on a PubSub topic that a LiveView is subscribed to. I think the topic and everything is wired up correctly, but the LiveView never sees the message (or shows a failure for not handling it properly.)

Here’s my PubSub setup:

defmodule Ido.Trips.PubSub do
  @moduledoc """
  PubSub for trips
  """

  # defined in application.ex
  @pubsub Ido.PubSub

  def broadcast_email_processed(element) do
    msg = %Ido.Trips.Events.EmailProcessed{element: element}
    broadcast!(element.trip_id, msg)
  end

  defp broadcast!(trip_id, msg) do
    Phoenix.PubSub.broadcast!(@pubsub, topic(trip_id), {__MODULE__, msg})
  end

  def subscribe_to_trip_id(trip_id) do
    Phoenix.PubSub.subscribe(@pubsub, topic(trip_id))
  end

  def unsubscribe_to_trip_id(trip_id) do
    Phoenix.PubSub.unsubscribe(@pubsub, topic(trip_id))
  end

  defp topic(trip_id) do
    "trip:#{trip_id}"
  end
end

I’m subscribing to it in a handle_params in my LiveView, not mount because I need the id of the Trip:

@impl true
  def handle_params(%{"id" => id} = params, _, socket) do
    if connected?(socket) do
      # make sure no duplicates
      Ido.Trips.PubSub.unsubscribe_to_trip_id(id)

      Ido.Trips.PubSub.subscribe_to_trip_id(id)
    end
...

@impl true
  def handle_info(
        {Ido.Trips.PubSub, %Ido.Trips.Events.EmailProcessed{element: element}},
        socket
      ) do
        {:noreply, put_flash(socket, :info, "An element was processed from email")}
  end

What did I mess up? I don’t have any warnings/errors in the console during startup or when I broadcast the message.

Thanks!

Marked As Solved

dfalling

dfalling

Ooof, I’m an idiot. So I was testing by running mix phx.server in one shell, and iex -S mix, and I somehow thought those would be the same app.

I just ran an iex -S mix phx.server, and then in that same REPL triggered some broadcasts and it worked perfectly.

Thank you all so much!

Also Liked

thomas.fortes

thomas.fortes

First thing I would check is if a generic handle_info(msg, socket) catch something in the msg.

sodapopcan

sodapopcan

Subscribing in handle_params could prove to be problematic. It’s hard to see without more context—is your LiveView handling both “index” and “show” pages? If not, then you can still get the id from def mount(%{"id" => id}, _session, socket). If they are sharing a LiveView, then as you have it you aren’t unsubscribing from the previous trip before subscribing to the new one which means they would pile up. handle_params get called any time there is a patch event. This applies to things like query params or subroutes that may potentially point to the same LiveView. This means that on any patch to the same trip it will unsubscribe and resubscribe. I’m not actually sure what effect that would have, but I wouldn’t say it’s the best place to put it. Possibly there is a sane way to do so but it’s not something I’ve ever seen before and I feel like it could lead to confusing bugs.

sodapopcan

sodapopcan

Ah, so is your problem that you are patching when switching between trips and that’s why you need handle_params? Otherwise, params is the first parameter given to mount so you should definitely be able to get the id from there.

You could also subscribe to a general trips channel and see if it’s the current one or not:

handle_info({:trip_updated, trip}, socket)
    when trip.id == socket.assigns.trip.id do
  # ...
  {:noreply, socket}
end

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement