uNetworking / uWebSockets.js

μWebSockets for Node.js back-ends :metal:
Apache License 2.0
8.08k stars 573 forks source link

socket.publish failure? #525

Closed ronag closed 3 years ago

ronag commented 3 years ago

I have a use case where a socket sends a message and the server is supposed to broadcast it to all others:

if (sender) {
  sender.publish(subscription.id, subscription.message)
} else {
  app.publish(subscription.id, subscription.message)
}

However, if the sender is closed then this code will actually not send the message to anyone. What's the best way to handle this?

Wouldn't it better to have a different signature on app.publish? Something like:

app.publish(sender, subscription.id, subscription.message) // if first arg is socket, don't publish to it
ghost commented 3 years ago

If sender "is closed" you cannot do anything with it - it should throw in that case. So either keep track of when sender closes, or handle the exception. If sender is closed then app.publish will have the same behavior (send to all but sender is the same if sender is closed).

ronag commented 3 years ago

If sender "is closed" you cannot do anything with it - it should throw in that case.

Looking at the code I'm not sure that's the case?

If sender is closed then app.publish will have the same behavior (send to all but sender is the same if sender is closed).

So you suggest something like:

if (sender) {
  try {
    sender.publish(subscription.id, subscription.message)
  } catch (err) {
    if (err.code == WHAT_CODE_???) {
      app.publish(subscription.id, subscription.message)
    } else {
      throw err
    }
  }
} else {
  app.publish(subscription.id, subscription.message)
}
ghost commented 3 years ago

https://github.com/uNetworking/uWebSockets.js/blob/master/src/WebSocketWrapper.h#L29

No I would prefer keeping track of when sender is closed, but otherwise, yes.

ronag commented 3 years ago

Thanks!