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

Best way to handle timeouts on acknowledgements

Open jjoschyy opened this issue 14 years ago • 3 comments

Dear LearnBoost

Using the acknowledgements "feature" from socket.io works fine. Just in case the server does not respond (e.g. due to connection loss or high server load), the acknowledgement callback will never be fired. Therefore, I tried to implement timeouts for acknowledgements.

The following example is working, but it can happen that both, timeoutErrorFn and acknCallbackFn, will be executed. Is there an better, more common way to solve this problem? Do you think about adding timeouts to the "emit" as basic feature?

Thank you very much for any idea! Joschy



var timeoutId = setTimeout(timeoutErrorFn, 500);

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

socket.emit('getUserData', acknCallbackFn);

jjoschyy avatar Apr 11 '12 22:04 jjoschyy

If anyone lands here, please check the following module: https://www.npmjs.com/package/timeout-callback

Hope it helps :)

jakubknejzlik avatar Feb 17 '15 14:02 jakubknejzlik

@jakubknejzlik Nice! Exactly what I was looking for.

nicopeeters avatar Mar 16 '17 10:03 nicopeeters

@jakubknejzlik you solution appears to be for the server since, any idea on solution for cleint side??

Mrjaid avatar Sep 03 '17 08:09 Mrjaid