title: node.js Socket io 'Error:Client is not defined'
body: <p>I'm following a tutorial and trying to make a simple node.js server using mysql and an Ec2 instance. The problem is, I keep getting the error 'node.js:201
          throw e; // process.nextTick error, or 'error' event on first tick
                ^
  ReferenceError: client is not defined
      at Object. (/var/www/app.js:28:3)'</p>
  
  <p>I have no idea why it's doing that. I have the correct file loaded into my index.html:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
  &lt;head&gt;
          &lt;title&gt;&lt;/title&gt;
  
          &lt;script src="http://54.213.60.208:8080/socket.io/socket.io.js"&gt;&lt;/script&gt;
          &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"&gt;&lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
          &lt;script type="text/javascript"&gt;
  
          $(document).ready(function() {
    var socket  = io.connect('http://54.213.60.208:8080');
  
   $('#save').click(function() {
      if ($('#employee_name').val() == '' || $('#employee_salary').val() == '') {
        return alert('Please enter both name/salary!');
      }
      var data = {
        name: $('#employee_name').val(),
        salary: $('#employee_salary').val()
      };
      socket.emit('add employee', data);
      $('#employee_name').val('');
      $('#employee_salary').val('');
    });
  
   socket.on('populate', function(data) {
      var out = "";
      $.each(data, function(i, obj) {
        out += "&lt;li&gt;"+obj.name+" is making "+obj.salary+"&lt;/li&gt;";
      });
      $('#employees').html(out);
    });
  });
  
          &lt;/script&gt;
  
  &lt;b&gt;Create new employee&lt;/b&gt;
  &lt;div&gt;Name: &lt;input id="employee_name" value="" type="text"&gt;&lt;/div&gt;
  &lt;div&gt;Salary: &lt;input id="employee_salary" value="" type="text"&gt;&lt;/div&gt;
  &lt;div&gt;&lt;input value="Save" id="save" type="button"&gt;&lt;/div&gt;
  
  &lt;br&gt;
  &lt;b&gt;List of Employees:&lt;/b&gt;
  &lt;ul id="employees"&gt;&lt;/ul&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here's my server code: </p>
  
  <pre><code>var fs = require('fs');
  var db_helper = require("./db_helper.js");
  
  var app = require('http').createServer(function handler(req, res) {
    fs.readFile(__dirname + '/index.html', function(err, data) {
      if (err) {
        res.writeHead(500);
        return res.end('Error loading index.html');
      } else {
        res.writeHead(200);
        res.end(data);
      }
    });
  }).listen(8080);
  
  
  var io = require('socket.io').listen(app);
  io.sockets.on('connection', function(client) {
    console.log('Client connected');
  
    // populate employees on client
    db_helper.get_employees(function(employees) {
      client.emit('populate', employees);
    });
  });
  
  // client add new employee
    client.on('add employee', function(data) {
      // create employee, when its done repopulate employees on client
      db_helper.add_employee(data, function(lastId) {
        // repopulate employees on client
        db_helper.get_employees(function(employees) {
          client.emit('populate', employees);
        });
      });
  });
  </code></pre>
  
  <p>Finally, my Mysql file: </p>
  
  <pre><code>var mysql = require('mysql');
  var MYSQL_USERNAME = 'root';
  var MYSQL_PASSWORD = 'RMWpsu@13';
  
  var client = mysql.createConnection({
    user: MYSQL_USERNAME,
    password: MYSQL_PASSWORD,
  });
  
  // destroy old db
  client.query('DROP DATABASE IF EXISTS mynode_db', function(err) {
    if (err) { throw err; }
  });
  
  // create database
  client.query('CREATE DATABASE mynode_db', function(err) {
    if (err) { throw err; }
  });
  console.log('database mynode_db is created.');
  client.query('USE mynode_db');
  
  // create table
  var sql = ""+
  "create table employees("+
  " id int unsigned not null auto_increment,"+
  " name varchar(50) not null default 'unknown',"+
  " salary dec(10,2) not null default 100000.00,"+
  " primary key (id)"+
  ");";
  client.query(sql, function(err) {
    if (err) { throw err; }
  });
  console.log('table employees is created.');
  
  // function to create employee
  exports.add_employee = function(data, callback) {
   client.query("insert into employees (name, salary) values (?,?)", [data.name, data.salary], function(err, info) {
      // callback function returns last insert id
      callback(info.insertId);
      console.log('Employee '+data.name+' has salary '+data.salary);
    });
  }
  </code></pre>