Configure Real-Time Socket.io Applications on cPanel Hosting
Configure Real-Time Socket.io Applications on cPanel Hosting
Introduction
Socket.io is a popular JavaScript library that enables real-time, bi-directional communication between clients and servers. Hosting real-time applications using Socket.io on cPanel requires special considerations due to shared hosting environments and limitations.
Prerequisites
- Active cPanel hosting with Node.js support (e.g., Hiverift or similar providers)
- Basic understanding of Node.js and Socket.io
- Access to cPanel dashboard and File Manager or SSH access
Step 1: Setup Your Node.js Application with Socket.io
Create or update your Node.js app to include Socket.io. Here’s a simple example:
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
io.on('connection', (socket) => {
console.log('New client connected: ' + socket.id);
socket.on('message', (msg) => {
console.log('Message received: ', msg);
io.emit('message', msg); // Broadcast to all clients
});
socket.on('disconnect', () => {
console.log('Client disconnected: ' + socket.id);
});
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
Step 2: Upload Your Application Files
Use cPanel’s File Manager or FTP to upload your Node.js app files (including package.json
, server.js
, etc.) to a directory within your home folder.
Step 3: Install Dependencies
If your hosting allows SSH access, connect via SSH and run:
npm install
If SSH is unavailable, upload the node_modules
folder from your local machine after running npm install
.
Step 4: Configure Node.js Application in cPanel
- Log in to cPanel dashboard.
- Navigate to Setup Node.js App.
- Create a new application or select your existing one.
- Set the Application Root to your app directory.
- Set the Application Startup File to your main file (e.g.,
server.js
). - Select the Node.js version supported by your app.
- Click Create or Update.
Step 5: Handle WebSocket Connections Behind Proxy
cPanel hosting typically uses Apache or NGINX as a proxy server. You must ensure WebSocket upgrade headers are properly forwarded.
If you have access to your domain’s .htaccess
file, add the following to enable WebSocket proxying:
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]
Note: Replace 3000
with your actual Node.js port if different.
Step 6: Restart Your Application
Use the Setup Node.js App interface in cPanel to restart your application so changes take effect.
Step 7: Test Your Real-Time Application
Open your application URL in multiple browsers or devices and test real-time features (e.g., chat messages) to verify Socket.io is working correctly.
Troubleshooting
- Connection Issues: Verify WebSocket support and proxy rules in
.htaccess
. - Port Conflicts: Ensure no other app is using the same port as your Node.js app.
- Resource Limits: Some shared hosting environments have limits on concurrent connections or long-running processes. Consider upgrading to VPS if needed.
Additional Resources
Need Support?
Contact Hiverift’s development support team at dev@hiverift.com for assistance deploying Socket.io apps on cPanel hosting.