lib/transport-http-server.js
function createStreamTransport(req, res) {
    // A transport object.
    var self = createBaseTransport(req, res);
    // Any error on request-response should propagate to transport.
    req.on("error", function(error) {
        self.emit("error", error);
    });
    res.on("error", function(error) {
        self.emit("error", error);
    });
    // When the underlying connection was terminated abnormally.
    res.on("close", function() {
        self.emit("close");
    });
    // When the complete response has been sent.
    res.on("finish", function() {
        self.emit("close");
    });

    // The response body should be formatted in the [event stream
    // format](http://www.w3.org/TR/eventsource/#parsing-an-event-stream).
    self.write = function(data) {
        // `data` should be either a `Buffer` or a string.
        if (typeof data === "string") {
            // A text message should be prefixed with `1`.
            data = "1" + data;
        } else {
            // A binary message should be encoded in Base64 and prefixed with
            // `2`.
            data = "2" + data.toString("base64");
        }
        // According to the format, data should be broken up by `\r`, `\n`, or
        // `\r\n`.
        var payload = data.split(/\r\n|[\r\n]/).map(function(line) {
            // Each line should be prefixed with 'data: ' and postfixed with
            // `\n`.
            return "data: " + line + "\n";
        })
        // Prints `\n` as the last character of a message.
        .join("") + "\n";
        // Writes it to response with `utf-8` encoding.
        res.write(payload, "utf-8");
    };
    // Ends the response. Accordingly, `res`'s `finish` event will be fired.
    self.close = function() {
        res.end();
        return this;
    };

    // The content-type headers should be `text/event-stream` for Server-Sent
    // Events and `text/plain` for others. Also the response should be encoded in `utf-8`.
    res.setHeader("content-type", "text/" + 
        // If it's Server-Sent Events, `sse` param is `true`.
        (req.params.sse === "true" ? "event-stream" : "plain") + "; charset=utf-8");
    // The padding is required, which makes the client-side transport on old
    // browsers be aware of change of the response. It should be greater
    // than 1KB, be composed of white space character and end with `\n`.
    var text2KB = Array(2048).join(" ");
    // Some host objects which are used to perform streaming transport can't or
    // don't allow to read response headers as well as write request headers.
    // That's why we uses the first message as a handshake output. The handshake
    // result should be formatted in URI. And transport id should be added as
    // `id` param.
    var uri = url.format({query: {id: self.id}});
    // Likewise some host objects in old browsers, junk data is needed to make
    // themselves be aware of change of response. Prints the padding and the
    // first message following `text/event-stream` with `utf-8` encoding.
    res.write(text2KB + "\ndata: " + uri + "\n\n", "utf-8");
    return self;    
}
function createStreamTransport(req, res) {
    // A transport object.
    var self = createBaseTransport(req, res);
    // Any error on request-response should propagate to transport.
    req.on("error", function(error) {
        self.emit("error", error);
    });
    res.on("error", function(error) {
        self.emit("error", error);
    });
    // When the underlying connection was terminated abnormally.
    res.on("close", function() {
        self.emit("close");
    });
    // When the complete response has been sent.
    res.on("finish", function() {
        self.emit("close");
    });

    // The response body should be formatted in the [event stream
    // format](http://www.w3.org/TR/eventsource/#parsing-an-event-stream).
    self.write = function(data) {
        // `data` should be either a `Buffer` or a string.
        if (typeof data === "string") {
            // A text message should be prefixed with `1`.
            data = "1" + data;
        } else {
            // A binary message should be encoded in Base64 and prefixed with
            // `2`.
            data = "2" + data.toString("base64");
        }
        // According to the format, data should be broken up by `\r`, `\n`, or
        // `\r\n`.
        var payload = data.split(/\r\n|[\r\n]/).map(function(line) {
            // Each line should be prefixed with 'data: ' and postfixed with
            // `\n`.
            return "data: " + line + "\n";
        })
        // Prints `\n` as the last character of a message.
        .join("") + "\n";
        // Writes it to response with `utf-8` encoding.
        res.write(payload, "utf-8");
    };
    // Ends the response. Accordingly, `res`'s `finish` event will be fired.
    self.close = function() {
        res.end();
        return this;
    };

    // The content-type headers should be `text/event-stream` for Server-Sent
    // Events and `text/plain` for others. Also the response should be encoded in `utf-8`.
    res.setHeader("content-type", "text/" + 
        // If it's Server-Sent Events, `sse` param is `true`.
        (req.params.sse === "true" ? "event-stream" : "plain") + "; charset=utf-8");
    // The padding is required, which makes the client-side transport on old
    // browsers be aware of change of the response. It should be greater
    // than 1KB, be composed of white space character and end with `\n`.
    var text2KB = Array(2048).join(" ");
    // Some host objects which are used to perform streaming transport can't or
    // don't allow to read response headers as well as write request headers.
    // That's why we uses the first message as a handshake output. The handshake
    // result should be formatted in URI. And transport id should be added as
    // `id` param.
    var uri = url.format({query: {id: self.id}});
    // Likewise some host objects in old browsers, junk data is needed to make
    // themselves be aware of change of response. Prints the padding and the
    // first message following `text/event-stream` with `utf-8` encoding.
    res.write(text2KB + "\ndata: " + uri + "\n\n", "utf-8");
    return self;    
}
function createStreamTransport ( req , res ) { // A transport object. var self = createBaseTransport ( req , res ) ; // Any error on request-response should propagate to transport. req . on ( "error" , function ( error ) { self . emit ( "error" , error ) ; } ) ; res . on ( "error" , function ( error ) { self . emit ( "error" , error ) ; } ) ; // When the underlying connection was terminated abnormally. res . on ( "close" , function ( ) { self . emit ( "close" ) ; } ) ; // When the complete response has been sent. res . on ( "finish" , function ( ) { self . emit ( "close" ) ; } ) ; // The response body should be formatted in the [event stream // format](http://www.w3.org/TR/eventsource/#parsing-an-event-stream). self . write = function ( data ) { // `data` should be either a `Buffer` or a string. if ( typeof data === "string" ) { // A text message should be prefixed with `1`. data = "1" + data ; } else { // A binary message should be encoded in Base64 and prefixed with // `2`. data = "2" + data . toString ( "base64" ) ; } // According to the format, data should be broken up by `\r`, `\n`, or // `\r\n`. var payload = data . split ( / \r\n|[\r\n] / ) . map ( function ( line ) { // Each line should be prefixed with 'data: ' and postfixed with // `\n`. return "data: " + line + "\n" ; } ) // Prints `\n` as the last character of a message. . join ( "" ) + "\n" ; // Writes it to response with `utf-8` encoding. res . write ( payload , "utf-8" ) ; } ; // Ends the response. Accordingly, `res`'s `finish` event will be fired. self . close = function ( ) { res . end ( ) ; return this ; } ; // The content-type headers should be `text/event-stream` for Server-Sent // Events and `text/plain` for others. Also the response should be encoded in `utf-8`. res . setHeader ( "content-type" , "text/" + // If it's Server-Sent Events, `sse` param is `true`. ( req . params . sse === "true" ? "event-stream" : "plain" ) + "; charset=utf-8" ) ; // The padding is required, which makes the client-side transport on old // browsers be aware of change of the response. It should be greater // than 1KB, be composed of white space character and end with `\n`. var text2KB = Array ( 2048 ) . join ( " " ) ; // Some host objects which are used to perform streaming transport can't or // don't allow to read response headers as well as write request headers. // That's why we uses the first message as a handshake output. The handshake // result should be formatted in URI. And transport id should be added as // `id` param. var uri = url . format ( { query : { id : self . id } } ) ; // Likewise some host objects in old browsers, junk data is needed to make // themselves be aware of change of response. Prints the padding and the // first message following `text/event-stream` with `utf-8` encoding. res . write ( text2KB + "\ndata: " + uri + "\n\n" , "utf-8" ) ; return self ; }
The client performs a HTTP persistent connection and watches changes in response and the server prints chunk as data to response.
The client performs a HTTP persistent connection and watches changes in response and the server prints chunk as data to response .