callbacks in browser after socket.io emit

i am developing a way to get callbacks in the browser page, following a emit to the socketio server.

server code:

/** exec php file and retrieve the stdout */
socket.on('php', function(func, uid, data) {
    console.log('php'.green + ' act:' + func + ' uid:' + uid);
    runner.exec("php " + php_[func].exec,
    function(err, stdout, stderr) { 
        if (err == null) {
            socket.emit('php', {uid: uid, o: stdout});
            console.log('emitted');
        } else {
            console.log('err '.red + stdout + ' ' + stderr);
        }
    });
});

this code executes a php page and retrieves the output to display or parse in the browser it receives an id to echo back to the page, so I can know what function to execute

browser code to execute callbacks:

    function log(text) {
        $('.out').append(text + '<br />');
    }
    window.callbacks = [];

    function setcb(c) {
        var max = 0;
        $.each(window.callbacks, function(index) {max = (index > max ? index : max);});
        window.callbacks[max+1] = c;
        return max+1;
    };
    function C(k){return(document.cookie.match('(^|; )'+k+'=([^;]*)')||0)[2]}

    var s = io.connect("http://"+ window.location.host +":8088");
    //s.emit('debug', 'here');
    s.on('getid', function(){
        console.log('getid cookieis: ' + C('igr') );
        s.emit('setid', C('igr'));
    });


    s.emit('php', 'test',
        setcb(
            function () {
                var data = JSON.parse(this);
                log('callback passed' + this);
            }
        ), null
    );

    s.on('php', function(data) {
        //window.callbacks[j.uid].call(j.o);
        log('rec' + JSON.stringify(data));
        //var jsn = JSON.parse(data);
        console.log(data);
        console.log(window.callbacks[data.uid]);
        window.callbacks[data.uid].call(data.o);
        delete window.callbacks[data.uid];
        window.callbacks.splice(data.uid, 1);
        console.log(window.callbacks);
    });

this is working, but when I try to make two requests at the same time, it doesn't run like expected, leaving one callback to execute and in the callbacks array.

test code:

    s.emit('php', 'test',
        setcb(
            function (data) {log('callback passed' + this);}
        ), null
    );
    s.emit('php', 'test',
        setcb(
            function (data) {log('callback passed' + this);}
        ), null
    );

I want to eliminate this error, and for each event received, execute the callback I define.

This is way more simple than I've imagined You can pass by reference the callback. server side code:

/** exec php file and retrieve the stdout */
socket.on('php', function(func, data, callback) {
    console.log('php'.green + ' act:' + func);
    runner.exec("php " + php_[func].exec,
    function(err, stdout, stderr) { 
        if (err == null) { 
            callback(stdout);
            //socket.emit('php', {uid: uid, o: stdout});
            console.log('emitted');
        } else {
            console.log('err '.red + stdout + ' ' + stderr);
        }
    });
});

client side code:

    function log(text) {
        $('.out').append(text + '<br />');
    }
    function C(k){return(document.cookie.match('(^|; )'+k+'=([^;]*)')||0)[2]}

    var s = io.connect("http://"+ window.location.host +":8088");
    //s.emit('debug', 'here');
    s.on('getid', function(){
        console.log('getid cookieis: ' + C('igr') );
        s.emit('setid', C('igr'));
    });


    s.emit('php', 'test', null,
        function(data) { log('d: ' + JSON.stringify(data)); }
    );