-
Notifications
You must be signed in to change notification settings - Fork 10.1k
io.engine undefined? #5376
-
Am I being an idiot? why is io.engine undefined?
in the docs, it says i can use io.engine.handleUpgrade
, but io.engine
seems to be undefined. vs code autosuggestion works tho, just when running the code it says undefined
https://stackblitz.com/edit/node-3y7mbxtu?file=index.js
const { Server } = require('socket.io');
const io = new Server({});
console.log(io.engine); // -> undefined
v4.8.1
Beta Was this translation helpful? Give feedback.
All reactions
It's possible, but a bit more verbose:
import { createServer } from "node:http"; import { Server } from "socket.io"; import { Server as Engine } from "engine.io"; const httpServer = createServer(); const io = new Server(); const engine = new Engine(); io.bind(engine); const io2 = new Server(); const engine2 = new Engine(); io2.bind(engine2); httpServer.on("request", (req, res) => { if (res.writableEnded) { return; // already handled, nothing to do } else if (req.url.startsWith("/first/")) { engine.handleRequest(req, res); } else if (req.url.startsWith("/second/")) { engine2.handleRequest(req, res); } else { res.writeHead(404); res.end(); } }); httpServer.on
Replies: 1 comment 2 replies
-
Hi!
io.engine
is defined once the Socket.IO server is bound to an HTTP server, for example:
import { createServer } from "node:http"; import { Server } from "socket.io"; const httpServer = createServer(); const io = new Server(httpServer); httpServer.on("upgrade", (req, socket, head) => { if (req.url.startsWith("/socket.io/")) { io.engine.handleUpgrade(req, socket, head); } else { socket.destroy(); } }); httpServer.listen(3000);
Reference: https://socket.io/docs/v4/server-api/#enginehandleupgraderequest-socket-head
Beta Was this translation helpful? Give feedback.
All reactions
-
But if engine is undefined. How will we run that handleUpgrade
function?
Edit: oh wait I think I get it. I'll try it out in a second thanks!
Edit2: I wanted to run 2 socket.io servers on a single http server on a different route, I guess that won't be possible
Beta Was this translation helpful? Give feedback.
All reactions
-
It's possible, but a bit more verbose:
import { createServer } from "node:http"; import { Server } from "socket.io"; import { Server as Engine } from "engine.io"; const httpServer = createServer(); const io = new Server(); const engine = new Engine(); io.bind(engine); const io2 = new Server(); const engine2 = new Engine(); io2.bind(engine2); httpServer.on("request", (req, res) => { if (res.writableEnded) { return; // already handled, nothing to do } else if (req.url.startsWith("/first/")) { engine.handleRequest(req, res); } else if (req.url.startsWith("/second/")) { engine2.handleRequest(req, res); } else { res.writeHead(404); res.end(); } }); httpServer.on("upgrade", (req, socket, head) => { if (req.url.startsWith("/first/")) { engine.handleUpgrade(req, socket, head); } else if (req.url.startsWith("/second/")) { engine2.handleUpgrade(req, socket, head); } else { socket.destroy(); } }); httpServer.listen(3000);
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 1