Socket.io emit on express route to specific user=

I have an express server set up as follows:

Server Side

const express = require('express')
const app = express()
const port = 3001
const http = require('http')
const server = http.createServer(app)
const { Server } = require("socket.io");
const io = new Server(server, {
    cors: {
        origin: "http://localhost:3000"
    }
});
app.io = io

//emitting works fine, but if more than one user sends a request to this endpoint, everyone gets all messages
app.get('/test', (req, res) => {
   req.app.io.emit('started', true)
   setInterval(() => {
        var clientConnected = true //some way to test if the client is connected. Anything including req.app.io or res.app.io doesn't work
        if (clientConnected) {
            req.app.io.emit('data', {key: value})
        } else {
            //shut down data transfer
            return;
        }
    }, 1000);
    //more stuff here
    res.end()
})
server.listen(port, () => {
  console.log(`Running on http://localhost:${port}`)
})

React Client Side

import * as React from 'react';
React.useEffect(() => {
   socket.on('started', (data) => {
       console.log(data)
       //do something here
   })
   socket.on('data', (data) => {
      console.log(data)
      //do something here
   })
}, [])

The post I got the base code from is almost 7 years old and doesn’t have much information on how to do these things, along with the socket.io docs that doesn’t really have any good information on this either.

There are two problems here. The first one is that all users are receiving the same messages which is a problem for my ui which adds the data to a state and it renders a part of the page with the new data. So I need every user who gets connected to get only their data that they requested.

The second problem is that using this method, I have no way of telling if the user is still connected to the route. If they are still connected I want to continue sending data, and if they aren’t I want to kill the process the server is doing to retrieve and send that data. Even emitting to the server inside of one of the ‘socket.on’ never gets received by the server. How do I go about checking if the client is still connected so I don’t waste bandwidth or waste storage space? I can’t use io.on(‘connect’) inside the route because I use it elsewhere to check if a user is online and count how many users are online. I just want to know if the user is still connected to the /test route specifically. Again, emitting works fine. I also want the messages to only be sent to that specific user connected to the /test route WHILE they are connected to the /test route. If the user refreshes their page or cancels I want to stop the data transfer on the server side which is doing things on its own.