Setting up a Node.js WebSocket server on a shared cPanel hosting environment

Recently, I obtained a shared hosting plan with cPanel support for nodejs applications. Through the "Setup Node.js App" feature, I am able to define and set up a node.js app on my server.

My goal is to create a websocket, and fortunately, they have opened Port 2088 for me to use specifically for this purpose.

Here is the code for my websocket server:

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);

Despite running the code successfully, I encountered an issue where sending a request from the client to the server led to a timeout error, preventing me from establishing a connection to the websocket server.

It seems that creating a websocket server on a shared cPanel host that can listen on a specific port requires a slightly different approach than usual.

After extensive research online, I could only find information on setting up node.js apps using cPanel menus, with no detailed instructions on how to create a websocket on a shared cPanel host. However, all sources mention the possibility of implementing websockets using nodejs on cPanel.

I'm curious if anyone else has experience working with a shared host that offers nodejs features and successfully deployed a websocket on it?

The administrators who provided me with the shared hosting seem to lack knowledge in this area and have been unable to assist me thus far...

Thank you in advance for any guidance or assistance you can provide.

UPDATE:

Running Node.js and Python scripts on shared hosts differs from their execution on VPS servers. Shared hosting typically utilizes phusion passenger. To resolve the issues I'm facing, I believe consulting someone experienced in working with Node.js on shared hosting platforms and familiar with the workings of phusion passenger might be beneficial.

Answer №1

When deploying a node application from the cPanel UI, Passenger is used by cPanel for deployment management. However, when a WebSockets connection request is made, Passenger does not handle it effectively. The client sends an HTTP request to "upgrade" via the Connection header in the request, which is not processed properly by Passenger. This can be observed by inspecting the WebSockets target resource using the JS debugger in your browser.

A demo on GitHub by Phusion showcases WebSockets using socket.io. The README of the demo mentions that WebSockets do not work as expected within Passenger, so the demo fallbacks to using HTTP long polling. To make this fallback work with socket.io, sticky sessions need to be configured in Passenger, which is not currently supported in cPanel's UI settings.

If you wish to run a node WebSockets server, it is recommended to run it outside of Passenger, possibly requiring you to move out of the shared hosting environment. While running it on cPanel is feasible, elevated shell privileges are necessary for your account.

Edit:
After further investigation, it was found that running a node WebSockets server from cPanel is achievable by utilizing forever to call your server app from a parent app registered in the cPanel Application Manager. The parent app will execute within Passenger, while the server app runs outside of Passenger thanks to forever. Despite Passenger's objections to using the Node Cluster module, the setup still functions correctly.

Ensure that your parent app responds to HTTP requests, as at least one request needs to be sent to instantiate it in Passenger.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm having trouble resolving 'fs' within my Next.js api route

When attempting to use the fs module within my api route file for filesystem operations, I encountered an error message: Module not found: Can't resolve 'fs'. This issue not only prevents me from utilizing fs in my handler function but also ...

What is preventing me from setting headers after a redirect?

