socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Possible to Ack incomming client EmitWithAck messages?

Open Wafje opened this issue 2 years ago • 1 comments

If a client sends a EmitWithAck message, how can I send a response to ack the message from the go server?

Wafje avatar Apr 04 '24 15:04 Wafje

If a client sends a EmitWithAck message, how can I send a response to ack the message from the go server?

that's through a callback, Wafje


socketClient.On("new-delivery-request", func(receivedData ...any) {
			fmt.Println("got new delivery request")
			receivedMessage := receivedData[0].(map[string]any)
			roomId := receivedMessage["socketRoomId"].(string)
			fmt.Println(receivedMessage)

			var callbackFn func([]interface{}, error)
			if fn, ok := receivedData[len(receivedData)-1].(func([]interface{}, error)); ok {
				fmt.Println("Callback is a function!")
				callbackFn = fn
				mapData := map[string]interface{}{
					"hasInterest": true,
					"who":         "wafje",
				}
				jsonData := []interface{}{mapData}
				callbackFn(jsonData, nil)

			} else {
				fmt.Println("Callback is not a function")
			}
}

the last payload from any event that has an acknowledgement is always a function with the signature " func([]interface{}, error)", invoke it like a callback function same way i did in the code above...and the emitter will get the response.

danielAsaboro avatar Apr 18 '24 08:04 danielAsaboro