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');
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
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:
Regards, KFSys
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.