title: Two way communication between server and client on socket.io node.js
body: <p>I am trying to write code for server in node.js in which, client running on browser will send some data to server when some ".on" event will occur. Now at server side the task is to receive data coming from client and send that data back to client.</p>
  
  <p>I am using socket.io.
  Write now I am doing this as,
  Client side:</p>
  
  <pre><code>&lt;p id="ValSlider1"&gt; Curr Val &lt;/p&gt;
  &lt;input class = "mySlider1" type="range" name="slider" id="slider-0" value="0" min="0"     max="100" /&gt;
  
  &lt;script&gt;
  
  var socket = io.connect('http://localhost');
  
  $(".mySlider1").change(function() {
  var sVal = $(this).val();
  socket.emit('ValSlider1', sVal);
  console.log('ValSlider1: ' + sVal );
  });
  
  socket.on('packet', function (data) {
  var valFromServer = data.split('-');
  
  document.getElementById("ValSlider1").innerHTML =   valFromServer[0];
  document.getElementById("ValSlider2").innerHTML =   valFromServer[1];
  document.getElementById("ValSlider3").innerHTML =   valFromServer[2];
  document.getElementById("ValSlider4").innerHTML =   valFromServer[3];
  
  $('#container1').html(data);
  });
  </code></pre>
  
  <p></p>
  
  <p>and on server side:</p>
  
  <pre><code>var qs = require('querystring'),
  fs = require('fs'),
  parser = new require('xml2json'),
  urr = require('url'),
  app= require('http').createServer(handler).listen(3000),
  io = require('socket.io').listen(app);
  
  function handler (req, res) {
  var reqObj = urr.parse(req.url, true);
  var reqPath = reqObj.pathname;
  if ('/' == reqPath ) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  fs.readFile(__dirname + '/client.html', function(err, data) {if     (err)                   {res.writeHead(500);
  return res.end('Errorloadingclient.html');
  }
  res.end(data);
  });
  } 
  };
  
  var slider1, slider2, slider3, slider4 ;
  
  io.sockets.on('connection', function(socket) {
  
  socket.on('ValSlider1', function(data){
  slider1 = data ;
  socket.emit('packet', data);
  console.log("Slider 1 Value: " + data);
  }); 
  });
  
  setInterval(function () {
  var data = slider1 + "-" + slider2 + "-" + slider3 + "-"    + slider4; 
  socket.emit('packet', data);
  console.log(data);
  },1000);
  });
  app.maxConnections = 1;
  </code></pre>
  
  <p>Now, when communication starts initially two-three slider change values are received by server and printed on console but then the values not get updated, server sends previous values only to client.
  Also, if I use  socket.emit('ValSlider1', sVal); twice at client side it works better, but why it require emit twice  am unable to find, any help will appreciated.
  Thanks.</p>