AlejandroHuerta

AlejandroHuerta

Hey everyone, here’s my use case: I have a channel that sends events to a GenServer running a simulation and that GenServer responds, as well as sends broadcast back to the channel. Now I want to create another GenServer that can connect to that same channel and send and receive messages like any other client.

I’ve done some searching and noticed there are libraries that implement a full client, as in use websockets to connect to the channel, but that seems overkill since my new GenServers are running within the same application. Is there anyway to achieve this without running a full client?

Failing this I think I could just have the two GenServers communicate directly with each other but I would like to avoid creating a different communication channel within my simulation.

Showing Posts 1 to 9

chrismccord

chrismccord

Creator of Phoenix

You can simply use PubSub. Subscribe to the channel topic to “listen in” on the channel messages. Broadcast on the channel topic to send messages to channel processes.

def init(...) do
  MyApp.Endpoint.subscribe("room:123")
  ...
end

alias Phoenix.Socket.Broadcast
def handle_info(%Broadcast{topic: "new_msg"} = msg, state) do
  # do something on new chat message broadcasted by channel process
end

def broadcast_new_msg(body) do
  MyApp.Endpoint.broadcast_from(self(), "room:123", "new_msg", %{body: body})
end
AlejandroHuerta

AlejandroHuerta OP

Thanks for the quick reply, Chris! This looks like it will work for my needs. A couple more questions if you don’t mind. Does subscribing to the topic trigger the topics join callback? I’m presuming it does not as the join callback takes a socket (that’s easy enough to get around in my case). From looking at the docs for PubSub, it looks like after I subscribe I need to call Process.info at some interval to get notified of incoming messages, is this correct?

derek-zhou

derek-zhou

No. See Chris’s usage of handle_info/2

AlejandroHuerta

AlejandroHuerta OP

Ah! Thanks for pointing that out. I understand now.

westmark

westmark

Hi! Sorry to resurrect this. Is this solution still valid? I can’t get it to work. Do I need to set something else up? I believe I have a fairly default setup (see below) using the default generated @session_options in the endpoint. I have confirmed that the session_uuid sent to the genserver is correct. What can I be missing?

endpoint.ex

socket "/socket/session", MyApp.SessionSocket,
    websocket: [connect_info: [session: @session_options]

socket.ex

use Phoenix.Socket
channel "lobby:*", MyApp.Channels.LobbyChannel, websocket: true, longpoll: false

lobby_channel.ex

use Phoenix.Channel
@impl true
def join("lobby:" <> session_uuid, _payload, socket) do
  ...

genServer.ex

use GenServer
def init(session_uuid) do
  MyApp.Endpoint.subscribe("lobby:#{session_uuid}")
  ...
end

def handle_info(msg, state) do
  IO.puts("This never gets called")
end
LostKobrakai

LostKobrakai

Just to cover the basics: Are there messages broadcasted on that topic while the genserver is alive?

westmark

westmark

This is what I am referring to when I say “Do I need to set something else up?” Does this mean I need to re-broadcast the message from handle_in in LobbyChannel? It was my (perhaps misinformed) assumption that this was done automagically.

LostKobrakai

LostKobrakai

Yes you need to broadcast messages in handle_in. That’s not a “re-broadcast” though, as handle_in doesn’t deal with broadcast messages, but with events of the connected client. The server end of a channel does automatially forward pubsub messages it receives to its connected client, but events of the client are not automatically considered messages to broadcast.

westmark

westmark

Ok thank you for clarifying that. And “re-broadcasting” was just a poor choice of words, didn’t actually mean “broadcasting again” :smiley: I had solved it in a similar way, but with GenServer.call instead.

— 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
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews