Create server available on HTTP/HTTPs that can be used with PM2 #4603
-
I'm trying to create a Socket.IO server that has the following goals:
For that I created a script based on the Socket.IO help article teaching how to use PM2 and this question that teaches to use HTTP and HTTPs.
Using HTTP and HTTPs always throws an error. Via HTTPs I can't load the socket.io.js bundle. But as this service will be available via browser, it will be necessary to make it available via HTTPs to users.
This is just using the first part of the script, without trying to run with PM2 yet. When placing the PM2 part next to the script, other errors appear:
Even using HTTP socket.io.js and connecting with However, if I run all this over HTTP only, without requiring HTTPs, it works perfectly. What am I doing wrong for HTTP/HTTPs not to work together? Will I have to make the server available only in HTTP and create a proxy via NGINX to support HTTPs and call the HTTP server? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
(Edited after rereading the question)
Yes. The For future readers, here is the solution without pm2: 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);
});
io.attachServe(httpsServer);
httpServer.listen(3000)
httpsServer.listen(3001); |
Beta Was this translation helpful? Give feedback.
(Edited after rereading the question)
Yes.
The
@socket.io/pm2
package creates its own HTTP server (plain HTTP, listening on port 8080 by default, see here), so it won't work with HTTPS.For future readers, here is the solution without pm2: