How can I send packets between the browser and server with socket.io, but only when there is more than one client?

In my normal setup, the client will emit data to my server regardless of whether or not there is another client to receive it. How can I make it so that it only sends packets when the user-count is > 1? I'm using node with socket.io.

To do this you would want to listen to the connection event on your server (as well as disconnect) and maintain a list of clients which are connected in a 'global' variable. When more than 1 client is connected send out a message to all connected clients to know they can start sending messages, like so:

var app = require('express').createServer(),
    io = require('socket.io').listen(app);

app.listen(80);

//setup express

var clients = [];

io.sockets.on('connection', function (socket) {
    clients.push(socket);

    if (clients.length > 1) {
        io.socket.emit('start talking');
    }

    socket.on('disconnect', function () {
        var index = clients.indexOf(socket);

        clients = clients.slice(0, index).concat(clients.slice(index + 1));

        if (clients.length <= 1) {
            io.sockets.emit('quiet time');
        };
    });
});

Note: I'm making an assumption here that the socket is passed to the disconnect event, I'm pretty sure it is but haven't had a chance to test.

The disconnect event wont receive the socket passed into it but because the event handler is registered within the closure scope of the initial connection you will have access to it.