Question

Digitaclocean blockeing socket.io

Posted December 25, 2020 1.2k views
ApacheNode.jsUbuntu 16.04

In my localhost socket.io working correctly, but in droplet it’s not working because socket.io requesting to http from https. How much I understand I can not add ssl certificate for ip address. The solution is instead ip address use domain, but in this case I don’t know how to create open port for domain not for ip address.

server.js

const options = {
    cors: {
        origin: "https://example.com",
        methods: ["GET", "POST"]
    },
    transports: ['websocket', 'polling'],
};

const http = require('http').Server();
const io = require('socket.io')(http, options);

io.on('connection', function (socket) {
    socket.on('message', function (data) {
        io.sockets.emit('add-review', data);
    })
});

http.listen(6001, '0.0.0.0', function(){
    console.log('listening on *:6001');
});

client.js

var socket = io('https://ip-address:6001');

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @karenshahmuradyan,

You’ll need to use Nginx as a reverse proxy. What it does is, usually HTTP listens on por 80 and HTTPS listens on port 443. With Nginx, you can configure your domain to listen on those ports and internally relay traffic to port 6001 where your socket.io is. That’s what a reverse proxy is called.

That way you can install an SSL certificate on the Domain, the connection will be private and secured with going over HTTPS and you’ll be able to use socket.io. Here is an article on how you can configure Nginx as a reverse proxy:

https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-one-ubuntu-18-04-server

Regards,
KFSys

by Jesin A
In this tutorial you'll configure Nginx as both a web server and as a reverse proxy for Apache to host four domains on a single server.