Loustak

Loustak

Error during WebSocket handshake: Unexpected response code: 403

Hello,

I’m trying to use channels and sockets on phoenix.
Here is what I have to handle sockets:

  channel "discussion:*", Flux.DiscussionChannel

  transport :websocket, Phoenix.Transports.WebSocket

  def connect(%{"token" => token}, socket) do
case Flux.Guardian.decode_and_verify(token) do
  {:ok, claims} ->
    case Flux.Guardian.resource_from_token(token, claims["sub"]) do
      {:ok, user} ->
        {:ok, assign(socket, :current_user, user)}
      {:error, _reason} ->
        :error
    end
  {:error, _reason} ->
    :error
end
  end

  def connect(_params, _socket), do: :error

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

And here is my channel:

  def join("discussion:" <> discussion_id, _params, socket) do
    discussion = Repo.get_by(Flux.Discussion, id: discussion_id)

    response = %{
      discussion: Phoenix.View.render_one(discussion, Flux.Discussion, "read.json"),
    }

    {:ok, response, assign(socket, :discussion, discussion)}
  end

  def terminate(_reason, socket) do
    {:ok, socket}
  end
end

But, when I’m connecting from the front in local I keep getting an error 403: Forbidden.
I get no errors messages on the server terminal. It looks like websockets are not activated there. I double check the socket endpoint of the server and it must be good. I don’t understand.

Thanks for help.

Marked As Solved

Loustak

Loustak

Ok I found the problem, the socket request sent from my front end was not matching the socket connect fonction of my back end

Where Next?

Popular in Questions Top

New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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 31525 112
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement