title: Socket IO makes multiple connections when the page is refreshed - Node JS
body: <p>I have developed a scrapping tool, that scraps jobs from all websites and save them into the database. I have made my own default log where I get messages(errors, info) etc. I am using socket.io to update my view in real time and for database too. </p>
  
  <p>The problem is when I start the app it perfectly get make socket, and database connections. But when I try to refresh the page, the same connection is made again twice with the same message and different ID's. As much I refresh the page the connections are made, and the id get changed, but for all made connection they use one ID,</p>
  
  <p>Below is the Log which shows it : </p>
  
  <p><a href="https://i.stack.imgur.com/w0DJh.png" rel="noreferrer"><img src="https://i.stack.imgur.com/w0DJh.png" alt="enter image description here"></a></p>
  
  <p>I have uploaded <a href="https://www.youtube.com/watch?v=LNuMq_E3Gc4&amp;feature=youtu.be" rel="noreferrer">this</a> video, please check this as well. Try to watch the very beginning, and then at <code>01:41</code> and <code>03:06</code>, before starting scrapping of the first site the connection is established, but when second website scrapping is started, the <code>Internet Connection</code> message is given twice, and the same stands for when third website scrapping is started, the number of messages get doubled every time. I don't know why.</p>
  
  <p>I have tried following the answer of <a href="https://stackoverflow.com/questions/41924713/node-js-socket-io-page-refresh-multiple-connections">this</a> question, but still no success. The code is 600+ lines on <code>server</code> file, and  150+ lines second file and same on the client side, that's why I can't upload all and it's a bit confidential.</p>
  
  <p>But the socket connection on the client and server is like this:</p>
  
  <p><strong>Server Side</strong> </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const express              =    require("express");
  const app                  =    express();
  const scrap                =    require("./algorithm");
  const event = scrap.defEvent;//imported from another file 
  const ms_connect = scrap.ms_connect;
  const server = app.listen(8000, function(){ console.log('Listening on 8000'); });
  const io                   =    require("socket.io").listen(server);
  const internetAvailable    =    require("internet-available");
  app.use(express.static(__dirname + "/"));
  
  
  app.get("/scrap",function(req,res){
      res.sendFile(__dirname+"/index.html");//Set the Default Route
      io.on("connection",function(socket){ //On Socket Connection
          socketSameMess("Socket",'Sockets Connection Made on ID : &lt;span style="color:#03a9f4;"&gt;'+socket.id+'&lt;span&gt;');
          ms_connect.connect(function(err){//On Connection with Database
              if(err) socketSameMess("database_error",err+" "); // If any error in database connection
              socketSameMess("Database ",'Connected to MYSQL Database Successfully...');
          })
      })
  })
  
  function eventSameMess(auth,mess){
      //hits the custom console
      defEvent.emit("hitConsole",{a:auth,m:mess});
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Client Side</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var socket = io.connect('http://localhost:8000');
  socket.on('connect',function(){
      if(socket.connected){
          initDocument();
      }
  })  </code></pre>
  </div>
  </div>
  </p>