Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js socket.io server callback not working

I'm working on a simple Node.js bi-directional client\server communication channel and I'm attempting to use socket.io on the server and socket.io-client on the client.

I've inserted the code below, as you'll see it's pretty basic stuff, and I'm running both on my local machine - to minimise complexity.

The behaviour I'm seeing is:

  1. I start the server.
  2. The server logs 'Server started' to the console.
  3. I start the client.
  4. The client logs 'Server is ready!'
  5. Nothing else happens...

What I'd expect is the server to log a 'Client is ready!' message and then the content ('Ready received').

I've even used WireShark to sniff the line and it does appear that the client is emitting the message, as designed - but the callback on the server isn't firing.

I'm running node v0.8.4 with express v3.1.0, socket.io v0.9.13 and socket.io-client v0.9.11 (all installed via npm).

Here's the server code...

var http = require('http'),  
    express = require('express'),
    app = express(),
    server = http.createServer(app);

app.configure(function(){
  app.use(express.static(__dirname + '/public'));
});

server.listen(8080);
console.log("Server started");

var io = require('socket.io').listen(server);

io.sockets
     .on('connection', function(socket){ 
          socket.emit('server ready', { msg: 'ready' }) ;
      })

     .on('comms', function(content) {
          console.log('Client is ready!');
          console.log(content);
     });

And here's the client code...

    var clientio = require('socket.io-client');
    var socket = new clientio.connect('http://localhost', { port: 8080 });

    socket
     .on('server ready', function(data){
          console.log('Server is ready!');
          socket.emit('comms', 'Ready received');
    })  

     .on('connect-error', function(error) {
          console.log('Connection Error\n' + error);
    })

     .on('error', function(error) {
          console.log('Socket Error\n' + error);
    })

The documentation and examples for both socket.io and socket.io-client are somewhat confused (to be charitable) and they appear to be a bit of a moving target... but from what I can tell, I think this should work.

I'm hoping someone can give me advice as to where I'm going wrong?

like image 960
Jon Deeming Avatar asked Feb 17 '13 15:02

Jon Deeming


People also ask

Why is Socket.IO not working?

First and foremost, please note that disconnections are common and expected, even on a stable Internet connection: anything between the user and the Socket.IO server may encounter a temporary failure or be restarted.

How do I connect to a Socket.IO server?

listen(port); // Create a Socket.IO instance, passing it our server var socket = io. listen(server); // Add a connect listener socket. on('connection', function(client){ console. log('Connection to client established'); // Success!

Does Socket.IO auto reconnect?

In the first case, the Socket will automatically try to reconnect, after a given delay.

How do I test a Socket.IO connection?

You can check the socket. connected property: var socket = io. connect(); console.


1 Answers

In your server you have this code:

io.sockets
 .on('connection', function(socket){ 
      socket.emit('server ready', { msg: 'ready' }) ;
  })

 .on('comms', function(content) {
      console.log('Client is ready!');
      console.log(content);
 });

What you should do is something like this:

io.sockets.on('connection', function(socket){
    socket.emit('server ready', { msg: 'ready' });

    socket.on('comm', function(content){
        console.log('Client is ready!');
        console.log(content);
    });

});
like image 146
udidu Avatar answered Sep 27 '22 23:09

udidu