WebSocket Integration in Node.js Apps on cPanel-Based Hosting

WebSocket Integration in Node.js Apps on cPanel-Based Hosting

WebSocket Integration in Node.js Apps on cPanel-Based Hosting

WebSocket Integration in Node.js Apps on cPanel-Based Hosting

How to enable real-time WebSocket communication in your Node.js apps hosted on cPanel environments

Introduction

WebSockets enable persistent, bi-directional communication between clients and servers, essential for real-time applications like chat, notifications, or live updates. Integrating WebSockets with Node.js on cPanel hosting requires some special steps due to proxy servers and shared hosting limitations.

Prerequisites

  • Active cPanel hosting plan with Node.js support
  • Basic knowledge of Node.js and WebSocket concepts
  • Access to cPanel dashboard and File Manager or SSH access

Step 1: Create a Simple WebSocket Server with Node.js

Here is a basic example using the popular ws WebSocket library:

const WebSocket = require('ws');

const server = new WebSocket.Server({ port: 3000 });

server.on('connection', (socket) => {
  console.log('Client connected');

  socket.on('message', (message) => {
    console.log('Received: ' + message);
    // Broadcast message to all connected clients
    server.clients.forEach(client => {
      if (client !== socket && client.readyState === WebSocket.OPEN) {
        client.send(message);
      }
    });
  });

  socket.on('close', () => {
    console.log('Client disconnected');
  });
});

console.log('WebSocket server running on port 3000');

Step 2: Upload Your Node.js App

Upload your application files (including package.json and your server script) to your hosting account using cPanel’s File Manager or FTP.

Step 3: Install Dependencies

If SSH is available, navigate to your app directory and run:

npm install ws

If not, install dependencies locally and upload the node_modules folder.

Step 4: Configure Node.js Application in cPanel

  1. Log in to your cPanel dashboard.
  2. Navigate to Setup Node.js App.
  3. Create a new application or select the existing one.
  4. Set the Application Root directory and Startup File (e.g., server.js).
  5. Choose the appropriate Node.js version.
  6. Save and start the application.

Step 5: Enable WebSocket Proxying with .htaccess

Since cPanel hosting uses Apache as a proxy, you need to configure your .htaccess file to support WebSocket connections:

RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) ws://localhost:3000/$1 [P,L]

RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*) http://localhost:3000/$1 [P,L]

Replace 3000 with your Node.js app’s port if different.

Step 6: Test Your WebSocket Server

Open your app URL and connect clients to test WebSocket functionality. Use browser console or tools like WebSocket Echo Test to validate the connection.

Tip: If you encounter connection issues, check error logs in cPanel and ensure ports are open and proxy settings are correct.

Troubleshooting & Best Practices

  • Ensure no firewall blocks your WebSocket port.
  • Use environment variables to configure ports and other sensitive data.
  • Consider using socket.io if you need fallback options for older browsers.
  • Keep your Node.js app running by restarting via cPanel or using process managers if supported.

Additional Resources

Need Assistance?

Contact dev@hiverift.com for expert help deploying WebSocket-enabled Node.js apps on cPanel hosting.

© 2025 Hiverift. All rights reserved.

Post Your Comment

Build Your Website with HiveRift

From professional business to enterprise, we’ve got you covered!

©2025, Hosting. All Rights Reserved by KhatuShyam Technologies

HiveRift
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.