9

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.

Mister M
  • 1,539
  • 3
  • 17
  • 37
  • Possible duplicate of [node.js, socket.io with SSL](https://stackoverflow.com/questions/6599470/node-js-socket-io-with-ssl) It looks to me like that thread should have everything you need. – Robert Moskal Oct 15 '17 at 15:21
  • Is there no way to do this without using Express? – Mister M Oct 15 '17 at 15:26
  • It's not enough to to simply include the client code in the browser. There needs to be a server side co component somplace. – Robert Moskal Oct 15 '17 at 15:29
  • You don't need express, but once you have sockets running, the above link tells you how to connect the client up. – Robert Moskal Oct 15 '17 at 15:49

3 Answers3

7

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});
Mister M
  • 1,539
  • 3
  • 17
  • 37
0

You can create an instance of an HTTPS server with all your relevant settings

var https = require('https');
https.createServer(options, app).listen(443);

Then simply pass it to the constructor:

new SocketIo(https)
vfioox
  • 730
  • 1
  • 8
  • 22
0

Important facts :

You must use https not http , Need to load crt files and create options object like in example Client side need only correct address like : https://YOUR_domain.com:PORT

Server side :

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//High definition
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// Globals 
//var MEMORY_CLEANER_INTERVAL = 5000;
//var crypto = require('crypto');
var tls = require('tls');
//dl  = require('delivery');
var fs = require("fs");
var mysql      = require('mysql');
var express    = require("express");
var app = express();
var http = require('http');
var https = require('https');
//var mkdirp = require('mkdirp');
var path = require('path');
//var nodemailer = require('nodemailer');
//includer
function read(f) {return fs.readFileSync(f).toString();}
function include(f) {eval.apply(global, [read(f)]);}

//var BASE = require('./lib/level1_module');
//BASE.NAME = "YEap";

var pkey = fs.readFileSync('/etc/httpd/ssl/YOUR_FILE.key');
var pcert = fs.readFileSync('/etc/httpd/ssl/YOUR_FILE_com.crt')

var SERVER_PORT = 9000;
 var options = {
    hostname: 'YOUR_PAGE.com',
    port: 9000,  
    key: pkey, 
    cert: pcert,
    requestCert: true,
    rejectUnauthorized: false,
};

var server = https.createServer(options, app).listen(SERVER_PORT, function(){
  console.log("Express server listening on port " + SERVER_PORT);
});  

var io = require('socket.io').listen(server);
server.listen(SERVER_PORT);
console.log('Socket server listening on port : ' , SERVER_PORT);
Nikola Lukic
  • 4,001
  • 6
  • 44
  • 75