Socket.io dilemma with clients.length (where clients === io.sockets.clients();)

Normally, when I send some JSON from one client to another, it works fine. But if there is only one client, it still sends packets. My solution was to (on the server-side, in node.js):

var clients = io.sockets.clients();

if(clients.length > 1){
// send stuff to other client
}

But when I do that, something extraordinarily strange occurs. I'll open up a client, start instigating the actions during the if statement, and the console, which prints the JSON before sending it, does nothing, because there is only one client. When I open up another client and instigate the actions during the if statement, the console will print out the JSON and send it to my other client. However, if I go over to my first client that I opened and instigate the actions in the if statement, the console won't print anything out, and it won't send anything. Why? I tried changing the if statement to say:

if("hello" === "hello"){
// do the same stuff as before
}

and both clients could send JSON to one another, but because I had changed the if statement, with one client open I would still send useless packets. For obvious reasons, the optimal situation would be if it wouldn't send packets with one client, but would send packets properly with more than one.

I hope that was clear, I'm sorry if that wasn't.

If I need to, I'll post more code.

EDIT:

Here is the code that updates the array:

var clients = io.sockets.clients();

io.sockets.on("connection", function (socket) {
clients.length++;
socket.on("disconnect", function() {
clients.length--;
console.log(clients.length + " clients are connected");
});
console.log(clients.length + " clients are connected");
});

Haven't done any note.js lately, maybe this is bogus. But I thought: maybe the clients variable is defined for each client when he connects, and not updated later on. This seems the logic thing to me. I think that what you need to do is: fire an event to all clients when a new client connects, and update the "clients" variable.

I'm not sure how your code works (what's this: clients.length++ ?? Incrementation of array's length??) but try doing this the "normal" way:

var clients;

io.sockets.on("connection", function (socket) {
  clients = io.sockets.clients();
  socket.on("disconnect", function() {
      clients = io.sockets.clients();
      console.log(clients.length + " clients are connected");
  });
  console.log(clients.length + " clients are connected");
});

or even better (without holding global reference to connected users):

function get_clients() {
  var clients = io.sockets.clients();
  console.log(clients.length + " clients are connected");
}

io.sockets.on("connection", function (socket) {
  socket.on("disconnect", function() {
    get_clients();
  });
  get_clients();
});

Because io.sockets.clients() returns the array of all connected clients at the moment of calling it (perhaps that's why you get this strange behaviour).