mmmries
How to send request to Phoenix Channel client
I have a phoenix project which acts as a public interface to a nerves powered device. As part of the UI the user can request for things be turned on. The phoenix client library has a nice way to send a “request” and await a response. I’m wondering if the same thing can be down from the server.
Can I push a message to a client with a uniq ID and wait for some amount of time to hear back a response with that same ID? I can implement this myself by storing a socket assigns in the channel state, but I’ll have to handle all the logic around timeouts and other edge cases myself.
Just wondering if other people have done this sort of interaction before where they want to send some data down to a channel client and then wait for the client to respond.
Most Liked
jjcarstens
I recently added this to one of our internal projects. I basically use a GenServer to handle the requests and store the ID. You interact via GenServer.call with a timeout. The initial request just saves the reference and the “from” reference in state and each incoming message to the server checks if that reference exists then does GenServer.reply to the “from” address.
Kind of a weird explanation. I’ll build a little code example in a bit and post that up (on mobile right now)
jjcarstens
K, I built a little example that you can run and see my idea for implementation. As a bonus, it also handles timeout waiting for a client response and making synchronous requests in a multi-node systems.
Take a gander and hopefully it sparks some ideas.
LostKobrakai
Can‘t you use the ref handling of phoenix channel messages instead of using a different channel for the response?
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








