Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to web socket in Phoenix: Ignoring unmatched topic. But I think it matches

I'm new to Phoenix and web sockets. I'm trying to create an app with Web Sockets, but Phoenix says Ignoring unmatched topic "analysis:dn-ds.axin2" in Protsci.UserSocket. I think I have the appropriate handlers for this kind of topic, so I do not understand where's the issue. Have I missed something in the Phoenix app? Or can it be on the client?. Here is my setup:

# web/ |
# -----| user_socket.ex
# -----| analysis_channel.ex

# user_socket.ex
defmodule Protsci.UserSocket do
  use Phoenix.Socket

  channel "analysis:*", Protsci.AnalysisChannel

  transport :websocket, Phoenix.Transports.WebSocket

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

  def id(_socket), do: nil
end

# analysis_channel.ex
defmodule Protsci.AnalysisChannel do
  use Phoenix.Channel
  require Logger

  def join("analysis:" <> topic, _message, socket) do
    Logger.info "Join"
    case String.split(topic, ".") do
      [analysis, protein] ->
        reply = %{ :received => :message, :analysis => analysis, :protein => protein }
        {:ok, reply, socket}
      _ ->
        reply = %{ :error => "Unmatched topic", :msg => :topic }
        {:ok, reply, socket}
    end

  end

  def handle_in(
    analysis,
    %{"x" => x, "y" => y, "z" => z},
    socket
    ) do
    Logger.info "in"
    broadcast! socket, :analysis, %{body: x + y + z}
    {:noreply, socket}
  end

  def handle_in("new:msg", _msg, socket) do
    broadcast! socket, :type, %{body: "Unknow message type"}
    {:noreply, socket}
  end

  def handle_in("analysis:" <> topic, _msg, socket) do
    broadcast! socket, topic, %{body: "Unknow message type"}
    {:noreply, socket}
  end

end
like image 551
Maxim Krabov Avatar asked Aug 05 '18 16:08

Maxim Krabov


1 Answers

The issue was due to me not joining the channel before sending messages.

like image 122
Maxim Krabov Avatar answered Nov 01 '22 01:11

Maxim Krabov