ericlathrop

ericlathrop

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.

Showing Posts 1 to 6

mgwidmann

mgwidmann

I’m curious to her the answer to this as well…

OvermindDL1

OvermindDL1

Honestly I’d probably be lazy and make a configuration for the module to call on, then in the app I’d get that config entry and just call whateverConfig.broadcast(...) on it, and just configure my endpoint in the config. ^.^

chrismccord

chrismccord

Creator of Phoenix

handle_out is being called because It’s not respecting the channel subscriber fastlanining, but without seeing your code it’s hard to say why that is. In any case, instead of trying to build cheat by building the Phoenix.Socket.Broadcast, I would have the external app broadcast “raw” messages over pubsub that your phoenix app subscribes to. You could have the channel subscribe to the topic and forward the message in handle_info, or you can have server(s) in your app subscribing to external topics/events, and then relaying them to channels via MyApp.Endpoint.broadcast. This way you aren’t coupling the external app to your channel layer directly. This also gives you a place to do any necessary data munging from the external data to the client representation.

ericlathrop

ericlathrop OP

I did get it to work by making these changes:

  1. I removed the struct definition so my broadcast code looks like:
  defp broadcast(topic, event, payload) do
    Phoenix.PubSub.broadcast(Www.PubSub, topic, %{
      topic: topic,
      event: event,
      payload: payload
    })
  end

and I call it like:

broadcast("session:12345", "yolo", %{ yoloed: 1 })
  1. I added a handle_info into my SessionChannel:
  def handle_info(%{topic: _, event: ev, payload: payload}, socket) do
    push socket, ev, payload
    {:noreply, socket}
  end

I found that code at the bottom of the Channel docs under “Subscribing to external topics”.

It works, but seems a little fragile. I like @chrismccord’s idea of making a server (GenServer?) that uses Phoenix.PubSub to listen to my own format message, and then MyApp.Endpoint.broadcast them. How do I prevent double-broadcasts when I have 2 webservers running each with their own GenServer doing the message forwarding?

keb

keb

I’m facing a similar problem, that is, in an umbrella app there is one app that supervises a PubSub server, another one that broadcasts a message, and a third one, which is the Phoenix app that contains the relevant channel. There is also Presence in use, and apparently “fastlaning” has been disabled for that as well:

18:12:44.561 [error] GenServer #PID<0.940.0> terminating
** (UndefinedFunctionError) function Web.Presence.Channel.handle_out/3 is undefined or private. Did you mean one of:

      * handle_in/3
      * handle_info/2

    (web) Web.Presence.Channel.handle_out("presence_diff", %{joins: %{}, leaves:%{...
    (phoenix) lib/phoenix/channel/server.ex:252: Phoenix.Channel.Server.handle_info/2
    (stdlib) gen_server.erl:616: :gen_server.try_dispatch/4
    (stdlib) gen_server.erl:686: :gen_server.handle_msg/6
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: %Phoenix.Socket.Broadcast{event: "presence_diff", payload: %{joins: %{}, leaves: %{...

My solution was to simply implement the missing handle_out callback:

  def handle_out(event, payload, socket) do
    push(socket, event, payload)
    {:noreply, socket}
  end

As long as the Phoenix app creates the PubSub server this isn’t needed, but it’s suddenly required when you configure your endpoint to use an external PubSub server instead. Why?

@chrismccord is there documentation on what fastlaning actually means, what it does and why it’s disabled in some cases? I couldn’t find anything about it on hexdocs, and I’m not sure where to look in the code.

dorian-marchal

dorian-marchal

Same question, I have a channel that broadcast events (from handle_in), with:

broadcast!(socket, "update", state)

This works well until I subscribe to a PubSub in the join callback:

:ok = Phoenix.PubSub.subscribe(MyAppWeb.PubSub, "topic")

Since I have added this subscription, the events broadcast trigger errors:

** (UndefinedFunctionError) function MyAppWeb.GridChannel.handle_out/3 is undefined or private

I can add the missing handle_out/3 callback but this doesn’t feel right and I don’t understand why I should add it.

What am I missing?

EDIT: I found out why: I was using the same topic name for my channel and my PubSub subscription. I didn’t realize channels and “global” PubSub shared the same namespace for topics.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews