Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

io.engine undefined? #5376

Discussion options

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

You must be logged in to vote

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

Comment options

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

You must be logged in to vote
2 replies
Comment options

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

Comment options

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);
Answer selected by SupertigerDev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /