Skip to content

Is the server side "connection" event emitted BEFORE the client receives the connection event? #4332

Answered by darrachequesne
ekr3peeK asked this question in Q&A
Discussion options

You must be logged in to vote

The "connection" event on the server side should be fired a bit earlier, because the client waits for a CONNECT packet (received in the long-polling HTTP request, or in a WebSocket frame).

You should register the event listener in the "connection" handler, without any delay:

io.on("connection", async (socket) => {
  // BAD! "event" might be received while there is no listener
  await fetchSomething();

  socket.on("event", () => {});
});

I think autoConnect: false is not needed in your example above, because clientSideSocket.on() will always be called before the connection (which involves I/O operations).

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@ekr3peeK
Comment options

@ekr3peeK
Comment options

@ekr3peeK
Comment options

@darrachequesne
Comment options

Answer selected by darrachequesne
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants