zishang520/socket.io

EmitwithAck never listens for the callback from the client so it always timed out

Closed this issue · 6 comments

This is a snippet from my code,
I'm porting a socket service already written in nodejs to go...
and this emitWithAck never returns a response ...it times out.

yet, the client automatically acknowledge the message as soon as it gets it
(it's instantaneous btw)

io.Timeout(180*time.Second).EmitWithAck("this-rider-location", receivedMessage)(
				func(args []any, err error) {
					fmt.Print(err)
					if err == nil {
                                        fmt.Println("we got a response")
                                        }
}
                                        

It turns out that it works, but i only get the responses from the acknowledgement (args) after timeout.
is there a way to get it as it arrives (asynchronously)? IIRC, the Nodejs socket io library does this.
And is this a skill issue on my end?

I'm doing this because I'm setting up a quiz platform where the message gets pushed to all connected clients...
and i want to use how fast they respond in grading them also

The broadcast EmitWithAck will wait for all clients to respond or time out before returning data.

Hi, thanks for the response.
Please is there an available API to broadcast to the room, and get the callback response when each client does response and not together?

Depending on your needs, you should use Ack for each clinet instead of broadcasting Ack, as in the following example:

io.On("connection", func(clients ...interface{}) {
		client := clients[0].(*socket.Socket)
		client.EmitWithAck("this-rider-location", "receivedMessage")(
			func(args []any, err error) {
				fmt.Print(err)
				if err == nil {
					fmt.Println("we got a response")
				}
			})
		client.On("disconnect", func(...interface{}) {
			// utils.Log().Success("/ test disconnect")
		})
	})

Return: a func(func([]any, error)) that will be fulfilled when all clients have acknowledged the event
using client does the same — wait for all client...
my use-case would love to initiate an action as each clients acknowledge not wait for all

I appreciate the help regardless, thank you

If you have any other questions, please feel free to contact me.