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.

  • Running docker -d fails on Ubuntu 14.04
  • Dockerfile: Docker build can't download packages: centos->yum, debian/ubuntu->apt-get behind intranet
  • How to resolve docker connectivity after reboot on Mac
  • session replication using AWS tomcat and RDS
  • How to stop/start logstash service running in docker
  • Docker: using Using --volumes-from to backup data files
  • 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 can I execute my tests in Debug in PyCharm using a Docker Python Interpreter?
  • How to bind Jenkins build output with tests result?
  • Passing ES_JAVA_OPTS variable with spaces when using docker compose
  • Cannot setup multi-host Docker overlay network with etcd
  • Can you pass flags to the command that docker runs?
  • Backing up data volume containers off machine
  • 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.