How to prevent Phoenix from retrying to connect to channel when auth fails?


When a user clicks on a conversation, this client-side script is run:

let convchannel ="conv:"+convid+"", {});
  .receive("ok", resp => { console.log("Joined conversation successfully", resp) })
  .receive("error", resp => { console.log("Unable to join conversation", resp) })

On the server side, I have this function:

  def join("conv:" <> convid, _message, socket) do
    if authorized?(convid, socket) do
      IO.inspect("successfully authorized to join conversation " <> convid)
      {:ok, socket}
      IO.inspect("not authorized to join room")
      {:error, "noauth"}

When the user is not authorized to join the room, I’m noticing that the browser continues to retry joining the room every few seconds, and that continuously calls my database in the authorized? function. Is there a way to prevent the browser from retrying to connect to that room, specifically if the error response shows noauth? However, if there is some kind of network/server issue going on, I’d still want the browser to continue retrying of course.

The socket is also connected to a lobby and user channel, so I can’t just kill the whole socket on error.


Here’s a related older post that discusses some possible solutions.

1 Like

let channel =, data);
.receive(“ok”, (resp) => {
.receive(“error”, (resp) => {
channel = null;

1 Like