How to use a json data for socket emit from client side?
<p>What is the best method to use a mysql fetched data during socket emiting time?
For example, I will have a data retrieved from database and now I would like to use that data on <code>socket.emit('emit_name', function(data){ })</code>.What will be the best way?</p>

<p>I firstly, tested with easy way to find if it works or not rather then diving beginning into complicated one from json.I used two data as 'type' and 'fruit' something like this on "Client side"</p>

<pre><code>var socket = io.connect( 'http://localhost:8080' );
var nameVal = "Type";
var msg = "apple";

socket.emit( 'messages', { type : nameVal, fruit: msg } );
</code></pre>

<p>on Server side</p>

<pre><code>var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );
var app = express();
var mysql = require('mysql');
var server = http.createServer( app );
var io = socket.listen( server );
var pool = mysql.createPool({
connectionLimit : 10,
host: "localhost",
user: "root",
password: "",
database: "mydb"
  }
);


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

console.log( "User connected..!" );

io.client.on( 'messages', function(data) {
console.log( 'Message received ' + data.type + ":" + data.fruit );

  });

});

server.listen(8080);
</code></pre>

<p>After doing this all I just started my node server and I get "connected" on my socket on page load but my work on socket side isn't working at all as I wised.The result(problem) on socket part of server side gives this error as below image :- </p>

<p><a href="https://i.stack.imgur.com/eGU1a.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eGU1a.png" alt="enter image description here"></a></p>

<p>What might be the problem?How can I solve this issue?</p>