Skip to content

Create the server running on HTTP and HTTPS #4600

Answered by darrachequesne
El-Tommy asked this question in Q&A
Discussion options

You must be logged in to vote

Hi! You can attach the Socket.IO server to multiple HTTP servers with:

import { readFileSync } from "fs";
import * as http from "http";
import * as https from "https";
import { Server } from "socket.io";

const httpServer = http.createServer();

const httpsServer = https.createServer({
  key: readFileSync("./key.pem"),
  cert: readFileSync("./cert.pem")
});

const io = new Server(httpServer);

httpsServer.on("request", (req, res) => {
  io.engine.handleRequest(req, res);
});

httpsServer.on("upgrade", (req, socket, head) => {
  io.engine.handleUpgrade(req, socket, head);
});

httpServer.listen(3000)
httpsServer.listen(3001);

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@El-Tommy
Comment options

@El-Tommy
Comment options

@darrachequesne
Comment options

Answer selected by El-Tommy
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