Search code examples
node.jssocketswebsocketsocket.ionodejs-server

NODEJS,SOCKET on port 8070 connecting on rendering but not directly opening


I have a cpanel where Apache is activated and displaying my website on port 80,443 https://mydomain.io

The below setup works on port 80 but not on other ports like if I want to run this at Port 8070

I want to run nodejs on port 8070 i.e http://IP:8070

SERVER SIDE

server.js

var compression = require('compression');
var _       = require('lodash');
var express = require('express');
var server  = express();
var app     = require('http').createServer(server);
var io      = module.exports.io = require('socket.io')(app);
server.use(compression());
app.get('/', (req, res) => {
   res.sendFile(__dirname + '/tester.html');
});
app.listen(8070, function(){
    console.log('listening on *:' + 8070);
});

Now when I open this IP:8070 this renders the HTML and it connects to sockets

WORKING

console.log(io.connect());

But I want to access it via mydomain.io

so If I try to go to mydomain.io/tester.html or a file locally on my pc

It doesn't connect to sockets

//NONE OF THESE WORKING!

WITH HTTP GETTING ERR:

   console.log(io.connect('http://mydomain.io:8070/', { transports: ["websocket"]}));
   console.log(io.connect('http://localhost:8070/', { transports: ["websocket"]}));
   console.log(io.connect('http://localhost/', { transports: ["websocket"]}));
   console.log(io.connect('http://mydomain.io/', { transports: ["websocket"]}));
   console.log(io.connect());

GETTIG ERR: Mixed Content: The page at '<URL>' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws:<URL>/socket.io/?EIO=3&transport=websocket'. This request has been blocked; this endpoint must be available over WSS.

WITH HTTPS NOT WORKING

   console.log(io.connect('https://mydomain.io:8070/', { transports: ["websocket"]}));
   console.log(io.connect('https://localhost:8070/', { transports: ["websocket"]}));
   console.log(io.connect('https://localhost/', { transports: ["websocket"]}));
   console.log(io.connect('https://mydomain.io/', { transports: ["websocket"]}));

WITH PORT 80 ITS WORKING BUT NOT ON ELSE

BUT if I run my server.js at port 80 and stopping apache using service httpd stop

then it works with mydomain.io/tester.html or a file locally on my pc

var socket = io.connect('wss://mydomain.io'); //Socket Address WOKRING

enter image description here


Solution

  • Yes, Thank you @v1shva, trying https.createserver instead of http createserver with my cpanel keys and cert file I am able to connect with this from anywhere.

    Thank You