I have been searching high an low for this and have not been able to find a solution. I have the following code in the server:
import express from 'express'; import { createServer } from 'https'; import { Server } from "socket.io"; import fs from 'fs'; const app = express(); const credentials = { key: fs.readFileSync('../key.pem'), cert: fs.readFileSync('../cert.pem') }; // create http/https server const server = createServer(credentials, app); const io = new Server(server); io.on('connection', (socket) => { console.log('a user connected'); }); server.listen(3000, () => { console.log('listening on localhost:3000'); }); Now in the client side I have:
import { io } from "socket.io-client"; import fs from "fs"; const socket = io("https://localhost:3000", { rejectUnauthorized: false, ca: fs.readFileSync("../cert.pem") }); socket.on("connect", () => console.log("connected to server")); If I change the server side to import to:
import { createServer } from 'http'; instead of:
import { createServer } from 'https'; and I change the client side to:
const socket = io("http://localhost:3000"); instead of:
const socket = io("https://localhost:3000"); I socket io is able to communicate just fine
It is just the https. I tried to add certs and to set
rejectUnauthorized: false with no avail. I found this question, but nothing there has worked. The socket io documentation has not been much help. What can I try next?