After making a json request in ExpressJS (node), this is the callback function I use: function handleQuestion(req, res){ request(req, res, function(response){ //redirect to the right url if page doesn't exist. var params = app.req ...

I encountered an issue while attempting to install dependencies listed in the package.json file using the npm install command

npm encountered an error with code 1 while trying to install a package at path C:\Users\HP\Desktop\workings\alx-files_manager\node_modules\sharp. The installation command failed due to issues with the sharp plugin and its ...

What is the access URL when using Angular 2's npm start command?

When I execute npm start in my angular 2 project directory, the console response includes the following lines: Access URLs: ------------------------------------ Local: http://localhost:3000 External: http://10.28.93.96:3000 ------------------------- ...

Utilizing NGINX as a reverse proxy for secure communication with Node.js

I'm currently running an NGINX server setup as a reverse-proxy server for a node application. I am now trying to enable HTTPS, but every time I attempt to access the site via HTTPS, I encounter a "502: Bad Gateway" error. server { listen 80; ...

unable to decipher the access code despite its presence

My website is displaying an error message indicating that the token is not valid, even though I am able to see the token in the console. This prevents me from showing user posts. Here is the code for generating the token: import jwt from "jsonwebtoke ...

What is the best way to include a class in the default template of Iron Router?

I've been scouring the internet without much luck in finding a solution to what seems like a very basic issue. I currently have an application with a standard template: Router.configure({ layoutTemplate: 'appLayout', }); <template name ...

Encountering a timeout issue while attempting to utilize npx create-react-app

Every time I attempt to execute this code, an error pops up: npm ERR! Response timeout while trying to fetch https://registry.npmjs.org/@typescript-eslint%2fparser (over 30000ms) The log shows the following: 205 warn notsup SKIPPING OPTIONAL DEPENDENCY: ...

What could be causing the delay in establishing a connection between my Node application and MongoDB?

It's taking approximately 1.2 minutes (76690 milliseconds) for the connection to my mongo db to be established. I am seeking assistance in determining whether there is an issue within my configuration that may be causing this delay, or if there are al ...

Why is ssr occurring when "client use" is implemented in a tsx file?

After transitioning a project from React to NextJS and working on the login page, I encountered an issue with server-side components not allowing the use of react-hook-form or @hookform/resolvers/yup. To make them work, it was necessary to add 'use cl ...

Distributing Requests with MongoSharding

Issue: Request Distribution Problem and System Troubles Recently, I implemented MongoDB Sharding on our production server with the following setup: I used 3 individual machines for configuring mongo sharding: First Machine - (shard1 primary + shard2 ...

notify a designated channel when ready for launch

I'm currently attempting to figure out how to send a message to a channel when the Discord bot is launched. I've experimented with this code: client.on('message', (message) => { client.on('ready', () => { channel = cli ...

Unable to find the module... designated for one of my packages

Within my codebase, I am utilizing a specific NPM package called my-dependency-package, which contains the module lib/utils/list-utils. Moreover, I have another package named my-package that relies on my-dependency-package. When attempting to build the pr ...

Tips for sending multiple Formdata in a single array to an express backend

How can I efficiently send multiple form data as an array to my express backend? When I try creating an array of formData, I end up receiving an empty object in Express. Even the payload in the network tab appears empty. let arr1 = {data: []} let data1 = ...

Exploring the capabilities of TabrisJs in conjunction with Upnp technology

Working with Upnp in TabrisJs seems to be a bit challenging. Even though it has good support for node packages, I am facing difficulties while dealing with Upnp. I included node-upnp-client in my package.json file. "dependencies": { "tabris": "^2.0 ...

Having trouble executing a query with variables in a Node.js, Express, MongoJS, and MongoDB environment

My task involves retrieving the output of a specific query in my application, which is currently yielding the desired result in mongoDb: var denominator = db.getCollection('Transactions').aggregate({ $group: { "_id": null, ...

Issue: The GoogleMapsAPI has not been properly defined for node-googlemaps

I am currently using node-googlemaps to access the Google Maps API through Node.js. As a Developer, I have generated my server API key as instructed in this documentation. According to the instructions, I need to create a publicConfig variable containing m ...

Difficulty encountered while setting up node and npm on Manjaro operating system

Operating System: Manjaro I added npm and nodejs using the following command: sudo pacman -S nodejs npm However, I encountered an error after installation: ~]$ npm -v node: error while loading shared libraries: libicui18n.so.67: cannot open shared ob ...

Display the revised file on an ejs template using Mongoose in a Node.js environment

I developed an application that allows users to create an account and update their information post-creation. The issue I'm facing is that even after saving the updated data to the database on the edit page, when users are redirected back to the user ...

"Encountered the error message "Self-signed certificate in certificate chain" while attempting to run the command 'node-gyp configure'

Encountered an error while attempting to build the Microsoft driver for Node.js for SQL Server gyp info it worked if it ends with ok gyp info using [email protected] gyp info using [email protected] | win32 | x64 gyp http GET https://nodejs ...