Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Socket.IO message delivery error on client side

We need to update the client side UI to indicate that a message fails to deliver. How do I have Socket.IO JS client call a custom callback directly when the message fails to deliver? For example, something like:

socket.emit("event", data).onError(myCallback);

I know Socket.IO provides the Ack mechanism to confirm delivery success. Therefore, one can set up a timer with a handler which calls the failure callback, if the ack is not called after a certain amount of time. But this doesn't seem to be the best way to do.

Also there is the error event provided by Socket.IO, but it doesn't come with info regarding which emit caused the error.

like image 407
lgc_ustc Avatar asked Oct 30 '22 23:10

lgc_ustc


1 Answers

Unfortunately there's no way to get errors from callbacks, the only way is to indeed create your own timeout:

var timeoutId = setTimeout(timeoutErrorFn, 500);

var acknCallbackFn = function(err, userData){
  clearTimeout(timeoutId)
  //manage UserData
}

socket.emit('getUserData', acknCallbackFn);

Source of the code

And there's another issue about this, open

So for the time being you have to stick with your manual setTimeout.

like image 109
Fabio Antunes Avatar answered Nov 15 '22 05:11

Fabio Antunes