nesimtunc

nesimtunc

Socket Authentication with Phoenix v1.5.1

Hi,
I’m newbie to Elixir + Phoenix world. I’ve started an open source project to help COVID-19 pandemic if you live in a community, like a share-house.

In the app, I don’t have a user system, not needed, however I need to add device feature in order to be able to send notification later. So, I want to know which device is connected. I’m going to create a unique device id for each client on first call and set it in the cookie (I’ve have a plug for that, coding is going on).

For test purpose, to make the code easier, I created a separated branch along with some explanation in the comments here: https://github.com/rainlab-inc/cothings/tree/socket-authentication which is created from this: https://github.com/rainlab-inc/cothings/tree/upgrade-phoenix-version-to-1.5

I’ve been following latest documentations for:

However I’m not able to receive the token in user_socket module. I get a very common error like this:

Server: localhost:4000 (http)
Request: GET /socket/websocket?vsn=2.0.0
** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in ColivingWeb.UserSocket.connect/3
        (coliving 0.1.0) lib/coliving_web/channels/user_socket.ex:6: ColivingWeb.UserSocket.connect(%{"vsn" => "2.0.0"}, %Phoenix.Socket{assigns: %{}, channel: nil, channel_pid: nil, endpoint: ColivingWeb.Endpoint, handler: ColivingWeb.UserSocket, id: nil, join_ref: nil, joined: false, private: %{}, pubsub_server: ColivingWeb.PubSub, ref: nil, serializer: Phoenix.Socket.V2.JSONSerializer, topic: nil, transport: :websocket, transport_pid: nil}, %{})
        (phoenix 1.5.1) lib/phoenix/socket.ex:572: Phoenix.Socket.user_connect/4
        (phoenix 1.5.1) lib/phoenix/socket.ex:434: Phoenix.Socket.__connect__/3
        (phoenix 1.5.1) lib/phoenix/transports/websocket.ex:32: Phoenix.Transports.WebSocket.connect/4
        (phoenix 1.5.1) lib/phoenix/endpoint/cowboy2_handler.ex:31: Phoenix.Endpoint.Cowboy2Handler.init/4
        (cowboy 2.7.0) coliving/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
        (cowboy 2.7.0) coliving/deps/cowboy/src/cowboy_stream_h.erl:320: :cowboy_stream_h.execute/3
        (cowboy 2.7.0) coliving/deps/cowboy/src/cowboy_stream_h.erl:302: :cowboy_stream_h.request_process/3
        (stdlib 3.11.2) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
[error] #PID<0.523.0> running ColivingWeb.Endpoint (connection #PID<0.522.0>, stream id 1) terminated

But if I do the solution that I found online, especially discussions from here, then I don’t get an authenticated socket connection.

For example adding this function in the user_socket module:

def connect(_params, socket, _connect_info), do: {:ok, socket }

You may check the commit history from the branches or following the links for the codes:

Creating token with a hard coded device_uuid: https://github.com/rainlab-inc/cothings/blob/socket-authentication/lib/coliving_web/templates/layout/app.html.eex#L12

JS Socket Client: https://github.com/rainlab-inc/cothings/blob/socket-authentication/assets/js/socket.js

User_Socket module: https://github.com/rainlab-inc/cothings/blob/socket-authentication/lib/coliving_web/channels/user_socket.ex

What do I miss from the documentations?

Thanks in advance for your help!

Marked As Solved

sb8244

sb8244

Author of Real-Time Phoenix

You don’t need to put the params inside of the object with params key. You can simply pass in the token object directly and it will work.

connect({ token })

Also Liked

sb8244

sb8244

Author of Real-Time Phoenix

The socket connect function accepts the params as argument 1. The initializer doesn’t accept params as an option. If you make that change, it looks like it will work.

Great post and welcome, BTW. I appreciate you laying it all out and linking to the code.

nesimtunc

nesimtunc

yeah … the main problem was actually related to moment and webpack: https://github.com/moment/moment/issues/2979

and here’s how I solved: https://github.com/rainlab-inc/cothings/commit/b6ca751901ad6b3ab04e530966734724aa882498

due to much debugging logs (aggressive reconnection ) I wasn’t able to see the real problem. However your hint was the proper way.

Thanks a lot!

Where Next?

Popular in Questions Top

fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement