0

if I emit from serverside it is imperative that the code can only respond with "on" at clientside?

My Problem is that if a user login he gets a specific users list as object.

Imagine A logs in, this gets a custom object with the contents of user B, G and X. Now is the problem, that the users B, G and X are each also have a custom object. Of course, everything on the server side.

Now I tried just a server side emit to send to these users. This emitted should also be received on the server side with an on. However, it does not work.

I tried something like this

io.sockets.on('connection', function(socket){
    socket.on('login', function(){
            for(var key in specific_object){
                specific_object[key].emit('add user');
            }
    });

    socket.on('add user', function(data){
            console.log("now please....");
    });
 });

hopefully someone can help me :)

UPDATE

enter image description here.jpg)

EDNA
  • 139
  • 2
  • 11
  • Server side socket can't emit another socket on server. Similarly, a Client Side socket can't emit another client side socket. What you want to do is still not clear to me. Please add some more server and client side sockets you want to emit and explain properly. – Harpreet Singh Jun 22 '14 at 03:16
  • [Picture](http://www.myimg.de/?img=problem03632.jpg). I added a picture of the problem. – EDNA Jun 22 '14 at 10:35
  • 1
    what I got is you want to emit - "adduser" from "login" and both are server side socket. if yes then again , you can't emit a socket defined at server side from another server's socket. You have got an answer, it may help you. – Harpreet Singh Jun 22 '14 at 10:46

1 Answers1

0

If I understand you correctly, you may create a function and call it from both login and add user events:

function addUser(socket, data) {
    console.log("now please....");
    //do something with socket and passed data
}

io.sockets.on('connection', function(socket){
    socket.on('login', function(){
        for(var key in specific_object){
            //specific_object[key].emit('add user');
            addUser(specific_object[key], {your: 'data'});
        }
    });

    socket.on('add user', function(data){
        addUser(socket, data);
    });
});
Oleg
  • 22,300
  • 9
  • 68
  • 84