socket.io
socket.io copied to clipboard
Support 'drain' event and boolean response in socket.emit() in order to implement proper Node.js style backpressuring
You want to:
- [ ] report a bug
- [x] request a feature
Current behaviour
From the documentation and code of socket.io and its dependencies such as engine.io and ws it's not clear, how to implement output intensive server application, that is capable to handle slow clients by considering "backpressure" (As described in https://nodejs.org/en/docs/guides/backpressuring-in-streams/ ). And it seems to be impossible.
Steps to reproduce (if the current behaviour is a bug)
It's not a bug, so, no step to reproduce.
Expected behaviour
I would expect socket.emit() to return bool and socket have 'drain' event, so that I could work with it like this (simplified):
someExternalSourceOfEvents.on('frequent_event', function(data) {
var ok = socket.emit('event', data);
if (!ok) {
someExternalSourceOfEvents.pause();
socket.on('drain', function() {
someExternalSourceOfEvents.continue();
});
}
});
Any updates?
For what it's worth, you can listen to the drain event emitted by the underlying engine:
socket.emit("some event");
// server side
socket.conn.on("drain", () => {
// ...
});
// client side
socket.io.engine.on("drain", () => {
// ...
});
Reference: https://socket.io/docs/v4/server-api/#socketconn
socket.emit() always return true though. Not sure if that covers the initial use case.