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

Support 'drain' event and boolean response in socket.emit() in order to implement proper Node.js style backpressuring

Open illarion opened this issue 7 years ago • 2 comments

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();
       });
   }
});

illarion avatar Jan 13 '18 17:01 illarion

Any updates?

illarion avatar Dec 03 '18 08:12 illarion

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.

darrachequesne avatar Apr 01 '22 12:04 darrachequesne