13

I have acquired a shared host with a cpanel which supports nodejs. I can define a node.js app through "Setup Node.js App".

I want to make a websocket. They have opened 2088 Port for me.

This is my websocket server code:

const http = require('http');
const WebSocket = require('ws');

const server = http.createServer();
const wss = new WebSocket.Server({ server });

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });

  ws.send('something');
});

server.listen(2088);

Well, I run my code and then I send this request from client to server:

socket = new WebSocket('ws://mydomain.com:2088');

socket.addEventListener('open', function (event) {
    socket.send('Hello Server!');
});

socket.addEventListener('message', function (event) {
    console.log('Message from server ', event.data);
});

But, I keep receiving a timeout error and I can not connect to the websocket server.

It appears that making a websocket server on a shared cpanel host that is able to listen on a specific port, is a little bit different from the usual.

I have combed through the internet and all I got on cpanel nodejs was how to build a node.js app using cpanel menus. I couldn't find any explanation on how to make a websocket on shared cpanel host. All of the sources say that it is possible to make a websocket with a nodejs which is offered on cpanel.

Now, has anyone ever had a shared host with nodejs features? And run websocket on it?

The admins who have sold the host to me, are complete idiots, know nothing about this, and can not help me...

Thanks for your help in advance.

UPDATE:

How to run Node.js and python in shared hosts differs from the way they are run in vps. According to what I found out, the phusion passenger is used in shared hosts. The problem I'm having can be solved by someone who has worked on shared hosts with Nodejs and knows about the way phusion passenger works.

saeid ezzati
  • 855
  • 11
  • 29
  • Did you try remote address instead of mydomain.com? ws://remote-addr:2088 – Ritesh Kumar Gupta Jan 05 '20 at 10:07
  • @ritesh_NITW yes – saeid ezzati Jan 05 '20 at 10:07
  • As the port is already opened for inbound/outbound, probably it could be windows defender firewall that might be blocking? If its windows server, can you check if controlpanel->system and security-> windows defender firewall-> allowed apps have node.js in the list. – Ritesh Kumar Gupta Jan 05 '20 at 10:12
  • @RiteshKumarGupta os is linux centos . but is shared host ! – saeid ezzati Jan 05 '20 at 11:07
  • problem with the shared host is: they surely have a reverse proxy or load balancer in front. Although they have opened the port for you inbound, I am not sure whether the loadbalancer is forwarding the request further to the server. I will get back. – Ritesh Kumar Gupta Jan 05 '20 at 11:50
  • @RiteshKumarGupta How to run Node.js and python in shared hosts differs from the way they are run in vps. According to what I found out, the **phusion passenger** is used in shared hosts. The problem I'm having can be solved by someone who has worked on shared hosts with Nodejs. – saeid ezzati Jan 06 '20 at 09:02

1 Answers1

6

For a node application deployed from the cPanel UI, cPanel relies on Passenger to manage deployment. When a WebSockets connection request is sent, the client sends an HTTP request to "upgrade" via the Connection header in the request. Passenger responds to standard HTTP requests, but doesn't do anything with the Connection header, so the WebSockets request is effectively ignored. You can actually see this happening if you open the JS debugger inside your browser and inspect the WebSockets target resource.

Phusion has a WebSockets demo posted to GitHub that uses socket.io. In the demo's README, it states that WebSockets doesn't work correctly inside of Passenger, so instead it resorts to using HTTP long polling as a fallback. However, this fallback is a feature engineered into socket.io and for it to work correctly, Passenger has to be configured to use sticky sessions, which is an option not currently exposed to the UI in cPanel.

If you want to use node as a WebSockets server, you're going to need to run it outside of Passenger, and thus will most-likely need to get out of the shared hosting environment. Running it on cPanel is possible, but not without elevated shell privileges for your account.

Edit:
Your question really bothered me and so I spent another couple of hours working on it. If you really really want to run a node WebSockets server from cPanel, you can accomplish this by calling your server app using forever from a parent app that you've registered in the cPanel Application Manager. The parent app will execute from Passenger, and then your server app will execute outside of Passenger via forever. Passenger will complain about using the Node Cluster module, but it will still work.

Be sure that your parent app responds to HTTP requests, because you'll need to send at least one request to instantiate it in Passenger.

stephancasas
  • 615
  • 3
  • 12
  • can you explain further how you go this working with forever, I'm not sure how you have this set up exactly – rubixibuc Mar 04 '22 at 04:10