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.
I have the same question. Is it possible to send a message to a connected user and wait to receive a response back? I simply want the client to report ping time to some 3rd party server.
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)
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.
Thank you very much. I appreciate this, I will surely inspect it
I have always heard of
GenServer, maybe it is time now to start using it.
Can‘t you use the ref handling of phoenix channel messages instead of using a different channel for the response?
@LostKobrakai you know…you probably can as long as you can keep it, and return to the caller, and handle timeouts.
This sample was adapted from some of our internal code where the messaging systems is MQTT and queued messages so I didn’t have the nice luxuries of Phoenix channels.
Do you know of any online tutorial on that?
Nope. I just know that messages in phoenix channels have a ˋrefˋ field, which afaik is used to track responses for messages initiated by the client.
The WAMP protocol supports this out of the box, everyone is a peer that can register procedures. I’ve been working on a client and server in Elixir. Worth checking out the protocol for some ideas.