Why am I not getting PubSub messages in production?

Hi,

I am trying out gigalixir and so far so good, it was easy to get an application deployed and working with SSL, however when I added a feature to my LiveView to receive PubSub events, it works on my local machine but not on the gigalixir server.
I assume it is something in my config/prod.exs but I am not sure what it could be, I duplicated the pubsub_server key from config/config.exs to make sure that was present, I set server: true, and tried binding to 0.0.0.0 using the http key but nothing seems to work.
There is no error on PubSub.subscribe or PubSub.broadcast, and no unhandled messages, they simply don’t come in as they do in my local machine.
I print the PubSub.node_name and it shows up as nonode@nohost on my dev machine and that app name@ip on the production machine, but I am not sure how to use that info or what else to check.
I also set MIX_ENV to prod on my machine and it works, so it must be something other than a config issue.
Any ideas on how to diagnose this? thanks!

Is this a single server or a cluster of servers? How are the PubSub messages being broadcast / from where?

Hi, this is just a simple application running on one node, nothing fancy.
It is a simple air traffic simulator that uses GenServers for each aircraft, which publish their state with this function:

defp publish_state(state) do
    IO.puts("#{state.call_sign} publishing state")

    :ok = PubSub.broadcast(
      Mkrandio.PubSub,
      "airplane",
      {:airplane_state, %{:call_sign => state.call_sign, :altitude => state.altitude,
      :takeoff_airport => state.takeoff_airport, :takeoff_city => state.takeoff_city}}
    )
  end

The PubSub service is started like so in application.ex, before the endpoint and before the simulator:

 # Start the PubSub system
      {Phoenix.PubSub, name: Mkrandio.PubSub},

On the server I can see the log message that the messages are being broadcast, no errors are being logged

2022-01-04T19:00:39.150545+00:00 mkrandio[b’mkrandio-7bc94966b4-gdt7m’]: web.1 | 94a47 publishing state
2022-01-04T19:00:39.150560+00:00 mkrandio[b’mkrandio-7bc94966b4-gdt7m’]: web.1 | 96748 publishing state

The receiver is a LiveView, which subscribes on mount:

:ok = PubSub.subscribe(Mkrandio.PubSub, "airplane")

And implements this callback, which works fine on my machine but does not get called on the production server, and as I mentioned even works locally when MIX_ENV=prod, so I don’t think it’s an endpoint config issue.

def handle_info({:airplane_state, plane}, socket) do
    IO.puts("Live View received")
    IO.inspect(plane)
    # find and replace plane in list otherwise insert
    socket = replace_plane(socket, plane)
    {:noreply, socket}
  end

I hope you’ll forgive what seems like an obvious question. If you look at your network inspector, is the WebSocket connecting?

LiveView can trip people up sometimes because it mounts via HTTP and goes live via a second WebSocket connection. In production, it could be misconfigured such that the WS connection isn’t happening.

1 Like

ooh no I didn’t think to check that, I know it was mounting and subscribing but I didn’t think to check connected status. The app is broken elsewhere at the moment so I can’t check right now but will post here as soon as I can get back to that, thanks very much for the insight

1 Like

Ok it was even more basic than that, it’s not loading assets/app.js, so the issue isn’t even with PubSub, it’s something in the deployment process, thank you very much, i will need to remember to have devtools open before blaming Elixir!

3 Likes

By the way I just bought your book, it looks like just what I needed, I’m excited to start reading it, thanks in advance!

Ok I’ve only been reading it for 10 minutes and it already paid for itself with the Copy as Curl tip, that would have taken me a million years to figure out.

Real Time Phoenix, people, buy it.

2 Likes