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.

  • Building a new docker image with the same name as an existing ones
  • Deis.io - How to debug apps which aren't running?
  • Docker raise an error when tried to create a data volume
  • Starting oracle with docker: “TNS:listener: all appropriate instances are blocking new connections” error
  • How to access a directory in hosts machine from inside a docker container?
  • Why did host network.service restart sever communications with all containers?
  • 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] }
    

  • Docker container does not start on executing docker run command
  • How can a Docker container tell whether its memory was limited?
  • Docker: Compressing Directories in Image
  • Fedora docker.io seems to not support btrfs
  • Docker + Kubernetes build
  • How do I deal with security updates in Docker images?
  • 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.