Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket.IO with uWebSockets: "initial_headers" event not being emitted #5300

Open
AKibMg opened this issue Feb 18, 2025 · 2 comments
Open

Socket.IO with uWebSockets: "initial_headers" event not being emitted #5300

AKibMg opened this issue Feb 18, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@AKibMg
Copy link

AKibMg commented Feb 18, 2025

Describe the bug
Seems like the "initial_headers" (and also "headers") event is never emitted when using Socket.IO with uWebSockets. Need a way to attach headers to the http handshake response.

To Reproduce
socket.io v4.8.1
uWebSockets.js v20.51.0

Server

server.mjs 

import { App } from "uWebSockets.js";
import { Server } from "socket.io";

const app = new App();
const io = new Server({
  serveClient: false,
  transports: ["websocket"],
});

io.attachApp(app);

io.engine.on("initial_headers", (headers, req) => {
  console.log("initial headers event emitted");
});

io.on("connection", (socket) => {
  console.log("connected");
});

app.listen(3000, (token) => {
  if (token) {
    console.log("Server listening on port 3000");
  } else {
    console.log("Failed to listen on port 3000");
  }
});

Client

const socket = io("http://localhost:3000", {
  transports: ["websocket"],
  reconnection: true,
});

Expected behavior
Expecting console to log "initial headers event emitted", but only getting "connected" and "Server listening on port 3000".

@AKibMg AKibMg added the to triage Waiting to be triaged by a member of the team label Feb 18, 2025
@ilkyy
Copy link

ilkyy commented Feb 28, 2025

only 'polling' requests are emitted by initial_headers event, downgrading uwebsocket to v20.30.0 or socket.io to 4.5.0 does not worked.

@darrachequesne
Copy link
Member

I could indeed reproduce the issue.

The test here is disabled for the engine based on uWebSockets:

it("should emit a 'initial_headers' event (websocket)", function (done) {
if (
process.env.EIO_WS_ENGINE === "eiows" ||
process.env.EIO_WS_ENGINE === "uws"
) {
return this.skip();
}

For the default engine, we use the headers event emitted by the ws server:

this.ws.on("headers", (headersArray, req) => {
// note: 'ws' uses an array of headers, while Engine.IO uses an object (response.writeHead() accepts both formats)
// we could also try to parse the array and then sync the values, but that will be error-prone
const additionalHeaders = req[kResponseHeaders] || {};
delete req[kResponseHeaders];
const isInitialRequest = !req._query.sid;
if (isInitialRequest) {
this.emit("initial_headers", additionalHeaders, req);
}
this.emit("headers", additionalHeaders, req);
debug("writing headers: %j", additionalHeaders);
Object.keys(additionalHeaders).forEach((key) => {
headersArray.push(`${key}: ${additionalHeaders[key]}`);
});
});

@darrachequesne darrachequesne added bug Something isn't working and removed to triage Waiting to be triaged by a member of the team labels Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants