lib/sse-channel.js
function(options) {
    var opts = options || {};
    if (opts.cors !== false) {
        this.cors = access(merge({
            origins: [],
            methods: ['GET', 'HEAD', 'OPTIONS'],
            headers: ['Last-Event-ID']
        }, opts.cors || {}));
    }

    var jsonEncode = (
        typeof opts.jsonEncode === 'undefined' ?
            false :
            Boolean(opts.jsonEncode)
    );

    this.jsonEncode = jsonEncode;
    this.historySize = opts.historySize || 500;
    this.retryTimeout = opts.retryTimeout || null;
    this.pingInterval = (opts.pingInterval | 0) || 20000;

    // Populate history with the entries specified
    this.history = ((opts.history || [])
        .filter(hasId)
        .slice(0 - this.historySize)
        .reverse()
        .map(function(msg) {
            return {
                id: msg.id,
                msg: parseMessage(msg, jsonEncode)
            };
        })
    );

    this.connections = [];
    this.connectionCount = 0;

    // Start a timer that will ping all connected clients at a given interval
    this.timer = setInterval(this.ping.bind(this), this.pingInterval);
}
function(options) {
    var opts = options || {};
    if (opts.cors !== false) {
        this.cors = access(merge({
            origins: [],
            methods: ['GET', 'HEAD', 'OPTIONS'],
            headers: ['Last-Event-ID']
        }, opts.cors || {}));
    }

    var jsonEncode = (
        typeof opts.jsonEncode === 'undefined' ?
            false :
            Boolean(opts.jsonEncode)
    );

    this.jsonEncode = jsonEncode;
    this.historySize = opts.historySize || 500;
    this.retryTimeout = opts.retryTimeout || null;
    this.pingInterval = (opts.pingInterval | 0) || 20000;

    // Populate history with the entries specified
    this.history = ((opts.history || [])
        .filter(hasId)
        .slice(0 - this.historySize)
        .reverse()
        .map(function(msg) {
            return {
                id: msg.id,
                msg: parseMessage(msg, jsonEncode)
            };
        })
    );

    this.connections = [];
    this.connectionCount = 0;

    // Start a timer that will ping all connected clients at a given interval
    this.timer = setInterval(this.ping.bind(this), this.pingInterval);
}
function ( options ) { var opts = options || { } ; if ( opts . cors !== false ) { this . cors = access ( merge ( { origins : [ ] , methods : [ 'GET' , 'HEAD' , 'OPTIONS' ] , headers : [ 'Last-Event-ID' ] } , opts . cors || { } ) ) ; } var jsonEncode = ( typeof opts . jsonEncode === 'undefined' ? false : Boolean ( opts . jsonEncode ) ) ; this . jsonEncode = jsonEncode ; this . historySize = opts . historySize || 500 ; this . retryTimeout = opts . retryTimeout || null ; this . pingInterval = ( opts . pingInterval | 0 ) || 20000 ; // Populate history with the entries specified this . history = ( ( opts . history || [ ] ) . filter ( hasId ) . slice ( 0 - this . historySize ) . reverse ( ) . map ( function ( msg ) { return { id : msg . id , msg : parseMessage ( msg , jsonEncode ) } ; } ) ) ; this . connections = [ ] ; this . connectionCount = 0 ; // Start a timer that will ping all connected clients at a given interval this . timer = setInterval ( this . ping . bind ( this ) , this . pingInterval ) ; }
Server-Sent Events "Channel"

The "channel"-concept is simply a collection of clients which should receive the same messages.
This is useful if you want to have one server which can easily handle different sources and
clients. History is automatically handled for you, as long as each event is accompanied by
a numeric ID. If a client reconnects with a `Last-Event-ID`-header, he receives all messages
newer than the given ID.

@param {Object}  options               Options for this SSE-channel
@param {Array}   options.history       An array of messages to pre-populate the history with.
Note: Number of items will equal the max history size,
where the last elements in the array will be the present
@param {Number}  options.historySize   The maximum number of messages to keep in history
@param {Number}  options.retryTimeout  Milliseconds clients should wait before reconnecting
@param {Number}  options.pingInterval  How often the server should send a "ping" to clients
@param {Boolean} options.jsonEncode    Whether the client should auto-encode data as JSON before
sending. Defaults to false.
@param {Object}  options.cors          Cross-Origin request options - uses `access-control`-module,
see https://www.npmjs.org/package/access-control for the
available options. Note that the `Last-Event-ID`-header
needs to be allowed. By default we do not allow any origins.
Pass `false` to disable CORS-checking altogheter.
Server - Sent Events Channel