2

I will try to explain myself the best than I can!

I have a node.js server and I want to add socket.io. I manage to get it running on http but I'm having some problems running it on https.

Server:

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


var key = fs.readFileSync('mydomain.key');
var cert = fs.readFileSync( 'mydomain.crt' );

var options = {
key: key,
cert: cert
};

var https = require('https');
var io = require("socket.io")(https);
var cors = require('cors')

var server = https.createServer(options, app).listen(443);

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", 'domain');
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  res.header('Access-Control-Allow-Credentials', true);
  next();



server.on('error', function (e) {
  console.log(e);
});

io.on('connection',function(socket){  
    console.log("A user is connected");
    socket.on('add reserva',function(status){
    //io.sockets.emit('update reserva', status);
      add_reserva(status,function(res){
        if(res){io.sockets.emit('update reserva', status);} 
          else {io.sockets.emit('error');}
      });
    });
});

**And the client code:**

      $(document).ready(function(){
          var socket = io("https://151.236.63.10:443");
          $("#add_status").click(function(){
            socket.emit('add reserva',$("#comment").val());
          });
          socket.on('update reserva',function(msg){
            new Noty({
                type: 'success',
                layout: 'topRight',
                text: msg
            }).show();
          });
    });
});

In addicion, if there is something I could do better with the code, I would appreciate any advice

5
  • https:// 151.236.63.10:443 An ip address cannot be ssl enabled. you need to define domain name. Commented Jul 11, 2018 at 10:17
  • Sorry I forgot to say it's a VPS, I still need to define domain name? Commented Jul 11, 2018 at 10:20
  • 2
    var io = require("socket.io")(https); → I should replace it by → var io = require("socket.io")(server) [and so place it after the creation]. Btw I will place all stuff like 443 in a config file. Commented Jul 11, 2018 at 10:23
  • @cagliostro Thanks a lot man! It worked! "var io = require("socket.io")(server)" How so place it in a config file? Commented Jul 11, 2018 at 10:28
  • @klnx just place your parameters in a config file (ex: config.js) and exports and object where you place all your var. Example: module.exports = {https:443} import it after in your server file, and instead of 443 you have config.https. Just cleaner. Commented Jul 11, 2018 at 10:47

1 Answer 1

1

Ok I'll give you a rewriting to get something cleaner. Hope it helps.

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

const config = require('./config'); //--> config file 

var options = {
key: fs.readFileSync('mydomain.key'),
cert: fs.readFileSync( 'mydomain.crt' )
};

const secureServer = https.createServer(options,app).listen(config.portHTTPS);

const io = require('socket.io')(secureServer) // you can add options for socket here
Sign up to request clarification or add additional context in comments.

Comments

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.