Node.js and node-mysql - can't broadcast/emit to a site live

I'm fairly new to node.js and am trying to ping my mysql database to get the information from a table and place it in a socket that multple browsers can view in real-time. I originally was getting the mysql information through an AJAX request to a Ruby generated page, but my server started to chug when I had a lot of users (the call was happening 1nce a second). That's why I'm moving forward with this node.js solution so that the server can run the request 1nce a second without each browser making the same request every second. If I can get the server to iterate and ping the mysql database and allow the browser to pipe into a socket with the information - I believe my server load will be fine.

My code is the following:

var http = require('http');
var mysqlClient = require('mysql').createClient({'user':'root','password':'tester123'});

mysqlClient.query('USE conferenceLine');
var mysqlData = [];

setInterval(function(){
mysqlClient.query(
  'SELECT number, page_id FROM callers',
  function selectCb(err, results, fields) {
    if (err) {
      throw err;
    }

    //if there are callers log all the callers
    for(var i = 0; i < results.length; i++){
        mysqlData[i] = results[i];
        console.log(mysqlData[i]);
    }

 });
}, 1000);


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

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

    console.log("New Connection: ", client.id); //log new connection

    client.emit('connection', client.id);//emit to let site know its connected

    //broadcast latest mysql response as json
    for(var i=0; i<mysqlData.length; i++){
        client.broadcast.emit('init', JSON.stringify(mysqlData[i]));
        console.log("this just happend");
    }

    client.on('disconnect', function() {
    console.log("Disconnected: ", client.id);
    });
});

When I delete the "broadcast" in "client.broadcast.emit('init', JSON.stringify(mysqlData[i]));" I'm seeing the mysql request result in my browser, but it's not live/real-time. i.e. I have to refresh my browser each time to view the result. How can I make this connection persistent and get the mysql data to pipe through a socket? Any help would be greatly appreciated.

you have on the client side already an event which handles the init event, basically what you need to do is the following:

create a button on the client side (for testing at least):

HTML:

<input type="button" id="testButton" onClick="requestData();">

on the javascript side of things on the client add the following:

function requestData(){
    socket.emit("requestNewData", {"nothing":"here"});
}

on the Node side add the following:

socket.on("requestNewData", function(data){
    client.emit('init', JSON.stringify(mysqlData[i]));
});

since you already have the 'init' event binded to display whatever you receive from Node, this'll work for testing purposes.

do remember this: Node.js with socket.io is basically dependent on events and event listening, the good part of this is that you can setup whatever event names you want for it to work with.

Hope I could be of help.