DEV Community

Discussion on: Server-Sent Events and React

Collapse
 
billyc10 profile image
billyc10 • Edited

I noticed that in the Node.js implementation, even if the client closes the connection, the setInterval function will continue running. I made an edit so it handles connection closures:


let eventInterval = setInterval(() => {
    res.write(`data: ${JSON.stringify(getData())}\n\n`)
  }, 2000)

req.on('close', (err) => {
    clearInterval(eventInterval);
    res.end();
    })
Collapse
 
sirwanafifi profile image
Sirwan Afifi

Perfect, thanks, I updated the source code on GitHub