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.

  • Why Cluster NodeJS vs Docker, And What About Einhorn?
  • MySQL stops randomically with Magento 2
  • Add Ruby SDK from Docker container as a remote SDK on RubyMine
  • fish shell pass argument from command
  • Docker-compose config front-end with back-end's IP from host's perspective
  • How to extend existing docker container?
  • 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 use additional parameters for PhantomJS in a Docker container?
  • Cloudera and Docker
  • Docker for Windows Server 2016 requires update KB3176936
  • docker.local hostname in /etc/hosts not resolving on OSX for docker machine (causing rspec tests to fail)
  • Docker container sharing clipboard with host
  • Docker container cannot access network
  • 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.