I'm trying to setup a server to server link using socket.io over ssl connection. This is my example:
/**
* Server
*/
var app = require('express')();
var config = require('./config');
var https = require('https');
var http = require('http');
var fs = require('fs');
var server = https.createServer({key: fs.readFileSync(config.ssl.key), cert: fs.readFileSync(config.ssl.cert), passphrase: config.ssl.passphrase}, app);
//var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(config.port);
app.get('/', function (req, res) {
res.send('Server');
//res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
/**
* Client
*/
var io = require('socket.io-client');
//var socket = io.connect('http://localhost', {port: 8088});
var socket = io.connect('https://localhost', {secure: true, port: 8088});
socket.on('connect', function(){
socket.on('event', function(data){});
socket.on('disconnect', function(){});
});
The code works fine when ran without SSL. I suspect it could be my self-signed certificate not being accepted, but I do not know how to make the client accept it.
Can I accept a self-signed SSL certificate, or is there another approach I can take?
After some more searching, adding this in the client makes it work:
require('https').globalAgent.options.rejectUnauthorized = false;
/**
* Client
*/
var io = require('socket.io-client');
//var socket = io.connect('http://localhost', {port: 8088});
require('https').globalAgent.options.rejectUnauthorized = false;
var socket = io.connect('https://localhost', {secure: true, port: 8088});
socket.on('connect', function(){
socket.on('event', function(data){});
socket.on('disconnect', function(){});
});