How to close the "Server-Sent Events" connection on the server?

Regarding this specification: http://www.w3.org/TR/eventsource/

How to close a closed connection on the server? On the client side, this is easy, just call close() , but what should I do on the server? Just kill him?

+1
source share
4 answers

node.js

 http.createServer(function (req, res) { //... // client closes connection res.socket.on('close', function () { res.end(); //... }); }); 

see an example implementation of an SSE server in node.js:

https://github.com/Yaffle/EventSource/blob/master/nodechat/server.js

+2
source

you can change the type of content on which servers other than "text / event-stream" are sent. This will close the source of customer events.

+2
source

I think you just close the connection (kill it). I have not seen talk of a graceful shutdown.

+1
source

Attention : if you use the package to manage events sent by the server in node.js, then by directly calling response.end() package may send additional data after response.end() , which will lead to the server crashing with the error β€œRECORD after closing "

Instead of directly calling response.end() , my workaround was to call response.emit('close') , which allowed the package to handle closure.

Node.js documentation at http.ServerResponse> Event.close :

https://nodejs.org/api/http.html#http_event_close_1

0
source

Source: https://habr.com/ru/post/921057/


All Articles