├── .gitignore ├── package.json ├── README.md ├── index.js └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | #* 2 | *$ 3 | *.BAK 4 | *.Z 5 | *.bak 6 | *.class 7 | *.elc 8 | *.ln 9 | *.log 10 | *.o 11 | *.obj 12 | *.olb 13 | *.old 14 | *.orig 15 | *.project 16 | *.pyc 17 | *.pyo 18 | *.rej 19 | */.git/* 20 | *~ 21 | ,* 22 | .#* 23 | .DS_Store 24 | .del-* 25 | .make.state 26 | .nse_depinfo 27 | .project 28 | .svn 29 | CVS.adm 30 | RCS 31 | RCSLOG 32 | SCCS 33 | _$* 34 | _svn 35 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "server-sent-events", 3 | "description": "Express middleware to push events/messages from the server to the browser, using EventSource.", 4 | "version": "1.0.1", 5 | "author": { 6 | "name": "Zac Barton" 7 | }, 8 | "license": "MIT", 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/zacbarton/node-server-sent-events" 12 | }, 13 | "keywords": [ 14 | "express", 15 | "middleware", 16 | "eventsource", 17 | "sse", 18 | "server-sent", 19 | "events" 20 | ] 21 | } 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | server-sent-events 2 | ======== 3 | 4 | Express middleware to push events/messages from the server to the browser, using EventSource. 5 | 6 | This middleware will keep the request stream open indefinitely, pad the initial response stream for IE and flush the response if the compression middleware is in use. 7 | 8 | Detailed information on how to setup the browser/client can be found [here][1]. 9 | 10 | Installation 11 | -------- 12 | 13 | $ npm install -g server-sent-events 14 | 15 | 16 | Usage (with express) 17 | -------- 18 | 19 | ```javascript 20 | // require sse 21 | var sse = require('server-sent-events'); 22 | var express = require('express'); 23 | 24 | var app = express(); 25 | 26 | app.get('/events', sse, function(req, res) { 27 | // res.sse is made available via the middleware 28 | res.sse('data: im from the server\n\n'); 29 | }); 30 | ``` 31 | 32 | [1]: https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | function sse(req, res, next) { 2 | req.socket.setKeepAlive(true); 3 | req.socket.setTimeout(0); 4 | 5 | res.setHeader('Content-Type', 'text/event-stream'); 6 | res.setHeader('Cache-Control', 'no-cache'); 7 | res.setHeader('Connection', 'keep-alive'); 8 | res.status(200); 9 | 10 | // export a function to send server-side-events 11 | res.sse = function sse(string) { 12 | res.write(string); 13 | 14 | // support running within the compression middleware 15 | if (res.flush && string.match(/\n\n$/)) { 16 | res.flush(); 17 | } 18 | }; 19 | 20 | // write 2kB of padding (for IE) and a reconnection timeout 21 | // then use res.sse to send to the client 22 | res.write(':' + Array(2049).join(' ') + '\n'); 23 | res.sse('retry: 2000\n\n'); 24 | 25 | // keep the connection open by sending a comment 26 | var keepAlive = setInterval(function() { 27 | res.sse(':keep-alive\n\n'); 28 | }, 20000); 29 | 30 | // cleanup on close 31 | res.on('close', function close() { 32 | clearInterval(keepAlive); 33 | }); 34 | 35 | next(); 36 | } 37 | 38 | module.exports = sse; 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Zac Barton 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. --------------------------------------------------------------------------------