socket.io with webpack module federation

Viewed 22

I'm using socket.io to communicate between a server and many clients. Locally everything works fine. However, on production it doesn't work as desired. We use microservices, and this specific microservice is part of a suite of services that run together using webpack module federation.

When accessing the app directly through the k8s ingress, I can see that the clients are connecting to the server. However, clients running through module federation (in the bigger app that consists of my microapp and others), do not connect to the server, and every few seconds a 404 error is printed to the console for a GET request to:

http://<BASE_URL>/socket.io/?EIO=4&transport=polling&t=XXXXXX

where XXXXXX is some random string. I believe that I need to redirect the clients' sockets somehow to reach the server, but I don't know how to do so.

Relevant Client Code

    const socket = io.connect("");
    socket.on("someEvent", (param) => {
         doSomething()
    });

Relevant Server Code


let server = app.listen(port, (err) => {
    if (err) throw err;
    // Express server Ready on http://localhost:port
    });
 ...

let io;
const initSocket = (server) => {
    io = require("socket.io")(server, {
        cors: { origin: "*" }
    });
    io.on('connection', (socket) => {
        logging.mainLogger.info(`successfully connected to socket 
${JSON.stringify(socket.id)}`);
    });
    io.listen(server);
}

const emitSomeEvent = (param) => {
    io.emit("someEvent", param);
}

0 Answers
Related