mtrudel/bandit

Phoenix Presence error

mvkvc opened this issue · 2 comments

Hello!

I created my Phoenix project with Bandit and then went to add a live cursor view for all connected users based on: https://github.com/dwyl/phoenix-liveview-realtime-cursor-tracking-tutorial. I kept getting an error similar to:

[error] Bandit.HTTP1.Handler #PID<0.840.0> received unexpected message in handle_info/2: %Phoenix.Socket.Broadcast{
  topic: "cursor_page",
  event: "presence_diff",
  payload: %{
    joins: %{
      "phx-F513SuoAeN-YDAOh" => %{
        metas: [
          %{
            y: 61.096605744125334,
            x: 46.808510638297875,
            color: "#09D7EA",
            username: "amadeus-blue",
            socket_id: "phx-F513SuoAeN-YDAOh",
            phx_ref: "F513S2Ctn_DpYgOD",
            phx_ref_prev: "F513S138wRzpYgNj"
          }
        ]
      }
    },
    leaves: %{
      "phx-F513SuoAeN-YDAOh" => %{
        metas: [
          %{
            y: 49.60835509138381,
            x: 53.191489361702125,
            color: "#09D7EA",
            username: "amadeus-blue",
            socket_id: "phx-F513SuoAeN-YDAOh",
            phx_ref: "F513S138wRzpYgNj",
            phx_ref_prev: "F513S1z-OyHpYgND"
          }
        ]
      }
    }
  }
}

I kept thinking I did something wrong and spent a lot of time trying to debug but then swapped out Bandit for Cowboy and it works as intended. Maybe it is a bug or a known difference in implementation but want to share in case anyone else gets bit by this.

There is a repro here: https://github.com/mvkvc/bandit_presence_repro, you can run it and just comment out https://github.com/mvkvc/bandit_presence_repro/blob/d050f4a1de373afa1db6416e58edbec98b2298ff/config/config.exs#L12 (the Bandit adapter in config.exs) and it works as intended.

Happy to contribute to a fix it is turns out to be a bug of some sort.

Thank you

I believe this is a dupe of #84, #92, #141, #218 and #264. #141 has the best description of what's going on, but the issue is that your mount code needs to discriminate on the value of connected? since LiveView calls this function at two places in the connection lifecycle. I assume that you're calling the relevant subscribe function from within an unconnected liveview initially, so self() is going to be the Bandit handler process, even after the liveview life cycle completes.

In point of fact, the issue is still happening with Cowboy, but because Cowboy silently eats errors it's just not being surfaced to you.

Closing this issue for hygiene. If the above is not in fact the case, please feel free to reopen it!

My apologies and thank you for explaining. I know I tried searching for it first but maybe I was too specific. Made the fix to check for connected? and works as intended.