I'm using simple socket.io 2.0.3 server without express or anything similar to run simple chat feature in my Laravel app.
Everything was working well until I decided to switch website to HTTPS. Now socket.io refuses to connect (ERR_CONNECTION_CLOSED).
Here is my simplest setup:
server.js:
var io = require('socket.io')(8080, {
origins : //some stuff
});
HTML file
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>
var socket = io(':8080');
//more stuff
I have all needed certificate files in server folder, intermediate.crt, domain.com.crt and domain.com.key
Can someone help with simplest example how to make this work on https? Thanks in advance!
Edit: Need possible solution without using Express.
I couldn't manage to write this in simple socket.io code, so I ended up using express after all.
Here is the simplest working code if anyone needs it in future:
server.js
var express = require('express');
var app = module.exports = express();
var https = require('https');
var fs = require('fs');
var server = https.createServer({
key: fs.readFileSync(/*full path to your key*/),
cert: fs.readFileSync(/*full path to your cert*/),
ca: fs.readFileSync(/*full path to your intermediate cert*/),
requestCert: true,
rejectUnauthorized: false
},app);
server.listen(8080); //listen on port 8080
var io = require('socket.io').listen(server);
io.set('origins', /*your desired origins*/);
io.set('transports', ['websocket',
'flashsocket',
'htmlfile',
'xhr-polling',
'jsonp-polling',
'polling']);
var sockets = {};
//your socket logic
in HTML:
var socket = io(':8080',{secure: true});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With