I use node.js (Express) with server-sent events. I would like to close the event stream by closing the sse HTTP connection. Given the folllwing function:
router.get('/sse', function (req, res) {
});
how can this be achieved?
I use node.js (Express) with server-sent events. I would like to close the event stream by closing the sse HTTP connection. Given the folllwing function:
router.get('/sse', function (req, res) {
});
how can this be achieved?
response.end();
Request the client to initiate the TCP socket connection termination sequence.
response.set("Connection", "close"); // Note: this is an HTTP header.
“Hey, I’ll be hanging up now, ’kay?”
response.connection.end();
socket.end([data[, encoding]][, callback])
Half-closes the socket. i.e., it sends a
FIN
packet. It is possible the server will still send some data.See
writable.end()
for further details.
FIN
packet, even.response.connection.destroy();
socket.destroy([error])
Ensures that no more I/O activity happens on this socket. Destroys the stream and closes the connection.
See
writable.destroy()
for further details.
use this
res.end();
and it will work to close connection
Selected answer is incorrect, res.end() ends the response and writes to the socket. But it doesn't close the TCP connection.
You need to get the underlying connection from the res object and call end on it to close it.
res.connection.end();