Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions lib/sseclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,25 @@ var util = require('util'),

module.exports = SSEClient;

function SSEClient(req, res) {
/**
* @param {http.ClientRequest} req
* @param {http.ServerResponse} res
* @param {Object} options
* @param {Object} options.headers: Extra headers to add to the SSE response.
* The defaults are: {
* 'Content-Type': 'text/event-stream',
* 'Cache-Control': 'no-cache',
* 'Connection': 'keep-alive'
* }
* Anything passed in options.headers will be merged over the defaults.
*
* @constructor
*/
function SSEClient(req, res, options) {
this.req = req;
this.res = res;
this.options = options || {};

var self = this;
res.on('close', function() {
self.emit('close');
Expand All @@ -14,13 +30,20 @@ function SSEClient(req, res) {

util.inherits(SSEClient, events.EventEmitter);


SSEClient.prototype.initialize = function() {
this.req.socket.setNoDelay(true);
this.res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});

// Merge extra headers with default ones.
var headers = Object.assign({
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
},
this.options.headers
);

this.res.writeHead(200, headers);
this.res.write(':ok\n\n');
};

Expand Down