Handle Acknowledgement Timeouts
A client should not wait indefinitely for an acknowledgement.
Socket.IO supports timeout-based acknowledgements using the timeout() method.
If the server does not acknowledge the event within the configured duration, the client receives an error through the acknowledgement callback.
Choose acknowledgement timeouts according to the operation. Database writes and external API calls may require different timeout values.
socket.timeout(5000).emit('chat:send', {
username,
message
}, (error, response) => {
if (error) {
console.error('Server acknowledgement timed out');
return;
}
if (!response.success) {
console.error(response.error);
return;
}
console.log('Message accepted');
});The timeout method configures a five-second acknowledgement deadline. If no acknowledgement arrives in time, the error argument is populated.
Acknowledgement timeouts prevent indefinite waiting.
The timeout callback receives an error when no acknowledgement arrives.
Choose timeouts based on expected processing time.