I am getting started with Server Sent Events (SSE) since my web app requires receiving real time updates from the server. It does not require sending anything to the server, therefore SSE was chosen over Websockets.
After reading through some examples, I have the following code:
On my server, in ./src/routers/mainRouter.js I have:
router.get('/updates', (req, res) => { res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive' }) // Listens for 'event' and sends an 'Event triggered!' message to client when its heard. eventEmitter.addListener('event', () => { console.log('Event triggered! Sending response.') res.write('data: Event triggered!\n\n') }) req.on('close', () => { console.log('Connection to client closed.') res.end() }) }) module.exports = router
On my client, in ./app/index.js I have:
const source = new EventSource('/updates') source.onmessage = (e) => { console.log(e) }
There are 2 issues I am having:
Once I open a connection from the client side and then close the connection (by closing the tab), the
'close'
event fires twice resulting in the code block withinreq.on('close')
running twice. I am not sure why this happens. Myconsole
on the server side looks like follows:Event triggered! Sending response. Connection to client closed. Connection to client closed.
More importantly, although
req.end()
is called, the router still keeps listening for events on that channel and tries to send responses down that channel resulting in aERR_STREAM_WRITE_AFTER_END
error and the server crashing. So the final console output looks like:Event triggered! Sending response. // First event triggers. Connection to client closed. // 'close' event fires. Connection to client closed. // 'close' event fires a second time (not sure why). Event triggered! Sending response. // Router continues listening for 'event' and sends another response although res.end() was called earlier events.js:187 throw er; // Unhandled 'error' event ^ Error [ERR_STREAM_WRITE_AFTER_END]: write after end