Phoenix framework & pass conn

I use the phoenix framework, so:

I have the following code in /web/static/js/socket.js

chatInput.on("keypress", event => { if (event.keyCode === 13) { channel.push("new_msg", {body: chatInput.val()}); //im want to pass @conn here chatInput.val("") } }); 

and at / web / channels / room_channel:

 use Phoenix.Channel defmodule fooBar do def handle_in("new_msg", %{"body" => body}, socket) do #and get conn here broadcast! socket, "new_msg", %{body: body} {:noreply, socket} end end 

I need to get a connection in room_channel. How to pass it to socket.js?

+5
source share
1 answer

Here is the solution for you. In your router .ex. You put a user token, for example:

 defp put_user_token(conn, _) do current_user = get_session(:current_user) user_id_token = Phoenix.Token.sign(conn, "user_id", current_user.id) conn |> assign(:user_id, user_id_token) end 

and then you can connect your pipeline:

 pipeline :browser do ... plug :put_user_token end 

and make sure you put the user token in your app.html.eex:

 <script>window.userToken = "<%= assigns[:user_id] %>"</script> 

Now you can check your socket in socket.js:

 let socket = new Socket("/socket", {params: {token: window.userToken}}) 

and in your user_socket.ex. You can assign your token using a socket:

 def connect(%{"token" => user_id_token}, socket) do case Phoenix.Token.verify(socket, "user_id", user_id_token, max_age: 1209600) do {:ok, id} -> {:ok, assign(socket, :user_id, id)} {:error, reason} -> reason end end 

Finally, in your channel:

 def handle_info(:after_join, socket) do push socket, "user_joined", %{message: "User #{socket.assigns.user_id} has joined" {:noreply, socket} end 
+5
source

All Articles