
WebSocket Integration in Node.js Apps on cPanel-Based Hosting
WebSocket Integration in Node.js Apps on cPanel-Based Hosting
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
- Log in to your cPanel dashboard.
- Navigate to Setup Node.js App.
- Create a new application or select the existing one.
- Set the Application Root directory and Startup File (e.g.,
server.js
). - Choose the appropriate Node.js version.
- 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.
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.