Pushing data from LiveView to Channel over PubSub?

I have a Channel on which the JavaScript front-end connects.
I have a LiveView UI from which the user can type messages and send them to JS UI (well, it can’t, which is the problem here).
My flow is as follows:

JS user ----> |subscribe to |
LV user ----> |same topic   |
C = Channel
LV= LiveView
--> C) broadcast(some_message)
--> LV) handle_info(some_message)
--> LV) broadcast(reply_message)
--> C) handle_info(reply_message)
--> C) push(socket, reply_message) <-- !DOES NOT WORK 

Is this even possible?
I saw this blog from Sophie DeBenedetto which leads me to think this doesn’t work (without Registry)
Any help?

Hi @regex.sh can you show your actual code? In principle this should work just fine, but the devil is in the details of how your subscriptions and broadcasts are done.

2 Likes

but the devil is in the details of how your subscriptions and broadcasts are done.

Yup, devil was in the detail :smiley:
I subscribed to topic outside “join” callback (I have send() for :after_join)
It now requires me to implement handle_out, which I did and push is working :slight_smile:

Is handle_out normal for this sort of thing?

Current code (it’s currently WIP, so don’t judge haha)
I had subscribe in handle_info prior to this

def join("live-chat:room:" <> guest_id, %{"platformId" => platform_id}, socket) do
  GuestChat.subscribe("live-chat:room:" <> guest_id)
  send(self(), {:join_room, platform_id, guest_id})
  {:ok, socket |> assign(:guest_id, guest_id)}
end

def handle_info({:join_room, platform_id, guest_id}, socket) do
  GuestChat.broadcast("live-chat:room:" <> guest_id, "new_guest", %{uid: guest_id})
  Presence.track(socket, "live-chat:room:" <> guest_id, %{guest_id: guest_id, platform_id: platform_id})
  {:noreply, socket}
end