3

I run a standalone server which listens on port 5000. When I connect to it by Netcat or Telnet, the app doesn't print on terminal anything, but on the Netcat/Telnet application screen, it shows that connection has been established.

var io = require('socket.io')();
io.on('connection', function(socket){
console.log('Socket connection established'); 
}); 
io.listen(5000); 
console.log('Listening to port 50000');
Behdad
  • 1,459
  • 3
  • 24
  • 36
  • This means it working. no? – Smit Mar 01 '17 at 17:32
  • @smit This means,If it works why it doesn't print anything on the terminal? – Behdad Mar 01 '17 at 17:35
  • Did `console.log('Listening to port 50000');` print? – Smit Mar 01 '17 at 17:37
  • @Smit Yes it prints, but nothing print about "Socket Connection established". This is my exact problem. – Behdad Mar 01 '17 at 17:42
  • Can you share your whole server `.js` file? including the `require`s – Smit Mar 01 '17 at 17:47
  • @Smit Thank you for helping. What I wrote is whole server `.js` file. And `require` is also included. – Behdad Mar 01 '17 at 17:56
  • This is my server.js file `var app = require('express')(); var server = require('http').createServer(app); server.listen(process.env.PORT || 3001) app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); }); var io = require('socket.io')(server); io.on('connection', function (socket) { console.log("Socket connected :"+socket.id); socket.emit('news', { hello: 'world' }); });` I agree its huge, but i am not listening on io but i am listening on http! – Smit Mar 01 '17 at 18:01
  • @Smit Yes you are right. but I really need it running standalone. Thank you by the way. – Behdad Mar 01 '17 at 18:16
  • sockets.io will not work for tcp sockets. Dont try to connect with telnet. If you want to use tcp sockets try net library for nodejs https://nodejs.org/api/net.html – Sharjeel Ahmed Mar 01 '17 at 18:35
  • @SharjeelAhmed Thank you for your information. – Behdad Mar 01 '17 at 18:37

1 Answers1

4

Socket.io is a library for websockets.

When you connect with telnet, it says that you are connected. This is sufficient to know that your server is listening.

If you want to see it in action, you have to connect to it using a socket.io client. For this, just include /socket.io/socket.io.js in an html file.

This is enough to establish the connection.

 socket = io.connect('http://localhost:5000');

The reason, why console.log is not displayed is that the websocket did not connect. Only the tcp/ip connection was established, this is what telnet is telling you.

Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
  • Socket.io is not a library for WebSockets. Its a library for realtime bi-directional communication that supports communication over multiple transports and by default **does not** use WebSockets but XHR Longpolling. WebSockets will only be used if they are supported on both sides of the exchange and can be successfully upgraded to. – peteb Mar 01 '17 at 18:33
  • @LorenzMeyer Thank you, your solution works. Now I know what was my mistake. – Behdad Mar 01 '17 at 18:35
  • @peteb You're right saying 'socket.io is not a library for websockets'. It is for establishing a continuous connection to the server, that allows the server to push events to the client. I don't agree 'by default does not use websockets'. If the browser supports websockets, it uses websockets, and will fall back on other techniques like xhr longpolling, if websockets are not available. – Lorenz Meyer Mar 01 '17 at 19:13
  • @LorenzMeyer It is incorrect to say that it falls back to longpolling. It starts with longpolling and will upgrade to websockets if possible. – peteb Mar 01 '17 at 19:15