Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io SSL error

I'm trying to secure my web-site

Everything was ok until I've finally installed SSL certificate (from CloudFlare, not a self-signed one)

So the problem is: when I'm opening my site via HTTP - everything works perfectly, but when I'm opening it via https - site is losing connection to the host (it's needed to work)

In console I can see:

socket.io-1.4.5.js:1 GET https://188.226.131.219:8080/socket.io/?EIO=3&transport=polling&t=LlI-URk net::ERR_CONNECTION_CLOSED

If I understood correctly here is a problem with ports. Using HTTP port 80 is ok and using https port 443 has problems. Is there any way to swap ports?

like image 795
Данил Адамия Avatar asked May 04 '17 10:05

Данил Адамия


2 Answers

You may try the following:

In the node.js backend, configure socket as:

var fs = require('fs');
var https = require('https');
var app = require('express')();


        var options = {
                key: fs.readFileSync('<.key location>'),
                cert: fs.readFileSync('<.cert location>')
        };
       var server = https.createServer(options, app);

server.listen(443, "0.0.0.0", function(){
        console.log('listening in port', 443);
});

In the client app you need to connect as:

 socket = io.connect(<server_ip>, {
            secure: true
        });
like image 66
stzoannos Avatar answered Sep 22 '22 16:09

stzoannos


I just want to throw this out there. This was causing me a similar error and might be what you are looking for.

The socket.io examples say to use the ip address to connect your client side script to the server. Something like this:

var socket = io.connect("https://123.45.6.78:443"); 

However, once you add an "A" record for your SSL certificate, you need to use the hostname of the "A" record, not the IP address it is directing to. Like so:

var socket = io.connect("www.mysite.com"); 
like image 45
Strixie Avatar answered Sep 21 '22 16:09

Strixie