I have channels working perfectly based on the docs, and my application is receiving and re-broadcasting messages as expected:
Here's the code that handles the channel:
defmodule HelloWeb.RoomChannel do
use Phoenix.Channel
def join("room:lobby", _message, socket) do
{:ok, socket}
end
def join("room:" <> _private_room_id, _params, _socket) do
{:error, %{reason: "unauthorized"}}
end
def handle_in("new_msg", %{"body" => body}, socket) do
broadcast!(socket, "new_msg", %{body: body})
{:noreply, socket}
end
end
However what I really want is to send messages down this Phoenix channel that come from a different Elixir process (which happens to be subscribed to a third party API via websockets). So whenever I get a websocket message from the third party handler process, I want to send that message to this RoomChannel
module, and get it to send the message down the channel to the browser.
How do I do this? Is there a GenServer-style handle_info
handler that I can write that will listen for incoming messages within RoomChannel
and send it down the Phoenix channel?
Or do I somehow have to send the Phoenix socket to another GenServer to handle it there?
Phoenix.Channel
is not a GenServer
on its own, it uses Phoenix.Channel.Server
underneath, but it mimics the GenServer
behaviour. This is not shown in the main tutorial documentation but you can find it if you look through the reference: