Channel Session of the Hexdocs Phoenix is broken

It seem to be a recurrent flaw about Phoenix, but still not solved.

Here is the reference : https://hexdocs.pm/phoenix/channels.html

When I try to create the chat application in the Hexdocs, chapter “Tying it all together”, I get this message in my browser when I try to join the lobby room :

Firefox can’t establish a connection to the server at ws://localhost:4000/socket/websocket?token=undefined&vsn=2.0.0.

The terminal gave me this error :

[error] Ranch listener ChannelerWeb.Endpoint.HTTP had connection process started with :cowboy_clear:start_link/4 at #PID<0.541.0> exit with reason: {:undef, [{ChannelerWeb.RoomChannel, :child_spec, [{ChannelerWeb.Endpoint, {#PID<0.541.0>, #Reference<0.1539963333.2518417409.254495>}}], []}, {Phoenix.Channel.Server, :join, 4, [file: 'lib/phoenix/channel/server.ex', line: 25]}, {Phoenix.Socket, :handle_in, 4, [file: 'lib/phoenix/socket.ex', line: 617]}, {Phoenix.Endpoint.Cowboy2Handler, :websocket_handle, 2, [file: 'lib/phoenix/endpoint/cowboy2_handler.ex', line: 175]}, {:cowboy_websocket, :handler_call, 6, [file: '/home/michou/FUN/elixir/Labo/channeler/deps/cowboy/src/cowboy_websocket.erl', line: 528]}, {:cowboy_http, :loop, 1, [file: '/home/michou/FUN/elixir/Labo/channeler/deps/cowboy/src/cowboy_http.erl', line: 254]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}]}

After looking by myself, it look like the linter ALE from vim is at fault. I tried to patch it, but couldn’t find explanantion with enough details to do so. Each answer seem to assume a knowlegde of vi which I don’t have.

Can someone please explain me how to patch this problem. And tell me if we will still need to do so in the next Phoenix iteration ?

Many Thanks

How about we make sure there aren’t issues on your end before we assume there are issues with Phoenix eh?

What version of Elixir are you using? elixir -v should tell you.

2 Likes

Yeah, sorry, but when I saw it’s a recurring problem and messages with similar behavior are as old as Phoenix 1.4.0, I went overboard with the title. Thanks for pointing it out.

Anyway, here are the versions I use :
Erlang/OTP 22
Elixir 1.9.1
Phoenix 1.5.6

You can see in the error that the websocket URL contains a token with value undefined. This looks suspect, I would recommend investigating that first.

In particular, review the steps in the documentation section Using Token Authentication. Could it be that you forgot step 2, or have an error, or maybe miss the :user_token assign in step 1? If you execute window.userToken, in the browser console, what do you get?

Regarding the problem with Vim ALE, you probably refer to this. If so, first make sure you are actually experiencing that issue, because you could be seeing a different one. Remove the build artifacts with rm -rf _build and then restart the Phoenix server: if the problem disappears, you are probably experiencing this issue with ALE, otherwise, it’s a different one. If it ends up being ALE, you can disable the mix linter in your .vimrc (which is anyway quite annoying, rather setup elixir-ls instead).

In any case, the Phoenix team does not maintain the Vim plugin ALE, so if you are running into that issue, it is not a problem that the Phoenix maintainers can be held responsible of.

2 Likes

Many thanks for your detailed response. However, the session “Using token authentication” is way latter in the documentation, and I should see result before. The documentation say I should see : “Joined successfully” in my console instead of my error.

After rechecking every step of the documentation, I still couldn’t find any reason about why it failed. Even removing the build folder didn’t solve it, so I guess it was a bad pick. You were once again right to point it out.

The logs told me I connected to UserSocket with websocket without trouble, and the error came after. also, I don’t know if it could be linked, but when I uncomment the line

import socket from "./socket"

in my app.js, my ide indicate the value is never used.

When I tried to recreate the bug with another testProject, however, the issue were different, so I guess I lacked some rigor first time. I’m surprised the compiler didn’t point it out, tho.Now I can get my message “Joined successfully”, but I still get that error message about “can’t establish a connection to the server”.

It pop once or twice before the log about joining a channel. I’m still curious about why does the error pop, and twice moreover, but you taught me a lot. Thanks you for solving that issue.
I’ll also be cooler next time.

1 Like