sheshant
By:
sheshant

After well connected, client side automatically start sending multiple connection request everytime to socket.io

June 15, 2017 533 views
JavaScript Node.js Ubuntu 16.04

when i connect client side to server side over cross origin, Client side start sending multiple connection request every time.
My client side code:-

 var socket; $(document).ready(function(){
 var n="hello";
 socket = io("ws://ip_address:4000",{query:'name='+n}); 
socket.on('connect', function() { 
if(socket.connected==true) { 
$("body").fadeIn('slow');
 }
 });
 socket.on('disconnect', function() { 
if(socket.disconnected==true) { 
$("body").fadeOut('slow'); }
 });

Server Side

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 : 'my_pass' 
database : 'db', 
debug : false
 });
io.on('connection', function(socket){ 
var store =socket.handshake.query.name; 
console.log(store);
 console.log(socket.id); 
});

And the console.log result in SSH is

sheshant@nodejs-1gb-blr1-01:~/chatmachine$ node index.js
 listening on *:4000
 sheshantsinha@gmail.com
 YwbTs36JwRQ_SNxPAAAA
 sheshantsinha@gmail.com
 jsrVBh4fvKdicCX4AAAB 
sheshantsinha@gmail.com
 WJ3hdI2vftyyey5LAAAC
 sheshantsinha@gmail.com
 5fhlkkxkf9VWHUDnAAAD
 sheshantsinha@gmail.com
 VC-0GfN6L_QvnOVVAAAE
 sheshantsinha@gmail.com 
JF7cxxbItBTq_x4JAAAF 
sheshantsinha@gmail.com 
BdvIAHCCkqox91mVAAAG
 sheshantsinha@gmail.com
 h2iZ5IgQMnNWg3rKAAAH 
sheshantsinha@gmail.com
 7KVUL7u2ZlFWgsn_AAAI
 sheshantsinha@gmail.com
 rlPWNyQI0xFnwxhEAAAJ
 sheshantsinha@gmail.com
 ihAPsPCuQEPKiinSAAAK
 sheshantsinha@gmail.com
 FMf4hV3qKmilkocNAAAL 
sheshantsinha@gmail.com
 6shVbfFMMzCy2SmRAAAM 
sheshantsinha@gmail.com
 fwWlKt52V2WNgvgoAAAN 
sheshantsinha@gmail.com
 -iELmL8muGMZfT8SAAAO
 sheshantsinha@gmail.com

But i am expecting only one connection request per user

sheshant@nodejs-1gb-blr1-01:~/chatmachine$ node index.js
 listening on *:4000
fwWlKt52V2WNgvgoAAAN
 sheshantsinha@gmail.com

When i do Inspect element and open network, There i see that, It send below request many time even after connection.

http://MY_IP_ADDRESS:PORT/socket.io/?name=sheshantsinha%40gmail.com&EIO=2&transport=polling&t=1497514896811-26

Request Method:GET 
Status Code:200 OK

How one can resolve this problem

Be the first one to answer this question.