Phoenix.PubSub stopped working in LiveView

I am at bit of a loss here since this issue has come out of the blue and I have no idea what has happened.

I have a MQTT subscriber that publishes messages to Phoenix.PubSub. I know this works as I can subscribe to these messages in the iex console and manually preview the messages in the message box. I know for sure that the broadcasting part of the PubSub is firing and working.

To try and debug what is going on I created a very very simple LiveView below.

defmodule BrokerExWeb.HumidityLive do
  use BrokerExWeb, :live_view

  alias Phoenix.PubSub

  def mount(_, _, socket) do
    if connected?(socket) do
      PubSub.subscribe(BrokerEx.PubSub, BrokerEx.Workstation.Cutter.topic(1, 1))

    {:ok, socket |> assign(:humidity, 1)}

  def render(assigns) do
    <div><%= @humidity %></div>

I was expecting this to start receiving messages (and falling over because I haven’t defined a handle_info callback), however the process never receives anything.

Does anyone know what might be happening here?

OMG I am still hungover from the weekend.

Just defining the handle_info callback sorted it. For some reason I was waiting for it die because I hadn’t.

Please ignore me.

I don’t see where you wrote the broadcast call to send events to the subscriber.

Also, try Using LiveBook, instead of iex to connect to your running instance. It will make life easier, you won’t have to keep aliasing Phoenix.PubSub every time you restart. (Or write your setup code in .iex.exs.)

Thanks for the tip, I don’t suppose you have a link to a tutorial for how to set that up? Sounds like it’ll be really useful going forwards.

Sure, it’s simple.

  1. Just start your application by giving a name & cookie:
    elixir --sname derpycoder --cookie wubalubadubdub -S mix phx.server

  2. Open up a LiveBook

  3. Click on Runtime Icon on Sidebar:
    Runtime Configure

  4. Pass in the app name & cookie

  5. Connection Success
    Runtime Connected

  6. Now you can access anything, that you can access via iex!!

Read up on how you can create Business Intelligence LiveBooks, so you can query your runtime from this interactive tool.

P.S. Just pass short name and the LiveBook will auto deduce your system node name.
e.g. I passed derpycoder and it expanded to derpycoder@Abhijits-Mini


Old School method: .iex.exs

You can create a .iex.exs file at the root of the project and then paste in your code:

alias DerpyCoderWeb.Router.Helpers, as: Routes
alias DerpyCoder.Accounts
alias DerpyCoder.Repo

IO.puts("Started IEx")

defmodule Helpers do
  @moduledoc """
  Can be used to reduce typing in IEx.
  def init do

Later you can call this helper in iex:

Helpers.init() and it will work.

P.S. You don’t have to bother with this anymore, once you start using LiveBook though.

1 Like

Getting this error on the Application

Error in Livebook

What is your elixir version? I had this at one time, because the elixir version in my project was too old and as such did not have some of the functions that Livebook needs.