1

I have created a socket.io chat application on my virtual server (Ubuntu), which runs as an systemd service and which is active running.

My server.js is located in:

/var/www/vhosts/mywebpage.de/w1.mywebpage.de/chat/

The server.js looks like this:

const io = require('socket.io')(3055);

io.on('connection', function(socket) {

    // When the client emits 'addUser', this listens and executes
    socket.on('addUser', function(username, room) {
        ...
    });

    // When the client emits 'sendMessage', this listens and executes
    socket.on('sendMessage', function(msg) {
        ...
    });

    // Disconnect the user
    socket.on('disconnectUser', function(username, room) {
        ...
    });

});

In my website (https) I try to connect as follow:

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>

<script type="text/javascript">
    var loSocket;
    $(document).ready(function() {
        if(typeof(loSocket) == 'undefined') {
            loSocket = io('https://w1.mywebpage.de:3055', {
                reconnectionAttempts: 5,
                forceNew: true
            });
        }
    });
</script>

But I can't get a valid connection.

The developer tools say this:

(failed) ERR_CONNECTION_CLOSED with initiator polling-xhr.js:264.

What could be the error ?

Alex M
  • 2,756
  • 7
  • 29
  • 35
Kelturio
  • 11
  • 1
  • Well, socket.io is no webserver on its on, so you will need to setup a server using node http or expressjs in your server.js. See https://socket.io/docs/ – Tom M Aug 09 '17 at 11:30
  • But about 2 years ago I got this same server.js working. I dont know what changed since there. The server.js is permanently running with /usr/bin/node. – Kelturio Aug 09 '17 at 11:34

1 Answers1

0

From what I have done in the past I would create a https server which serves the SSL cert and create the socket server using the https server you created, this will allow you to connect via https and you will need to enable secure on socketio (use this question as a ref)

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(80);

function handler (req, res) {
    fs.readFile(__dirname + '/index.html',
    function (err, data) {
        if (err) {
            res.writeHead(500);
            return res.end('Error loading index.html');
        }

        res.writeHead(200);
        res.end(data);
    });
}

io.on('connection', function (socket) {
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
        console.log(data);
    });
});

Use this code as ref on how to create a socketio server using http You can find this code on the socket.io docs

NOTE: You will need to you https not http like shown in the example

Sean
  • 1,444
  • 1
  • 11
  • 21
  • Thanks, but I dont think THATs the problem, because you can see in the middle from https://socket.io/docs/ that ... var socket = io('socket.io')(PORT) ... creates a http server already. – Kelturio Aug 09 '17 at 11:38
  • Ye it creates one if you don't supply one, if you create your own and create the socket server using that it will use the one you created. – Sean Aug 09 '17 at 11:39
  • @Kelturio it creates an _HTTP_ server, not an _HTTPS_ server. – robertklep Aug 09 '17 at 11:40
  • You can do this tho `var app = require('https').createServer({ key: '', cert: '' }); var io = require('socket.io')(app);` – Sean Aug 09 '17 at 11:41
  • Can I also do this without a "key" and "cert" ? – Kelturio Aug 09 '17 at 11:58
  • No, for SSL you will NEED a certificate. If you don't have one and you can't buy/get one you could use LetsEncrypt its a service which gives people free certificates, you might think this is insecure but its not its perfectly save and a lot of people use it including some big brand names. https://letsencrypt.org/ – Sean Aug 09 '17 at 12:01
  • Is it possible just to create a cert just instant with `openssl` for free ? – Kelturio Aug 09 '17 at 12:54
  • Yes you can but it wont be verified therefor you will get an error when accessing it. Your best bet would be to use LetsEncrypt – Sean Aug 09 '17 at 14:03