Express/Socket.io: The right way for one socket per request

I'm trying to build a simple Node app using Express, Socket.io, and the ntwitter module so you can just search for a term (that's req.params.searchTerm below) and ntwitter will search for it and output the stream via socket.io.

I'm running into errors, though, with multiple windows open, or closing a window and then opening a window immediately afterwards. Sometimes the search term is not updated, sometimes some kind of socket.io error is actually thrown. Could someone clarify the right way to do this or shed some light to my error? Thanks in advance. Code below.

var server = http.createServer(app);

var io = require('socket.io').listen(server);

server.listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

app.get('/:stream', function(req, res) {
 res.render('stream', { title: 'Search Twitter | ' + req.params.searchTerm });
    io.sockets.on('connection', function(socket) {

    var publishFlag = true;
    setInterval(function() {
        publishFlag = true;
    }, 1000);

    twit.stream('statuses/filter', { track: [req.params.searchTerm] },
        function(stream) {
            stream.on('data', function(tweet) {
                if(publishFlag) {
                    socket.emit('tweet', tweet);
                    publishFlag = false;
                }
            });
        }
    );
  });
});

It think that is somewhat what you are trying to do :

Server side :

var server = http.createServer(app);

var io = require('socket.io').listen(server);

server.listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

app.get('/:stream', function(req, res) {
 res.render('stream', { title: 'Search Twitter | ' + req.params.searchTerm });
});

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

    socket.on('stream', function(searchTerm){

        twit.stream('statuses/filter', { track: [searchTerm] },
            function(stream) {
                stream.on('data', function(tweet) {
                    socket.emit('tweet', tweet);
                });
            }
        );
    };
});

client side (/:stream)

var socket = io.connect('http://localhost');
socket.on('tweet', addTweetOnPage);
socket.emit('stream', searchTerm);