Introduction
Please keep in mind the installation, configuration, and management of Socket.IO is best handled by a developer or systems administrator with the skills, training, and expertise required to do so for you.
Socket.IO is a third-party application that is not created by, provided by, or supported by cPanel. This guide is provided as a courtesy to help you get started with some of the cPanel specifics that are different from the official guide.
Please refer to the official Socket.IO documentation or a developer for any topics not covered in this guide. This guide is based on the following official guide:
https://socket.io/get-started/chat/
Step 1: NodeJS installation for cPanel
Access SSH or Terminal as the root user and run the following command to install the required packages:
yum install ea-ruby27-mod_passenger ea-apache24-mod_env ea-nodejs16
For each cPanel user that would use NodeJS & NPM on the command line, issue the following command, ensuring to replace USERNAME with the username of the cPanel user. This enables the user to access the node and npm commands easily.
echo "export PATH=$PATH:/opt/cpanel/ea-nodejs16/bin/" >> /home/USERNAME/.bashrc
Step 2: App Setup
Access SSH or Terminal as the cPanel user. If you are the root user already, you can switch to the cPanel user easily with the following command:
su -l -s /bin/bash USERNAME
Create the following directory:
mkdir /home/USERNAME/socketio
Change to that directory:
cd /home/USERNAME/socketio
Create the following package.json file:
/home/USERNAME/socketio/package.json
{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {}
}
Install the required packages with npm
npm install express
npm install socket.io
Create the following app.js file
NOTE: On cPanel you would use app.js instead of index.js
NOTE: Due to the use of Passenger, console.log must be redefined. This app.js shows how that is done. Logs will be written to /home/username/socketio/debug.log
/home/USERNAME/socketio/app.js
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
var fs = require('fs');
var util = require('util');
var log_file = fs.createWriteStream(__dirname + '/debug.log', {flags : 'w'});
var log_stdout = process.stdout;
console.log = function(d) { //
log_file.write(util.format(d) + '\n');
log_stdout.write(util.format(d) + '\n');
};
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
socket.on('chat message', (msg) => {
console.log(msg);
io.emit('chat message', msg);
});
});
http.listen(3000, () => {
console.log('listening on *:3000');
});
Create the following index.html file:
/home/USERNAME/socketio/index.html
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: 0.5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<h1>Socket.IO Chat Test</h1>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
$(function () {
var socket = io();
$('form').submit(function(e){
e.preventDefault(); // prevents page reloading
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
});
</script>
</body>
</html>
Step 3: App Registration with Application Manager
1. Login to cPanel as the cPanel user.
2. Under the software section click on the "Application Manager" icon.
3. Click the blue "Register Application" button
4. Enter "Testing SocketIO" as the Application name
5. Select your desired domain from the "Deployment Domain" dropdown
6. Leave the "Base Application URL" as /
7. Enter socketio as the "Application Path". This translates to: /home/USERNAME/socketio
8. Select "Development" as the environment
9. There is no need for environmental variables at this time.
10. Scroll to the bottom of the page and click the blue "Deploy" button
Now visit your domain to view the test chat application.
Whenever you make changes to the app.js file, you will need to go into the Application Manager and redeploy the application for the new changes to take effect.