J0 / phoenix_gen_socket_client

Socket client behaviour for phoenix channels
MIT License
232 stars 48 forks source link

Client unable to reply to server push messages #70

Open kerrd89 opened 1 year ago

kerrd89 commented 1 year ago

I am happy to help extend/fix this problem if someone can point me in the right direction.

Perhaps I misunderstand the implementation, but I am bumping into a problem attempting to reply in the client to a message from the server.

The server can respond to push messages which come into the handle_reply method in the client. But the client cannot respond to push messages from the server. The messages come into the handle_message method for the client, which does not have the from input. The documentation for the client reply is asking for a from GenServer input which doesn't exist on handle_message.

Attempts to reply with anything else than {:ok, state} or {:connect, state} returns an error from the handle_message in the client and the responses do not make it to the server.

I am attempting to use this for realtime messaging. Sending a request to a client to update configuration and wanting to a receive a reply to that message. The work around currently is publishing a different message #{original_message}_reply but this feels like it isn't using the built-in benefits of the socket.

Any advice? Known shortcomings? Pointers for how one would make this work? Suggestions for improving this project?

lxedge commented 9 months ago

Hey buddy, you misunderstand the pub/sub pattern indeed. It's not the phoenix implementation.

In pub/sub pattern, server push msg down to the client, but there's no required for client to confirm this msg, it's named subscribe some thing, which called topic in elixir. And, you will find that, in Message Queue like RabbitMQ, it does the same thing. To ensure delivery, client must set ACK before connect to the queue.

Well, finally, socket is duplex, but in App layer in TCP/IP as B/S or C/S arch, server side has no responsibility to send any ACK to client side. Simply, we use WebSocket when we need to push msg without client request before.

So for your problem, you should ask client to send a ack msg to server after receive some msg. Like RabbitMQ did, It's not the specification of pattern.