Load chat messages upon page load using websockets on node.js

Hi I'm developing a chat application using nodejs I'm new to node so I'm not very well familiar on its capabilities... I have made my application store its chat messages on mysql database only but I need to also display the past message and current one of a user here is the index.js

    var mysql =  require('mysql'); 
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var validator;

var connection =  mysql.createConnection({ // setup the connection
        host : "localhost",
        user : "root",
        password: "",
})
function getStdout(command, args, fn) {
    var childProcess = require('child_process').spawn(command, args);
    var output = '';
    childProcess.stdout.setEncoding('utf8');
    childProcess.stdout.on('data', function(data) {
        output += data;
    });
    childProcess.on('close', function() {
        fn(output);
    });
}

app.use('/assets', require('express').static(__dirname + '/assets'));
app.use('/temp', require('express').static(__dirname + '/temp'));
app.get('/', function(req, res){
  //res.sendfile(__dirname + '/' +validator);
  res.send(validator);
});

//you should have only one io.on('connection')
io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    console.log('message: ' + msg);

       var myMsg= msg; // obtain the incoming msg
  var strQuery = "INSERT INTO chat_storage(chat) VALUES(?)"; // your SQL string

  connection.query("use schat"); // select the db
  connection.query( strQuery, myMsg, function(err, rows){
     if(err) {
        // handle errors
     } else {  
      io.emit('chat message', msg);
        // message received
     }
   }); 

  });
});

getStdout('php', ['message.php'], function(output) {
  validator = output;
  //start your server after you get an output
  http.listen(3000, function(){
    console.log(validator);
  });
});

now here is the page for loading the chat messages

    <?php startblock('script') ?>

 <script src="/socket.io/socket.io.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      $(document).ready(function(){

      $.ajax({ 
        url: "localhost:3000/includes/message/store_chat.php",
        type: "POST",
          dataType: "html",
        success: function (result) {
          $("#messages").html(result);

        }
      });

      });

      var socket = io();
      $('form').submit(function(){
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
      });
      socket.on('chat message', function(msg){

         $('#messages').append($('<li>').text(msg));
});
</script>
<?php endblock(); ?>

My idea was to the chat messages once the page loads I was trying to achieve it using ajax as you can see on the script that I have provided.. but it was no good didnt work at all Please help me

Couple of suggestions:

1) Store all of your messages in-memory ( unless you see this growing to several MB of data ) so that you can catch up any new client quickly.

2) Use socket.io to send the chat messages that have been stored rather than an AJAX call.

I've also included SequelizeJS instead of raw MySQL - It has a much cleaner raw query model and allows you to transition into a DAO model of sorts if you want to.

app.js

// Highly suggest replacing raw mysql with SequelizeJS - http://sequelizejs.com/
var Sequelize = require('sequelize'),
  app = require('express')(),
  http = require('http').Server(app),
  io = require('socket.io')(http);

var validator;
var messages = [];

var sequelize = new Sequelize('schat', 'root', '');

app.use('/assets', require('express').static(__dirname + '/assets'));
app.use('/temp', require('express').static(__dirname + '/temp'));
app.get('/', function(req, res){
  res.send(validator);
});

io.on('connection', function(socket){
  // Send all previously sent messages
  for( i in messages ) {
    socket.emit('chat message', messages[i]);
  }

  socket.on('chat message', function(msg){
    console.log('message: ' + msg);

    // Push the message into the in-memory array.
    messages.push(msg);

    // Storage the message for when the application is restarted.
    sequelize.query('INSERT INTO chat_storage(chat) VALUES("'+msg'")').success(function() {
      // Insert was successful.
    }).error(function (err) {
      // Error inserting message
    });

    // Send the message to everyone
    socket.broadcast.emit('chat message', msg);
  });
});

function getStdout(command, args, fn) {
  var childProcess = require('child_process').spawn(command, args);
  var output = '';
  childProcess.stdout.setEncoding('utf8');
  childProcess.stdout.on('data', function(data) {
    output += data;
  });
  childProcess.on('close', function() {
    fn(output);
  });
}

// Load Messages
sequelize.query('SELECT chat FROM chat_storage').success(function (rows) {
  for( i in rows ) {
    messages.push(rows[i].chat);
  }
  getStdout('php', ['message.php'], function(output) {
    validator = output;
    http.listen(3000, function(){
      // Start server.
    });
  });
}).error(function (err) {
  // Error!
});

php include

<?php startblock('script') ?>

<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
  var socket = io();
  $('form').submit(function(){
    socket.emit('chat message', $('#m').val());
    $('#messages').append($('li').text($('#m').val()));
    $('#m').val('');
    return false;
  });
  socket.on('chat message', function(msg){
    $('#messages').append($('<li>').text(msg));
  });
</script>
<?php endblock(); ?>