3

This is frustrating me like crazy!

i'm using a proxy so that any requests that go to myurl/APP/ are answered by node.js at myurl:8001

I now need this to work over https. easy.... i thought.

i have Apache serving up the old version from the public folder. That is stand alone, and when i'm done building this, it will just be removed. but for now needs to remain in tact and accessible. Lets encrypt is setup on this. and https://myurl works fine, showing content from the public folder of course.

if i go to https://myurl:8001 then chrome says "site can't be reached". If i go to http://myurl:8001 it works fine. I think this is because https default port is 443. I have VPS not dedicated so i don't think i can alter that. And surely if i did alter the ssl port then it wont work for the public folder??

i'll show you the basics of whats going on;

app.js;

   var express = require('express');
var app = express();
var serv = require('http').Server(app);

app.get('/',function(req, res) {
    res.sendFile(__dirname + '/client/index.html');
});
app.use('/client',express.static(__dirname + '/client'));

serv.listen(8001);
console.log("Server started.");

var SOCKET_LIST = {};

var io = require('socket.io')(serv,{});
io.sockets.on('connection', function(socket){
    socket.id = Math.random();
    socket.x = 0;
    socket.y = 0;
    socket.number = "" + Math.floor(10 * Math.random());
    SOCKET_LIST[socket.id] = socket;

    socket.on('disconnect',function(){
        delete SOCKET_LIST[socket.id];
    });

});

setInterval(function(){
    var pack = [];
    for(var i in SOCKET_LIST){
        var socket = SOCKET_LIST[i];
        socket.x++;
        socket.y++;
        pack.push({
            x:socket.x,
            y:socket.y,
            number:socket.number
        });    
    }
    for(var i in SOCKET_LIST){
        var socket = SOCKET_LIST[i];
        socket.emit('newPositions',pack);
    }




},1000/25);

client/index.html;

<canvas id="ctx" width="500" height="500" style="border:1px solid #000000;"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>

<script>
    var ctx = document.getElementById("ctx").getContext("2d");
    ctx.font = '30px Arial';

    var socket = io.connect('http://www.myurl:8001', {path: "/socket.io"});

    socket.on('newPositions',function(data){
        ctx.clearRect(0,0,500,500);
        for(var i = 0 ; i < data.length; i++)
            ctx.fillText(data[i].number,data[i].x,data[i].y);      
    });
</script>

It works fine as the code is but only over http. I need this to work over SSL

i need this line to work when its https;

var socket = io.connect('https://www.myurl:8001', {path: "/socket.io"});

How is this possible?

any help is greatly appreciated.

1
  • After changing the routing from a single forward slash to a double. i no longer needed to use the port and could use the url path instead so var socket = io.connect('myurl:8001', {path: "/socket.io"}); became var socket = io.connect('myurl/theappfolder', {path: "/socket.io"}); Commented Dec 4, 2017 at 23:52

2 Answers 2

2

Your server code is only creating an http server, not an https server and since socket.io uses your http server, it will only run on http. http and https servers are different (the https server implements certificate verification and data encryption which is not present with the http server).

To make an https server, change this:

var express = require('express');
var app = express();
var serv = require('http').Server(app);

to this:

const express = require('express');
const app = express();
const options = {...};    // fill in relevant certificate info here
const serv = require('https').createServer(options, app);

And, you will have to fill in the appropriate certificateinfo in the options data structure.

https.createServer() code examples are here.

You can then run your https server on any port you want as long as you connect to it with an https URL and the right port number. You are correct that the default port for an https URL is 443 so if not port number is specified, that's what a browser will attempt to use.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply Jfriend00 - I have solved this now. I havent got time now to write up a detailed version of how i solved it but yes you are right... i had to create an https server. What i actually had to do was listen on a separate port for socket.io. I will actually make a video explaining it to dummies like myself because even though there is a ton of info regarding this... trust me when i say, the info isn't good for dummies. but i have it working and will pass the knowledge on as soon as i have time :-)
@FeelsUnique - Well, unless you want your web server to be http and your socket.io to be https, there's no technical reason that the web server and socket.io can't be on the same port. So far you haven't described any product requirement that would actually require a separate port.
@FeelsUnique - Did this answer your question? If so, you can indicate that to the community by clicking the checkmark next to the answer and also earn yourself some reputation points for following the proper procedure here on stackoverflow.
1

Ok this was a bit embarrasing... the actual problem i had was because of the following.

If you can follow this lol...

So in dreamhost they allow you to run apache and serve content in the usual way. there is a /public folder for this. fairly standard stuff. Because i'm trying to branch out into being able to use sockets (please don't lecture me on using sockets with PHP... let it go!) So i was using dreamhost which allows a person to proxy a node.js app through a port. eg. port xxxx ends up at thewebsiteurl/whateverfolderyouchoose/

ok so in my node.js i was using express. now in express you can specify routing really easy. For some bizzare reason, i cant be bothered to actually find out why, if you know please enlighten me... i had to put double forward slash in the routing, like so "//whateverroute/". all the examples i was learning from use a single forward slash. eg "/whateverroute/" but that didn't work. if you know why, please tell us. So before i figured this out, i couldn't actually route anything correctly, hence having to use the url with the port number at the end. of course that messes up the https. example, https://www.theurl.com:xxxx wasnt secure, because the node.js app wasnt running an https res req server. Actually i did set up the https node server and thought that was the solution to the problem at first.

So the solution to this bizarre problem was the node.js routing. ofcourse its stupidly complicated because, hey life's like that when your learning from google and not someone who actually knows your exact setup.

What turns out to be funny is, i cant stand node.js. I can see the benefit to using it. But it seems like alot of ball ache for something as simple as what i'm making. Also i have now discovered phone/gap so php actually is ok for what i want to do. Ok so i'm hitting the server way to many times with AJAX requests but it's coping with a few hundred users right now and when it hits the 10'000 mark when the site probably wont work very well, i should be making enough to get a real node.js programmer in to learn from.... hopefully.

I do appreciate the help tho guys.

1 Comment

app.get('/',function(req, res) { had to be changed to app.get('//',function(req, res) { i cant tell you why. It was pure fluke i tried it

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.