socket.io (expressJS) works, but I am unable to send individual responses

18 Views Asked by At

TLDR:

ExpressJS backend server -> reactJS server frontend

Backend ws port 2000 Backend Express port 5000 Frontend port 3000

Essentially my code looks something like this:

app.post('/api/auto/submit', async (req, res) => {

// async function call
variable = await getData(arguments) 

}


async function getData(arguments){

// puppeteer logic

// wait for variable foo to be defined

// once foo is defined -> send to the frontend

  socket.on('waitingMFA', async (data) => {
    // Create a unique room name for this user
    const roomName = `${socket.id}`;

    // Wait for Numbers to be defined
    const Numbers = await waitForNumbers();

    console.log('This is the data that I got: ', mfaNumbers);
    console.log('RoomName: ', roomName);

    // Emit the Numbers data only to the specific user who requested it
    socket.join(roomName); // Join a room specific to the user
    io.to(roomName).emit('Numbers', Numbers, roomName);
  });
}

So the gist is that I need some way to store a socket.id for a user who presses the button (maybe even take in the cookie and parse it to get their username?), and then do the logic -> and send back the two numbers, but only for the person that requested it, and not everyone else.

Currently my code works, however the issue is that it sends it to everyone that's signed in. The numbers are supposed to be unique for every user and they are different every time the function runs.

I am open to swapping libraries from socket.io to ex ws if any of you know that library better?

Thank you for the help in advance <3

I have tried to define the io logic outside of anything (top of the document), and then get the numbers as a resolve, but yeah that didn't work.

I tried to swap to ws, but find the documentation to be a bit confusing

Tried to fetch the cookie, but didn't manage to do that either...

0

There are 0 best solutions below