8

Here are 2 related questions. Posting them together makes more sense.

Question 1

I have a node.js app which emits an event out to all clients, and all current clients will respond with a ready emit. How can I create a list of all the clients that replied to the initial emit, and what kind of identification can be used to distinguish the clients?

Question2:

What I am trying to do after collect a list of connected clients, is to then access a MySQL database table of N number of rows and assign each client X rows each. These rows will be emitted back to their respective clients. How can this be done?

Current Code for Qn 1

Node Code

setInterval(function() {
    util.log('Checking for new jobs...');
    dbCheckQueue(function(results) {  // checks if there are new rows to "distribute" to clients
        if (results.length) {
            util.log(results.length + ' new jobs found.');
            io.sockets.emit('job_available');
        }
    });
}, 10*1000);

Client-side JS Code

socket.on('job_available', function() {
                console.log('Job Available.. Responding with Ready!');
                socket.emit('ready');
            });

io.sockets.on('connection', function(socket) {
    socket.on('ready', function() {
        // UPDATE N rows with client_id in column checkout.
        // Then SELECTS * from table where checkout = client_id
        getListings(client_id, function(listings) {
            socket.emit('job', listings);   // send jobs
        });
    });
});

Current Code for Qn 2 The code works for a single client, but how do I loop through all connected clients and perform the same updating of column and selecting of rows?

io.sockets.on('connection', function(socket) {
    socket.on('ready', function() {
        // UPDATE N rows with client_id in column checkout.
        // Then SELECTS * from table where checkout = client_id
        getListings(client_id, function(listings) {
            socket.emit('job', listings);   // send jobs
        });
    });
});

2 Answers 2

20

Socket.io provides you with a public api for that, so instead of hacking something up like Bryan suggest you can use:

io.sockets.clients()

That will returns an array of all connected clients.

If you want all clients connected to a certain namespace:

io.of('/namespace').clients()

But you can even filter it even more.. if you want to have all sockets in a room:

io.sockets.clients('room name here as first argument')

Will return a array of connected sockets for the room room name here as first argument

6
  • Thanks 3rdEden, I used Bryan's solution with slight modifications because I only want those connected clients that replied with a ready message to be pushed into an array. Can using the API do something that in a different way?
    – Nyxynyx
    Nov 28, 2011 at 23:05
  • Actually slightly hacking with the api would be better all you had to do was something like socket.isReady = true; when is ready is recieved and by that you would of saved yourself from mantaining another list :P Jan 22, 2012 at 5:49
  • @3rdEden You are the author of node-memcached? I am using it well :) Apr 11, 2014 at 17:19
  • Yes, I'm also the author of that module :-)
    – 3rdEden
    Apr 15, 2014 at 14:54
  • 11
    I believe the functionality in this answer is deprecated in 1.0 Mar 15, 2015 at 1:05
16

You will need to keep track of the connected clients yourself. The simple way to do that would be to use an array:

var clients = [];

io.sockets.on('connect', function(client) {
    clients.push(client); 

    client.on('disconnect', function() {
        clients.splice(clients.indexOf(client), 1);
    });
});

Then you can references that clients array on the server wherever you need to, in your ready event handler or whatever. Something like:

io.sockets.on('connection', function(socket) {
    socket.on('ready', function() {
        // UPDATE N rows with client_id in column checkout.
        // Then SELECTS * from table where checkout = client_id
        clients.forEach(function(client, index) {
            var client_id = index; // Just use the index in the clients array for now
            getListings(client_id, function(listings) {
                socket.emit('job', listings);   // send jobs
            });
        });
    });
});
3
  • could you proevide the logic of a getListings() function, because I applying smiliar solution but I see on screen on the name of a first logged user. Feb 14, 2016 at 10:48
  • The getListings call is just sample code that I pulled from the original question. I have no idea what it contains or how it's implemented.
    – rossipedia
    Feb 14, 2016 at 18:13
  • I see, because I am storing clients in array and when I try to retrieve them with the hepl of foreach or for loops I just only the the first logged in client's username in browser. Feb 15, 2016 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.