phtrivier

phtrivier

Handle phoenix socket.connect() error

I’m using phoenix 1.3.2

I have a socket that checks for user authentication at connection time:

def Socket do
   use Phoenix.Socket

  def connect(params, socket) do
     case check_authentication_params(params) do 
       {:ok, authed_socket} -> {:ok, authed_socket}
       _ -> :error 
    end
  end
   
end

I connect in a browser with the phoenix npm package:

import { Socket } from 'phoenix';

let webSocket = new Socket(`....`);

webSocket.onError(error => {
   // Handle error
});

webSocket.connect()

My problem is that, on the client side, in the webSocke.onError handler, the error event seems to be the same when:

  • the connect succeeds, but later the connection is dropped (momentarily, for example, when my server restarts)
  • the connection is done with wrong params, so the connect function returns :error. I have log line that says:
WebSocket connection to '....t?vsn=2.0.0' failed: Error during WebSocket handshake: Unexpected response code: 403

But I cannot make the difference in the code.

What I want to do is to forcefully disconnect the websocket (to avoid repeated reconnection attempt) when I know the client does not have the proper credentials.

If I always use webSocket.disconnect in the error handler, then, as I understand it, I loose automatic reconnection in case of a network hicup.

Is there a way around that ?

Where Next?

Popular in Questions Top

ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement