Question

I'm currently trying to send some information via Socket.io with Node.js but it looks like I'm doing something wrong and I really don't know why.

I have some containers on my machine using Docker and I use the Docker.io node package for my app in order to get all the information I want.

Here's the server side function that sends the data

io.sockets.on('connection', function(socket){
   socket.emit('message',container);
});


var container = docker.containers.list(handler);

function handler(err, res) {
if (err) throw err;
console.log("data returned from Docker as JS object: ", res);
}

Here's the client side code that gets the socket message

var socket = io.connect('/');
socket.on('message', function(data){
    console.log(data);
});

The data that I'm sending through the socket ( the container list looks) like this:

[ { Command: 'top ',
Created: 1393878688,
Id: 'fa46297fa16ff184673077545437a64f2adaf62db8774be696d76cc9f52b7881',
Image: 'ubuntu:12.04',
Names: [ '/cranky_archimedes' ],
Ports: [],
Status: 'Up 20 minutes' } ]

But the only thing I get client side is : Object {}

and the socket.io log is sending me this:

debug - websocket writing 5:::{"name":"message","args":[{}]}
[ { Command: 'top ',
Created: 1393878688,
Id: 'fa46297fa16ff184673077545437a64f2adaf62db8774be696d76cc9f52b7881',
Image: 'ubuntu:12.04',
Names: [ '/cranky_archimedes' ],
Ports: [],
Status: 'Up 20 minutes' } ]

The args section is empty. I really don't know how I can put the container list JSON into that arg so it can be send proprely. Am I missing something?

EDIT 1:

When I log before emitting my container variable looks like this:

{ callback: [Function: handler] }
Was it helpful?

Solution

There are actually a few errors here. First container is not what you think it is. It is not the result of docker.containers.list(), it is a function.

Consider the following example

var hello = function(){
  return "hello";
}
console.log(hello); // [Function]
console.log(hello()); // hello

hello is the function that returns "hello" but hello is not equal to "hello".

Basically when you are sending container, you aren't actually sending the result of docker.containers.list(), but the function itself.

Your second problem is that you are using an async function, but you are not waiting for the callback of this function to emit your data.

So basically with your current code, you are sending the wrong thing, at the wrong time.

What you need to do in order to make things work is probably something like this :

io.sockets.on( 'connection', function( socket ) {
  docker.containers.list( function( err, res ) {
    socket.emit( 'message', res );
  });
});

Haven't tried it, but it should work!

OTHER TIPS

your client code looks like this:

socket.on('message', function(data)){
    //this console.log is not actually inside the callback
    console.log(data);
}

It needs to look like this:

socket.on('message', function(data) {
    //here the log is inside the callback
    console.log(data);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top