Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/yosuke-furukawa/sse_stream

Server Sent Event Stream for Node.js
https://github.com/yosuke-furukawa/sse_stream

Last synced: about 1 month ago
JSON representation

Server Sent Event Stream for Node.js

Awesome Lists containing this project

README

        

# Server-Sent Events Stream

Server-Sent Events Stream can send Server-Sent Events using Node.js.

## How to install

```bash
$ npm install sse_stream
```

## How to use

Server side
```js
var http = require("http");
var SSEStream = require("sse_stream");
var fs = require("fs");

http.createServer(function(req, res){
if (req.headers["accept"] === "text/event-stream") {
var sse_stream = new SSEStream();
var exec = require('child_process').exec;
var child = exec("tail -f test.log");
res.writeHead(200, {
'Content-type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
child.stdout.pipe(sse_stream).pipe(res);
sse_stream.on("end", function() {
res.end();
});
readStream.on("end", function() {
res.removeAllListeners();
});
sse_stream.on("error", function(error) {
console.error("error " + error);
});
readStream.on("error", function(error) {
console.error("error " + error);
});
} else {
fs.createReadStream("index.html").pipe(res);
}
}).listen(8080);
```

Client side

index.html
```html


Server Sent Events


$(function() {
var source = new EventSource("/");
source.onmessage = function(event) {
$("p").append(event.data);
$('html, body').animate({
scrollTop: $(document).height()
}, 100);
};
});




```

## create stream server
install global
```bash
$ npm install sse_stream -g
```

### server-sent event for file
```bash
$ sse_server --file test.log
```

### server-sent event for command
```bash
$ sse_server --cmd "tail -f test.log"
```

### server-sent event change port
```bash
$ sse_server --cmd "tail -f test.log" --port 3002
```