socket.io
socket.io copied to clipboard
Possible to Ack incomming client EmitWithAck messages?
If a client sends a EmitWithAck message, how can I send a response to ack the message from the go server?
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.