title: socket.io - hundreds of requests instead of one connection
body: <p>I created simple app to start with socket.io, but when I run it, Chrome(tested in other browsers, result same) eats all of my CPU and makes many requests:</p>
  
  <p><a href="https://i.stack.imgur.com/3RinX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3RinX.png" alt="enter image description here"></a></p>
  
  <p>I'm new to sockets, but I'm sure this is not how it should work. The code running in browser is really simple, it should just connect to socket and log all received data to console:</p>
  
  <p>index.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;script src="https://cdn.socket.io/socket.io-1.2.0.js" charset="UTF-8"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;script type="application/javascript"&gt;
      var Sockets = io.connect('http://localhost:4000');
      Sockets.on('Test', function (data) {
          console.log(data);
      });
  &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Also, my server file looks like this:</p>
  
  <p>server.js</p>
  
  <pre><code>var app = require('express')();
  var http = require('http').Server(app);
  var bodyParser = require("body-parser");
  var io = require('socket.io')(http);
  var port = 4000;
  
  http.listen(port, function () {
      console.log('Server running at port ' + port);
  });
  
  var urlencodedParser = bodyParser.urlencoded({extended: false});
  app.post('/', urlencodedParser, function (req, res) {
      if (!req.body) return res.sendStatus(400);
      var post = req.body;
      io.emit("Test", post.data);
      console.log(post.data);
      res.send('true');
  });
  
  io.on('connection', function(socket){
      console.log('a user connected');
      socket.on('disconnect', function(){
          console.log('user disconnected');
      });
  });
  </code></pre>
  
  <p>When I run the server <code>node server.js</code>, I got the <code>Server running at port 4000</code> message and everything seems fine. But when I open the index.html in my browser, the node console is spammed by <code>a user connected</code> messages. Instead of connecting one client, the browser makes dozens of requests every second. When I close the browser, there is no output for some time, and then the node console is spammed by <code>user disconnected</code> messages.</p>
  
  <p>This server should redirect all data sent via POST to connected sockets. When I make this POST request, the node server receives it (I know because it print's it into node console). But it's not received by the socket client, as there is no output in browser console (but the browser still makes dozens of new connections every second.</p>
  
  <p>What is wrong here? First I thought I just messed up, so I went back and copy-pasted code from <a href="https://www.itnetwork.cz/php/websocket" rel="nofollow noreferrer">tutorial I found</a> (not in English, but in Czech), but nothing changed. The tutorial has a lot of positive feedback, so there is propably something wrong with my computer. But what?</p>