Client side start infinite request to the server in nodejs.

May 15, 2017 935 views
Node.js JavaScript Arch Linux

After starting server

node index.js

And when user connect through socket

socket = io("http://hostname:4000");

Client side didn't seems to be stable, it start doing infinite request and after every request socket id changed.
And this cause many problem, But this type of problem doesn't occur when i test on local server.
Request type is cross origin request.
Please tell me how i can resolve this problem.

  • Can you post the full code?

  • Client side code

    var socket;
    var n='sheshant';
     socket = io("http://hostip:4000",{query:'name='+n});
          socket.on('chatmsg', function(msg){
          var user = JSON.parse(msg);
          if(user.type=="user") {
            $("#msg_console").append("<div id='out_name'>""</div>"+"<div id='out_msg'>"+user.message+"<br>"+"<div class='downtimer'>"+user.time+"<br>";   
          } else {
                $("#msg_console").append("<div id='sys_msg'>"+user.message);

    Server side code

    var app = require('express')();
    var http = require('http').Server(app);
    var io = require('')(http);
    var date = require('date-and-time');
    var mysql = require('mysql');
    var pool      =    mysql.createPool({
        connectionLimit : 100, //important
        host     : 'localhost',
        user     : 'root',
        password : '',
        database : 'db',
        debug    :  false
    io.on('connection', function(socket){
        var store;
         console.log(store+" is connected");
    console.log(" is connected");
    socket.on('disconnect', function(){
         console.log(store+" is disconnected");
    console.log(" is disconnected");
    http.listen(4000, function(){
      console.log('listening on *:4000');
  • @mohsen47, see the above, This is the code.

Be the first one to answer this question.