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
The issue was due to me not joining the channel before sending messages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With