Question

Client side start infinite request to the server in nodejs.

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.

Subscribe
Share

@mohsen47, see the above, This is the 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');
});

Can you post the full code?


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Sorry to see that your question hasn’t received an answer yet. Unfortunately, after this much time, it is unlikely that an answer for this specific question will be provided. For people still landing here via search, please open a new question with your code attached and we’d be happy to take a look.