Hi, friends,
In my application, I have a channel that is responsible on sending and receiving messages from external topics (the client doesn’t need to “join” them explicitly).
I could achieve channel communication through broadcast_from and MyApp.Endpoint.broadcast_from, but… only when I join both topics (let’s say… my “core” channel and the secondary - external - topic).
My question is: is it possible to achieve channel communication (send and receive messages) with external topics without the client have to joining on them?
P.S. I’d already used MyApp.Endpoint.subscribe, without success. Here is what I’ve done 'til now:
defmodule MyApp.CommunicationChannel do
use MyAppWeb, :channel
def join("communication", _payload, socket) do
{:ok, socket}
end
def handle_in("join_external", %{"topic" => topic}, socket) do
MyApp.Endpoint.subscribe(topic)
{:noreply, socket}
end
def handle_in("send_to_external", %{"topic" => topic} = payload, socket) do
MyApp.Endpoint.broadcast_from! self(), topic, "receive_payload", payload
{:noreply, socket}
end
def handle_in("send_to_client", payload, socket) do
broadcast! socket, "send_to_client", payload
end
def handle_info(%Phoenix.Socket.Broadcast{topic: _, event: event, payload: payload}, socket) do
push socket, event, payload
{:noreply, topic}
end
end
defmodule MyApp.ReceiverChannel do
use MyAppWeb, :channel
def join("receive", _payload, socket) do
MyApp.Endpoint.subscribe("communication")
{:ok, socket}
end
def handle_in("receive_payload", payload, socket) do
MyApp.Endpoint.broadcast_from! self(), "communication", "send_to_client", payload
{:noreply, socket}
end
def handle_info(%Phoenix.Socket.Broadcast{topic: _, event: event, payload: payload}, socket) do
push socket, event, payload
{:noreply, topic}
end
end
What am I doing wrong? The communication between them only works when the client subscribe to both channels.
Thanks in advance!






















