Presence in LiveView: `ERROR: 1st argument: the table identifier does not refer to an existing ETS table`

I am trying to use the Phoenix.Presence behaviour in my LiveView. I intend to use it to track a channel topic on the LiveSocket, rather than initiating a new socket connection (i.e. UserSocket). There are several online resources covering this topic (e.g. Phoenix Presence with Phoenix LiveView - Tutorials and screencasts for Elixir, Phoenix and LiveView), but I keep running into an error.

The error in a nutshell: 1st argument: the table identifier does not refer to an existing ETS table. I believe this ‘1st argument’ refers to self() in:

      Presence.track(
        self(),
       "story",
        socket.assigns.current_user.id,
        %{}
      )

I have been looking around for solutions and trying to debug the code, but the error remains. I would like to understand what the issue is.

Below all the relevant source code and error messaging.

defmodule TodayCostoryWeb.Presence do
  use Phoenix.Presence, otp_app: :today_costory,
                        pubsub_server: TodayCostory.PubSub
end

defmodule TodayCostory.Application do
  use Application

  @impl true
  def start(_type, _args) do
    children = [
      ...
      {Phoenix.PubSub, name: TodayCostory.PubSub},
      TodayCostoryWeb.Presence,
      TodayCostoryWeb.Endpoint
    ]

    opts = [strategy: :one_for_one, name: TodayCostory.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

defmodule TodayCostoryWeb.StoryLive do
  use TodayCostoryWeb, :live_view
  alias TodayCostoryWeb.Presence
  ...

  def mount(_params, session, socket) do
    socket =
      assign_new(
        socket,
        :current_user,
        fn ->
          Accounts.get_user_by_session_token(session["user_token"])
        end
      )

    if connected?(socket) do
      Presence.track(
        self(),
       "story",
        socket.assigns.current_user.id,
        %{}
      )
      TodayCostoryWeb.Endpoint.subscribe("story")
    end

    ...

    {:ok, socket}
  end

[error] GenServer #PID<0.20718.0> terminating
** (ArgumentError) errors were found at the given arguments:

  * 1st argument: the table identifier does not refer to an existing ETS table

    (stdlib 3.17.1) :ets.lookup(TodayCostoryWeb.Presence, :pool_size)
    (phoenix_pubsub 2.1.1) lib/phoenix/tracker.ex:314: Phoenix.Tracker.pool_size/1
    (phoenix_pubsub 2.1.1) lib/phoenix/tracker.ex:134: Phoenix.Tracker.track/5
    (today_costory 0.1.0) lib/today_costory_web/live/story_live.ex:27: TodayCostoryWeb.StoryLive.mount/3
    (phoenix_live_view 0.17.11) lib/phoenix_live_view/utils.ex:301: anonymous fn/6 in Phoenix.LiveView.Utils.maybe_call_live_view_mount!/5
    (telemetry 1.1.0) /Users/macbookpro2021/Documents/dev/today_costory/deps/telemetry/src/telemetry.erl:320: :telemetry.span/3
    (phoenix_live_view 0.17.11) lib/phoenix_live_view/channel.ex:1001: Phoenix.LiveView.Channel.verified_mount/8
    (phoenix_live_view 0.17.11) lib/phoenix_live_view/channel.ex:59: Phoenix.LiveView.Channel.handle_info/2
    (stdlib 3.17.1) gen_server.erl:695: :gen_server.try_dispatch/4
    (stdlib 3.17.1) gen_server.erl:771: :gen_server.handle_msg/6
    (stdlib 3.17.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: {Phoenix.Channel, %{"params" => %{"_csrf_token" => "IhU7Am8bOgAoPxABEhstPCURMnkEGgN5AAtP9UMemjDxpQbrGzPI5J25", "_mounts" => 0, "_track_static" => ["http://localhost:4000/assets/app.css", "http://localhost:4000/assets/app.js"]}, "session" => "SFMyNTY.g2gDaAJhBXQAAAAIZAACaWRtAAAAFHBoeC1GeElZMkZVZE9GVng4M1RuZAAMbGl2ZV9zZXNzaW9uaAJkAAdkZWZhdWx0bggAhhhAP7ANEhdkAApwYXJlbnRfcGlkZAADbmlsZAAIcm9vdF9waWRkAANuaWxkAAlyb290X3ZpZXdkACBFbGl4aXIuVG9kYXlDb3N0b3J5V2ViLlN0b3J5TGl2ZWQABnJvdXRlcmQAHUVsaXhpci5Ub2RheUNvc3RvcnlXZWIuUm91dGVyZAAHc2Vzc2lvbnQAAAAAZAAEdmlld2QAIEVsaXhpci5Ub2RheUNvc3RvcnlXZWIuU3RvcnlMaXZlbgYAVK7dD4MBYgABUYA.cGrahkmk2vmiMJkMvsmJaMN21ic8YHPR_DiblXj6QrU", "static" => "SFMyNTY.g2gDaAJhBXQAAAADZAAKYXNzaWduX25ld2wAAAABZAAMY3VycmVudF91c2VyamQABWZsYXNodAAAAABkAAJpZG0AAAAUcGh4LUZ4SVkyRlVkT0ZWeDgzVG5uBgBUrt0PgwFiAAFRgA.xlF3G3iTjebpn47eOjRIqvj9YPL2nRKS1sX1qlvl1aY", "url" => "http://localhost:4000/"}, {#PID<0.20712.0>, #Reference<0.2939426857.1123287043.49659>}, %Phoenix.Socket{assigns: %{}, channel: Phoenix.LiveView.Channel, channel_pid: nil, endpoint: TodayCostoryWeb.Endpoint, handler: Phoenix.LiveView.Socket, id: "users_sessions:i0LCd7tjBpn7KYtPTQtTT_eJmxSlFOVAB6pG_SIV0vU=", join_ref: "4", joined: false, private: %{connect_info: %{session: %{"_csrf_token" => "cTORVNweEUTybJONbkb01P1L", "live_socket_id" => "users_sessions:i0LCd7tjBpn7KYtPTQtTT_eJmxSlFOVAB6pG_SIV0vU=", "user_token" => <<139, 66, 194, 119, 187, 99, 6, 153, 251, 41, 139, 79, 77, 11, 83, 79, 247, 137, 155, 20, 165, 20, 229, 64, 7, 170, 70, 253, 34, 21, 210, 245>>}}}, pubsub_server: TodayCostory.PubSub, ref: nil, serializer: Phoenix.Socket.V2.JSONSerializer, topic: "lv:phx-FxIY2FUdOFVx83Tn", transport: :websocket, transport_pid: #PID<0.20712.0>}}
State: #Reference<0.2939426857.1123287043.49661>
[error] an exception was raised:
    ** (ArgumentError) errors were found at the given arguments:

  * 1st argument: the table identifier does not refer to an existing ETS table

        (stdlib 3.17.1) :ets.lookup(TodayCostoryWeb.Presence, :pool_size)
        (phoenix_pubsub 2.1.1) lib/phoenix/tracker.ex:314: Phoenix.Tracker.pool_size/1
        (phoenix_pubsub 2.1.1) lib/phoenix/tracker.ex:134: Phoenix.Tracker.track/5
        (today_costory 0.1.0) lib/today_costory_web/live/story_live.ex:27: TodayCostoryWeb.StoryLive.mount/3
        (phoenix_live_view 0.17.11) lib/phoenix_live_view/utils.ex:301: anonymous fn/6 in Phoenix.LiveView.Utils.maybe_call_live_view_mount!/5
        (telemetry 1.1.0) /Users/macbookpro2021/Documents/dev/today_costory/deps/telemetry/src/telemetry.erl:320: :telemetry.span/3
        (phoenix_live_view 0.17.11) lib/phoenix_live_view/channel.ex:1001: Phoenix.LiveView.Channel.verified_mount/8
        (phoenix_live_view 0.17.11) lib/phoenix_live_view/channel.ex:59: Phoenix.LiveView.Channel.handle_info/2
        (stdlib 3.17.1) gen_server.erl:695: :gen_server.try_dispatch/4
        (stdlib 3.17.1) gen_server.erl:771: :gen_server.handle_msg/6
        (stdlib 3.17.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3

The “1st argument” here refers to the first argument being passed to :ets.lookup inside the implementation of Tracker:

I’m not sure how this could go wrong, given that the table is created a handful of lines above in Tracker.init :thinking:

1 Like

That’s already good to know. Wasn’t sure how to read the error message, tbh.

I’d double check the order in which you start or stop things. This very much sounds like you’re trying to track something before or after presence was started.

The strange thing is, however, that there aren’t many things I can change the order of. At least, not the parts I wrote. By far must of all the code is generated with phx.gen or is naturally part of LiveView. It’s close to a fresh set of phx.new scaffolds.

And I have tried a bunch of alterations already. But, nevertheless, I’ll check again. Ty.

Have you tried uncommenting the subscription/tracking code, just to be sure those instances are the ones causing the issues?

Yes and the problem disappears.

@LostKobrakai @al2o3cr

Thank you for your replies.

It ended up being the most ‘are you sure it’s plugged into the wall?’ type of situations. I forgot to restart the server. It works now.

Hope this thread saves somebody in the future some time.

Hey i am running with the exact same issue. I was starting the Endpoint before my presence module and thought it was the problem, but changing the order (to start first Presence) as stated in the PubSub documentation does not solve it either. This happens every time the application starts so it has already ‘restarted’ multiple times… do you know what can be wrong?