Assuming this is a remote Ubuntu server, could it be that the firewall doesn't allow connections on port 4000? Node could listen all that it wants, but if this requests don't even get to the server, nothing will happen.
Mar 5, 2018
9
Level 25
Socket.io Not working in live server Ubuntu
Hi
Am trying with socket.io and I made successful run on localhost. But when I tried to implement the same on server it doesn't work.
Server Code
var server = require('http').Server();
var io = require('socket.io')(server);
server.listen(4000, '0.0.0.0', function () {
console.log('listening');
});
io.on('connection', function(socket) {
console.log('Client connected.');
// Disconnect listener
socket.on('disconnect', function() {
console.log('Client disconnected.');
});
});
Client Code
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
<script>
//for localmachine
var socket = io.connect(':4000');
//for server
var socket = io.connect(':4000');
console.log(socket.connected);
console.log(socket.connecting);
socket.on( 'connection', function () {
console.log( 'connected to server' );
} );
console.log(socket);
socket.on('connect_error', handleNoConnect);
socket.on('connect_timeout', handleNoConnect);
socket.on('connect', onConnect);
function handleNoConnect() {
console.log("No connection");
}
function onConnect() {
console.log("connected");
// set other event handlers on a connected socket
socket.on('disconnect', function() {
console.log("disconnected");
});
}
socket.on('connection',function (data) {
console.log(data);
});
</script>
Though I checked the 4000 port is listening in IPV4
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 9899 buffercode 12u IPv4 3246821 0t0 TCP *:4000 (LISTEN)
Am getting No Connection.
Kindly suggest me where I did the mistake
Level 17
@vinoth06 you could use a telnet session from your local machine to try and reach it. I'm guessing this is not the issue, but it can't hurt to check.
You can do it with any terminal in Windows (needs telnet enabled through the extra Windows options), Linux or Mac:
telnet yourhostnameorip 4000
It should look like this:
// This one doesn't connect
$ telnet google.com 4000
Trying 216.58.211.110...
// This one does connect
$ telnet google.com 80
Trying 216.58.211.110...
Connected to google.com.
Escape character is '^]'.
Please or to participate in this conversation.