WebSocket support depends on the hosting provider's server configuration and the resources available to your hosting account. Having Node.js support doesn't automatically guarantee that every real-time communication feature will work.
Before troubleshooting your code, check whether your hosting provider supports WebSocket connections on its cPanel environment.
If WebSockets are supported, investigate these areas:
- Make sure your Socket.IO server is attached to the correct HTTP server.
- Verify that your application is using the port assigned by the hosting environment.
- Check whether the proxy configuration supports WebSocket upgrades.
- Confirm that the client and server use compatible Socket.IO versions.
- Check whether connection timeouts or resource limits are causing disconnections.
For example:
const http = require('http');
const express = require('express');
const { Server } = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = new Server(server);
io.on('connection', (socket) => {
console.log('Client connected:', socket.id);
});
server.listen(process.env.PORT);
If the hosting environment doesn't support the required WebSocket configuration, you'll need to discuss alternative hosting options with your provider.