Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/yosuke-furukawa/sse_stream
- Owner: yosuke-furukawa
- Created: 2013-05-25T05:55:12.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T20:24:35.000Z (11 months ago)
- Last Synced: 2024-10-03T17:08:59.129Z (about 1 month ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 3
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
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
```