Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
Node.JS JavaScript socket.io emit message in microservices architecture

I'm trying to create a notification feature in Node.JS using Socket.io, and Javascript.

The app.js (the entry script) imports the server.js script (which contains the API endpoints).

Socket.io then listens to app.js (const io = socket(server)) and notifies index.html if any notification messages are emitted by the server.

The code works fine if I combine app.js and server.js into one script.

The problem is, when I separate the scripts into a microservices architecture like below, the messages aren't showing.

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.

I've tried importing socket.io-client into server.js, then using const socket=io(), and emitting a message using socket.emit('notification','Task completed..!'). But this doesn't work.

Could you please help me spot the problem?

Thanks in advance!

app.js:


const app = require('./server'); 
const socket = require("socket.io");

const port = 6000; 

const server = app.listen(port, () => {
    console.log(`App started on port ${port} ...!`)
})

const io = socket(server);

module.exports={io};

server.js:


const express = require('express');
const io  = require('socket.io-client');
const socket=io();

// Express configurations
const app = express()

app.get('/start', (req, res) => {

    // Task completed notification
    io.on("connection", (socket)=>{
         socket.emit('notification',`Task completed..!`);
     });

});

// Export app
module.exports = app;

index.html:


<html lang="en">
<head>
  <title>Notify</title>
</head>
  <script src="/socket.io/socket.io.js"></script>
  <script src="/assets/js/jquery.js"></script>
  <script>
  var socket = io();

  socket.on('notification', (msg) => {

    // Create notification element
    var nb = ` ${msg} `;
            
    $('#messages').html(nb);
  
  });
  </script>
  <body>
    <span id="messages"></span>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Fixed it!

app.js:


const app = require('./server'); 
const socket = require("socket.io");

const port = 6000; 

const server = app.listen(port, () => {
    console.log(`App started on port ${port} ...!`)
})

const io = socket(server);

exports.io=io; // added

server.js:


const express = require('express');
const io  = require('socket.io-client');
const socket=io();

// Express configurations
const app = express()

app.get('/start', (req, res) => {

    // Task completed notification
    require('./app').io.emit('processing',`Task Completed..!`); // updated

});

// Export app
module.exports = app;

index.html:


<html lang="en">
<head>
  <title>Notify</title>
</head>
  <script src="/socket.io/socket.io.js"></script>
  <script src="/assets/js/jquery.js"></script>
  <script>
  var socket = io();

  socket.on('notification', (msg) => {

    // Create notification element
    var nb = ` ${msg} `;
            
    $('#messages').html(nb);
  
  });
  </script>
  <body>
    <span id="messages"></span>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda