Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implementing websocket server by nodejs on shared cpanel host

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.

like image 652
saeid ezzati Avatar asked Jan 05 '20 09:01

saeid ezzati


People also ask

Can I use WebSocket in shared hosting?

Also, shared hosting limits how long a process can run, so WebSocket connections would get dropped periodically.

Is Node JS good for WebSockets?

Node. js can maintain many hundreds of WebSockets connections simultaneously. WebSockets on the server can become complicated as the connection upgrade from HTTP to WebSockets requires handling. This is why developers commonly use a library to manage this for them.


1 Answers

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.

like image 153
stephancasas Avatar answered Sep 17 '22 19:09

stephancasas