title: How to send data from the express node.js server with socket.io
body: <p>I am learning how to use node.js, express and socket.io.</p>
  <p>I would like to send the results of a query in json format from the server to the clients every a range of time. I created the function <code>intervalFunc</code> to do that.</p>
  <p>By the way it doesn't work.</p>
  <p>This is my code in <code>index.js</code>:</p>
  <pre><code>var express = require('express');
  var socket = require('socket.io');
  
  var app = express();
  
  //php package
  var phpExpress = require('php-express')({
      binPath: 'php'
    });
  
  // set view engine to php-express
  app.set('views', './views');
  app.engine('php', phpExpress.engine);
  app.set('view engine', 'php');
  
  // routing all .php file to php-express
  app.all(/.+\.php$/, phpExpress.router);
  
  //App setup
  var server = app.listen(4000, function() {
      console.log('listening on requests on port 4000');
  });
  
  //Static files
  app.use(express.static('public'));
  
  //Socket setup
  var io = socket(server);
  
  io.on('connection', function(socket) {
      console.log('made socket connection', socket.id);
  
      socket.on('chat', function(data) {
          io.sockets.emit('chat', data);
      });
  
      socket.on('typing', function(data) {
          socket.broadcast.emit('typing', data);
      });
  
      
  });
  
  var mysql = require('mysql');
  
  var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'xxx',
    password : 'xxx',
    database: &quot;xxx&quot;
  });
  
  connection.connect();
  
  //function not working
  function intervalFunc() {
      connection.query('SELECT prov_id, prov_nome FROM provincie', function(err, rows, fields) {
          if (err) throw err; //error
          console.log('The solution is:', rows);
          io.sockets.emit('query_res', rows);
        
      });
  }
  
  setInterval(intervalFunc, 1500);
  
  connection.end();
  </code></pre>
  <p>The error that I receive is located in the <code>if</code> inside the function that is <code>if (err) throw err;</code>.</p>
  <p>This is the exception of the error:</p>
  <pre><code>C:\xampp\htdocs\socketio_project\index.js:60
          if (err) throw err;
                   ^
  
  Error: Cannot enqueue Query after invoking quit.
      at Protocol._validateEnqueue (C:\xampp\htdocs\socketio_project\node_modules\mysql\lib\protocol\Protocol.js:215:16)
      at Protocol._enqueue (C:\xampp\htdocs\socketio_project\node_modules\mysql\lib\protocol\Protocol.js:138:13)    
      at Connection.query (C:\xampp\htdocs\socketio_project\node_modules\mysql\lib\Connection.js:198:25)
      at Timeout.intervalFunc [as _onTimeout] (C:\xampp\htdocs\socketio_project\index.js:59:16)
      at listOnTimeout (internal/timers.js:549:17)
      at processTimers (internal/timers.js:492:7) {
    code: 'PROTOCOL_ENQUEUE_AFTER_QUIT',
    fatal: false
  }
  [nodemon] app crashed - waiting for file changes before starting...
  </code></pre>
  <p>Can help?</p>