Description
I noticed that in the 4.4.0 release, a timeout
flag was added:
socket.timeout(5000).emit("my-event", (err) => {
if (err) {
// the client did not acknowledge the event in the given delay
}
});
Interestingly I implemented similar logic in an application of mine by patching client-side sockets, but I combined it with promises as well. Something like
await socket.request('event', 'some data');
where the promise times out after a fixed amount of time if the server did not acknowledge.
As a result, I was wondering whether it would a nice feature to add to the library as well. The api would look something like
// Awaits indefinitely for a server response
await socket.request('event', 'some data');
// Times out after 5 seconds
await socket.timeout(5e3).request('event', 'some data');
This could be accompanied by a reply()
and replyAny()
method as well of course, which looks something like this and hence hides the callbacks from the user alltogether:
socket.reply('event', async (data) => {
return res.toJSON();
});
socket.reply('event', async (...data) => {
throw new Error('Something went wrong');
});
socket.replyAny(async (event, ...data) => {
return res.toJSON();
});
Additionally I think it might be useful to have a way of resurrecting errors as well, but I'm not sure how the api can look like. Perhaps something like
socket.errorBuilder(json => buildCustomErrorFromJSON(json));
or
io.errorBuilder(json => buildCustomErrorFromJSON(json));
where socket.errorBuilder
can be used to override for that socket.
As always, I'd be happy to create a PR for this should you decide that it is a feature that is desirable to have in the library. In my opinion it fits nicely in the trend that callbacks are more and more being replaced by promises.