Writting empty object with socket.io

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.

  • Unable to locate runnable browser in Docker container
  • Cannot start container : [8] System error: exec: “up3”: executable file not found in $PATH
  • Can't curl linked container in Docker even though I can ping it
  • log tag option ignored by docker daemon
  • docker native networking vs weave net/flux/show
  • Symfony2 app always has a 3 second wait between kernel.request and kernel.controller
  • 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] }
    

  • How to run 2 wordpress blogs using docker on ec2
  • How to compile C code that is using kernel function in docker and use pci device in container?
  • Docker CPU and memory too low
  • how to run a daemon process in docker container with API/v1.5?
  • Generating dockerfile of a gradle java application
  • Docker swarm deployment takes time and causes container to kill the service
  • 2 Solutions collect form web for “Writting empty object with socket.io”

    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!

    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);
    });
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.