title: Socket.io multiple response instead of one
body: <p>I'm facing a problem with socket.io and node js.</p>
  
  <p><strong>Context</strong></p>
  
  <p>I have two servers, one of them is processing heavy jobs and the other is responding to the clients.
  The main case is the following one : </p>
  
  <ol>
  <li>The client request data</li>
  <li>The "mid server" looks if I have this data in the database. If I haven't, itsend a request to the second server</li>
  <li>The second server performs the research.</li>
  <li>Once its done, the second server push de data to the "mid server"</li>
  <li>The mid server finally push the data to the client (and persists it for future client requests)</li>
  </ol>
  
  <p>Here's the sample code</p>
  
  <p><strong>Client</strong></p>
  
  <pre><code>&lt;script type="text/javascript"/&gt;
      var socket = io('https://localhost:9091', {'forceNew': true);
      // send a request to the mid server
      socket.emit('request', data);
  
      socket.on('response', async (response) =&gt; {
          // when the mid server responds, the response is printed
          $('#container').append(response);
      });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>Mid server</strong></p>
  
  <pre><code>const app = require('express')();
  const http = require('http').createServer(app);
  const io = require('socket.io')(http);
  
  // in order to use this server as a Socket.io client
  const secondServer = require('socket.io-client').connect('http://localhost:9092');
  
  app.set('view engine', 'ejs');
  
  app.get('/', (req, res) =&gt; {
      res.render('index', {})
  });
  
  io.on('connection', async (socket) =&gt; {
  
      console.log('User connected');
  
      // On client request
      socket.on('request', async (data) =&gt; {
  
          console.log('Requesting from ' + socket.id);
  
          // The same request is transmited to the second server
          secondServer.emit('request', data);
      });
  
      // Once the second server has finished his job
      secondServer.on('response', async (data) =&gt; {
  
          console.log('Responding for ' + socket.id);
  
          // I send the data back to the client
          socket.emit('response', data);
      });
  
      socket.on('disconnect', () =&gt; {
          socket.disconnect();
      });
  });
  
  // port is 9091
  http.listen(port, () =&gt; {
      console.log('Server listening on port ' + port);
  });
  </code></pre>
  
  <p><strong>Second server</strong></p>
  
  <pre><code>const io = require("socket.io").listen(9092);
  
  io.on("connection", function (socket) {
  
      socket.on('request', async () =&gt; {
          // await in order to wait the heavyJob to be done before sending the response
          var data = await heavyJob()
  
          // Send back the data to the mid server
          socket.emit('response', data);
  
      });
  });
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>The problem I'm facing is, if I refresh the client page, the mid server will send twice the data, once to the old socket and the once for the new one as so :</p>
  
  <p><a href="https://i.stack.imgur.com/DeQBX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DeQBX.jpg" alt="console output"></a></p>
  
  <p>I have also tried to respond to the client with <code>socket.emit('response', data)</code> on mid server side and <code>socket.on('response', (data) =&gt; {})</code> on client side instead of using <code>callback</code> function. It doesn't change anything.</p>
  
  <p>Do I misunderstanding something ?</p>
  
  <p>Thanks for your help</p>
  
  <p><strong>Edit</strong></p>
  
  <p>It doesn't only happen when the client is refreshing his page. It happens when two different clients send a request at the same time. The server is responding four times, two times for each client.</p>
  
  <p><a href="https://i.stack.imgur.com/x8RSo.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/x8RSo.jpg" alt="console output"></a></p>