Skip to content

Commit

Permalink
Merge 042ce32 into dc7c42e
Browse files Browse the repository at this point in the history
  • Loading branch information
Spissable committed Dec 5, 2018
2 parents dc7c42e + 042ce32 commit 51c89c5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lib/sse-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

var url = require('url');
var util = require('util');

var pull = require('lodash/pull');
var merge = require('lodash/merge');
var reject = require('lodash/reject');
var events = require('events');
var access = require('access-control');

// See initializeConnection() for an explanation
var preambleData = new Array(2057).join('-') + '\n';

Expand All @@ -20,20 +20,21 @@ var preambleData = new Array(2057).join('-') + '\n';
* 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.
* @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.
* @param {Message} options.keepAliveMessage The message used for the periodic keep-alive "ping".
*/
var SseChannel = function(options) {
var opts = options || {};
Expand All @@ -55,6 +56,7 @@ var SseChannel = function(options) {
this.historySize = opts.historySize || 500;
this.retryTimeout = opts.retryTimeout || null;
this.pingInterval = (opts.pingInterval | 0) || 20000;
this.keepAliveMessage = opts.keepAliveMessage ? parseMessage(opts.keepAliveMessage, true) : ':';

// Populate history with the entries specified
this.history = ((opts.history || [])
Expand Down Expand Up @@ -169,7 +171,7 @@ SseChannel.prototype.getConnectionCount = function() {
*
*/
SseChannel.prototype.ping = function() {
broadcast(this.connections, ':\n');
broadcast(this.connections, this.keepAliveMessage + '\n');
};

/**
Expand Down Expand Up @@ -347,7 +349,7 @@ function parseMessage(msg, jsonEncode) {
}

var data = msg.data || '';
if (jsonEncode) {
if (jsonEncode && msg.data !== null && typeof msg.data !== 'undefined') {
data = JSON.stringify(data);
}

Expand Down
34 changes: 34 additions & 0 deletions test/sse-channel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,40 @@ describe('sse-channel', function() {
req.end();
});

it('sends custom automatic ping', function(done) {
var interval = 25;
var message = {
data: null,
event: 'KEEP-ALIVE',
id: 1
};
initServer({ pingInterval: interval, keepAliveMessage: message });

var opts = url.parse(host + path);
opts.headers = { Accept: 'text/event-stream' };

var initialMessage = ':ok\n\n';
var parsedKeepAliveMessage = 'event: KEEP-ALIVE\nid: 1\ndata: \n\n\n';

var req = http.request(opts, function(res) {
var buf = '';
res.on('data', function(chunk) {
buf += chunk.toString();
});

setTimeout(function() {
req.abort();
assert.equal(
buf, initialMessage + parsedKeepAliveMessage
);
done();
}, interval);
});

req.setNoDelay(true);
req.end();
});

it('sends "preamble" if client requests it', function(done) {
initServer();

Expand Down

0 comments on commit 51c89c5

Please sign in to comment.