title: Node.JS JavaScript socket.io emit message in microservices architecture
body: <p>I'm trying to create a notification feature in Node.JS using Socket.io, and Javascript.</p>
  <p>The app.js (the entry script) imports the server.js script (which contains the API endpoints).</p>
  <p>Socket.io then listens to app.js (<code>const io = socket(server)</code>) and notifies index.html if any notification messages are emitted by the server.</p>
  <p>The code works fine if I combine app.js and server.js into one script.</p>
  <p>The problem is, when I separate the scripts into a microservices architecture like below, the messages aren't showing.</p>
  <p>I can't figure out how to emit a socket.io message from server.js when I split the endpoint code into another script like below.</p>
  <p>I've tried importing <code>socket.io-client</code> into server.js, then using <code>const socket=io()</code>, and emitting a message using <code>socket.emit('notification','Task completed..!')</code>. But this doesn't work.</p>
  <p>Could you please help me spot the problem?</p>
  <p>Thanks in advance!</p>
  <p>app.js:</p>
  <pre><code>
  const app = require('./server'); 
  const socket = require(&quot;socket.io&quot;);
  
  const port = 6000; 
  
  const server = app.listen(port, () =&gt; {
      console.log(`App started on port ${port} ...!`)
  })
  
  const io = socket(server);
  
  module.exports={io};
  
  </code></pre>
  <p>server.js:</p>
  <pre><code>
  const express = require('express');
  const io  = require('socket.io-client');
  const socket=io();
  
  // Express configurations
  const app = express()
  
  app.get('/start', (req, res) =&gt; {
  
      // Task completed notification
      io.on(&quot;connection&quot;, (socket)=&gt;{
           socket.emit('notification',`Task completed..!`);
       });
  
  });
  
  // Export app
  module.exports = app;
  
  </code></pre>
  <p>index.html:</p>
  <pre class="lang-html prettyprint-override"><code>
  &lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;title&gt;Notify&lt;/title&gt;
  &lt;/head&gt;
    &lt;script src=&quot;/socket.io/socket.io.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;/assets/js/jquery.js&quot;&gt;&lt;/script&gt;
    &lt;script&gt;
    var socket = io();
  
    socket.on('notification', (msg) =&gt; {
  
      // Create notification element
      var nb = ` ${msg} `;
              
      $('#messages').html(nb);
    
    });
    &lt;/script&gt;
    &lt;body&gt;
      &lt;span id=&quot;messages&quot;&gt;&lt;/span&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  
  </code></pre>