I’ve got an umbrella app with a Phoenix app, and some vanilla elixir apps. Is there a way to broadcast to a Phoenix channel from a vanilla elixir app? I’d like to avoid adding a dependency on the Phoenix app to reference its Endpoint.
I’m working on using Phoenix.PubSub
to do this, but I’m not having any luck. This code:
Phoenix.PubSub.broadcast(Www.PubSub, topic, %{
__struct__: Phoenix.Socket.Broadcast,
topic: topic,
event: event,
payload: payload
})
Is giving me this error:
[error] GenServer #PID<0.952.0> terminating
** (UndefinedFunctionError) function Www.TimeChannel.handle_out/3 is undefined or private. Did you mean one of:
* handle_in/3
* handle_info/2
(www) Www.TimeChannel.handle_out("now", %{now: "yolo"}, %Phoenix.Socket{assigns: %{user_id: 14904}, channel: Www.TimeChannel, channel_pid: #PID<0.952.0>, endpoint: Www.Endpoint, handler: Www.UserSocket, id: "users_socket:14904", joined: true, pubsub_server: Www.PubSub, ref: nil, serializer: Phoenix.Transports.WebSocketSerializer, topic: "time:now", transport: Phoenix.Transports.WebSocket, transport_name: :websocket, transport_pid: #PID<0.909.0>})
(phoenix) lib/phoenix/channel/server.ex:234: Phoenix.Channel.Server.handle_info/2
(stdlib) gen_server.erl:601: :gen_server.try_dispatch/4
(stdlib) gen_server.erl:667: :gen_server.handle_msg/5
(stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: %Phoenix.Socket.Broadcast{event: "now", payload: %{now: "yolo"}, topic: "time:now"}
State: %Phoenix.Socket{assigns: %{user_id: 14904}, channel: Www.TimeChannel, channel_pid: #PID<0.952.0>, endpoint: Www.Endpoint, handler: Www.UserSocket, id: "users_socket:14904", joined: true, pubsub_server: Www.PubSub, ref: nil, serializer: Phoenix.Transports.WebSocketSerializer, topic: "time:now", transport: Phoenix.Transports.WebSocket, transport_name: :websocket, transport_pid: #PID<0.909.0>}
I’m confused about it wanting to use handle_out
because I’m not using intercept
anywhere, but maybe I just have my message format wrong. I was hoping to get the vanilla elixir app to broadcast directly to the web browser clients.