sheshant
By:
sheshant

A process in nodejs automatically repeat for infinite time

May 15, 2017 174 views
Node.js Arch Linux

When i start a index.js file through command line

node index.js

It starts but, it print the result multiple time even a single user is connected.
Result is:-

7544816137 is connected
gX8dcI27WNRNww6QAAAd
gX8dcI27WNRNww6QAAAd
connected as id 277
7544816137 is connected
gX8dcI27WNRNww6QAAAd
gX8dcI27WNRNww6QAAAd
connected as id 277

This happens on digital ocean server only, It doesn't happen on localserver.
Please tell me a way to resolve this problem

2 comments
  • @sheshant
    Friend provide more info, How the client is connecting? Post more logs? Show us the source code?

  • Client side code

    var socket;
    $(document).ready(function(){
    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'>"+user.name+"</div>"+"<div id='out_msg'>"+user.message+"<br>"+"<div class='downtimer'>"+user.time+"<br>"+user.date);   
          } else {
                $("#msg_console").append("<div id='sys_msg'>"+user.message);
                callback(n,grp);
          }
    })
    })
    

    Server side code

    var app = require('express')();
    var http = require('http').Server(app);
    var io = require('socket.io')(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 =socket.handshake.query.name;
         console.log(store+" is connected");
    console.log(socket.id+" is connected");
    })
    socket.on('disconnect', function(){
         console.log(store+" is disconnected");
    console.log(socket.id+" is disconnected");
    })
    http.listen(4000, function(){
      console.log('listening on *:4000');
    });
    
Be the first one to answer this question.