title: how to show real-time data using node.js, socket.io and mysql?
body: <p>I am new to Node.js and trying to show real-time data to a web-page. What I try to do is first I insert into MySQL database via an URL. This is URL [<a href="http://localhost:4000/api/motor_details?deviceId=145&amp;a=sdfa3&amp;b=us&amp;c=uds]" rel="nofollow noreferrer">http://localhost:4000/api/motor_details?deviceId=145&amp;a=sdfa3&amp;b=us&amp;c=uds]</a>. Below code is able to insert into the database. The data is visible only refreshing the page but I want to see inserted data to my web-page without refreshing it.<br/>
  This is my index.js (Main File)</p>
  
  <pre><code>&lt;pre&gt;
  var express = require('express');
      var socket = require('socket.io');
      var mysql = require('mysql');
      var http = require("http");
      var app = express();
      var server = app.listen(4000,function(){
          console.log('Listening 4000');
      });
      var io = socket(server);
      app.use(express.static('public'));
  
      var connectSql = mysql.createConnection({
          host: "localhost",
          user: "root",
          password: "root",
          database: "grid_component",
          socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock'
      });
      connectSql.connect();
      app.get('/api/motor_details', function(req, res) {
          var device_id = req.param('deviceId');
          var a = req.param('a');
          var b = req.param('b');  
          var c = req.param('c');  
          var entries = {
              device_id: device_id,
              a: a,
              b: b,
              c: c,
              date: '2018-06-27'
          }
          var query = connectSql.query('insert into entries set ?', entries,function (err, result) {
              if(err){
                  console.error(err);
                  return;
              }  
          })
      });
      io.on('connection',function(socket){
          connectSql.query('SELECT * FROM entries',function(err,rows){
              if(err) throw err;
              socket.emit('showrows', rows);
          });
      });
  &lt;/pre&gt;
  
  &lt;pre&gt;
  var socket = io.connect('http://localhost:4000');
   socket.emit('showrows');
   socket.on('showrows', function(rows) {
       var html='';
       for(var i=0; i&lt;rows.length; i++){
           html += rows[i].device_id + ' ' + rows[i].a + '&lt;br&gt;';
       }  
       document.getElementById("display").innerHTML = html;
   }); 
  &lt;/pre&gt;
  
  [![Error Screenshot][1]][1]
  </code></pre>
  
  <p>I even tried to change the req.param with req.params, req.body or req.query but its showed undefined</p>