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

Greetings,

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

let convchannel = socket.channel("conv:"+convid+"", {});
convchannel.join()
  .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}
    else
      IO.inspect("not authorized to join room")
      {:error, "noauth"}
    end
  end

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.

Thanks!

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

1 Like

let channel = socket.channel(channelName, data);
channel.join()
.receive(“ok”, (resp) => {
//
})
.receive(“error”, (resp) => {
channel.leave();
channel = null;
//
});

1 Like