Search code examples
server-sent-events

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


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

How does one close the opened connection on the server? Client-side wise it is easy, just call close(), but what should I do on the server? Just kill it?


Solution

  • node.js:

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

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

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