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.

4
  • Possible duplicate of node.js, socket.io with SSL It looks to me like that thread should have everything you need. Commented Oct 15, 2017 at 15:21
  • Is there no way to do this without using Express? Commented Oct 15, 2017 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. Commented Oct 15, 2017 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. Commented Oct 15, 2017 at 15:49

3 Answers 3

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});
Sign up to request clarification or add additional context in comments.

1 Comment

In origin which URL do we need to set backend URL of socket or application URL?
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)

Comments

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);

1 Comment

why are you calling .listen twice on the https server?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.