Deathslice246

Deathslice246

Always get that my join/3 function is undefined(join crash). What am I missing?

I have these two channels: UserChannel and PrivateChannel. UserChannel is the user’s own lobby and an example of joining that channel is user:3 where 3 is the user’s id. An example of joining private channel is private:3:23 where 3 is the id of the first user and 23 is the id of the second user.

The series of events that a user would take is as follows: In the client side when the user login, I create the socket and send in the user’s id as the socket’s param and try to connect to the UserChannel; this occurs successfully. When the user wants to initiate a private chat, they search by name and a series of profiles appear. Once they click on it, a function called startPrivateChat fires and calls the connectToPrivateChat defined in my chat.js. What happens is that I get a join crash because my join/3 function in App.PrivateChannel is undefined.

Now in the phoenix documentation and from multiple other sources, they say that all I need is one socket and I can use that socket to multiplex to different channels. So what I did is create a chat.js service that references things like the socket, the two channels(this.user_channel and this.private_channel) and another methods like connectToSocket, connectToUserChannel, connectToPrivateChannel etc. So why is this happening and what exactly am I doing wrong?

P.S - I’m using Phoenix 1.2 and Elixir 1.6.5

user_socket.ex

defmodule AppChat.UserSocket do
 use Phoenix.Socket

 channel "user:*", AppChat.UserChannel
 channel "private:*", AppChat.PrivateChannel

 transport :websocket, Phoenix.Transports.WebSocket

 def connect(%{ "user_id" => id }, socket) do
   current_user = AppChat.Repo.get(AppChat.User, String.to_integer(id))
   {:ok, assign(socket, :current_user, current_user)}
 end

 def id(socket), do: "user_socket:#{socket.assigns.current_user.id}"
end

user_channel.ex

defmodule AppChat.UserChannel do
  use AppChat.Web, :channel
  require Logger

  def join("user:" <> user_id, _payload, socket) do
    if socket.assigns.current_user.id == String.to_integer(user_id) do
      {:ok, socket}
    else
      {:error, %{reason: "unauthorized"}}
    end
  end
end

private_channel.ex

defmodule AppChat.PrivateChannel do
  use AppChat.Web, :channel
  require Logger

  def join("private:" <> users_string, _payload, socket) do
    if users_string |> String.split(":") |> Enum.member?(socket.assigns.current_user.id) do
	{:ok, %{}, socket}
    else
	{:error, %{reason: "unauthorized"}}
    end
  end
end

Most Liked

kokolegorille

kokolegorille

This will never be true, because id is an Int, while the list contains Strings.

It would be nice to see the join crash error log. I guess it might be js crashing, not channels crashing.

chrismccord

chrismccord

Creator of Phoenix

Phoenix 1.2 vs 1.3 isn’t a problem here. While we added AppWeb naming, it’s just a module name. What the elixir stack trace is telling us is that AppChat.PrivateChannel does not exist. From your provided snippets, the user socket channel matches the defmodule AppChat.PrivateChannel, but is it possible you renamed these in your example? Can you include your, current, exact, user_socket.ex and private_channel.ex, along with a current elixir stack trace? Thanks!

Deathslice246

Deathslice246

Ok, I’ll make an example on github and post it here shortly.

Where Next?

Popular in Questions Top

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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement