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

Callbacks that are defined on the server and sent to the client do not make it to the client listener. I've included a link to a repository that reproduces the bug in a clean environment.

Open AlexAllocated opened this issue 12 years ago • 8 comments

I can do callbacks from the client to the server but I can't do callbacks that are defined on the server and sent to the client. The client listener doesn't receive the callback function in its arguments even though it receives all other arguments. The callback function just vanishes.

There is a workaround, but it's not the most elegant.

Instead of:

// server
socket.emit('someEvent', function () {
    console.log("Callback ran on server...");
});
// client
socket.on('someEvent', function (callback) {
    callback(); // fails because callback is undefined
});

You can work around the issue with a second event, like this:

// server
socket.emit('someEvent');
socket.on('someEventComplete', function () {
    console.log("Complete event ran on server...");
});
// client
socket.on('someEvent', function () {
    socket.emit('someEventComplete');
});

The problem with this "workaround" is that it creates a race condition in some situations. For example, if I fire someEvent multiple times in a row it's possible that my return calls to someEventComplete will be fired out of order and I won't know which ones correspond to the other.

For those of you who want to work around this race condition issue, this should get you started:

  1. Generate some kind of unique identifier (combination of socket ID and event ID is probably good).
  2. Register a one time event listener that listens for an event with the name of the unique identifier.
  3. Send the unique identifier to the client along with any other arguments you need to send.
  4. When the client is ready to reply, emit an event using the identifier handed down by the server as the name.

Now you know which replies correspond to which handler invocations. It should work, but it's pretty gross and cumbersome.


I can confirm that the reverse (client to server)...

// client
socket.emit('someEvent', function () {
    console.log('Callback ran on client...');
});
// server
socket.on('someEvent', function (callback) {
    callback();
});

...works just as it should. It's only when the callback is defined on the server and sent to the client that it fails.


I really want to see this issue fixed so I went the extra mile and created a project that reproduces the bug. To run it:

  1. Clone the repsitory.
  2. npm install
  3. node app.js
  4. Go to http://localhost:3000/

You'll see a callback sent to the server and successfully executed. Then you'll see a callback sent to the client, which fails because it is undefined.

AlexAllocated avatar Mar 07 '14 03:03 AlexAllocated

It's a shame that this gets no attention. It's a blatant bug that requires a massive workaround and I put forth a lot of effort to demonstrate the bug in all its glory so that it would be easy for someone familiar with socket.io code to debug and fix. Looking at the number of open issues though, I'm not going to hold my breath.

AlexAllocated avatar Jun 17 '14 14:06 AlexAllocated

@chevex sorry we haven't gotten around to it yet!

rauchg avatar Jun 17 '14 14:06 rauchg

Well I appreciate it. In the future though, just an acknowledgement is all I would have needed and I would be content with this still being open. I just want to know that my detailed efforts actually matter and are seen by someone.

AlexAllocated avatar Jun 17 '14 14:06 AlexAllocated

We're cleaning up old issues as well. We definitely look at all the new ones.

rauchg avatar Jun 17 '14 14:06 rauchg

Much appreciated.

Thank you for the kind replies.

AlexAllocated avatar Jun 17 '14 15:06 AlexAllocated

It's been a few months so I tried my sample project with the latest version of socket.io. This issue must have been reported elsewhere or fixed unintentionally. I should have checked that this was still an issue before complaining. I just saw no update and assumed it was still broken.

This can legitimately be closed now.

AlexAllocated avatar Jun 17 '14 15:06 AlexAllocated

Thanks for the update!

— Guillermo Rauch – @rauchg https://twitter.com/rauchg

On Tue, Jun 17, 2014 at 8:17 AM, Alex Ford [email protected] wrote:

Closed #1444 https://github.com/Automattic/socket.io/issues/1444.

— Reply to this email directly or view it on GitHub https://github.com/Automattic/socket.io/issues/1444#event-132228814.

rauchg avatar Jun 17 '14 15:06 rauchg

Well it's been 6 years and still go work around.... https://stackoverflow.com/questions/62934533/is-it-possible-to-do-a-server-side-callback-within-socket-io

Nathan1258 avatar Jul 16 '20 12:07 Nathan1258