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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
chrismccord
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.
AlejandroHuerta
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
joincallback? I’m presuming it does not as thejoincallback 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
No. See Chris’s usage of
handle_info/2AlejandroHuerta
Ah! Thanks for pointing that out. I understand now.
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.ex
lobby_channel.ex
genServer.ex
LostKobrakai
Just to cover the basics: Are there messages broadcasted on that topic while the genserver is alive?
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_ininLobbyChannel? It was my (perhaps misinformed) assumption that this was done automagically.LostKobrakai
Yes you need to broadcast messages in
handle_in. That’s not a “re-broadcast” though, ashandle_indoesn’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
Ok thank you for clarifying that. And “re-broadcasting” was just a poor choice of words, didn’t actually mean “broadcasting again”
I had solved it in a similar way, but with
GenServer.callinstead.