sse-stream

expose html5 server sent events (sse) as a writable stream

sse-stream

Expose HTML5 Server Sent Events as an installable appliance on Node.JS http servers; connections are emitted as Writable streams.

var http = require('http')
  , fs = require('fs')
  , through = require('through')
  , sse = require('sse-stream')('/sse')
  , serv

module.exports = serv = http.createServer(function(req, resp) {
  resp.setHeader('content-type', 'text/html')
  resp.end('<html><body><script type="text/javascript">('+js+')()</script></body></html>')
})

sse.install(serv)

sse.on('connection', function(client) {
  fs.createReadStream('/usr/share/dict/words')
    .pipe(through(function(buf) { this.emit('data', buf.toString()) }))
    .pipe(client)
})

// client-side code:
function js() {
  var es = new EventSource('/sse')
    , pre = document.createElement('pre')
    , closed = false

  document.body.appendChild(pre)

  es.onmessage = function(ev) {
    if(closed) return

    pre.appendChild(document.createTextNode(ev.data))

    window.scrollTo(0, pre.clientHeight)
  }

  es.addEventListener('end', function() {
    es.close()
    closed = true
  }, true)

  es.onerror = function(e) {
    closed = true
  }
}
API

sse = require('sse-stream')(path | options)

Create a SSE server that emits connection events on new, successful eventstream connections.

The argument may either be a string path to listen on (defaults to /sse/) or an object:

{ path: '/listen/on/this/path'
, keepalive: 1000 }

keepalive determines the interval time in ms that keepalives will be sent to all connected clients.

sse.on('connection', function(client))

client is a writable stream representing a client connection (request response pair).

Of note, all data sent through this connection will be stringified before sending due to the event stream spec.

client.retry(integer ms)

Send a "retry" message that lets the client know how many MS to wait until retrying a connection that ended.

license

MIT

Repository

https://github.com/chrisdickinson/sse-stream


上一篇:@pollyjs/adapter
下一篇:lambci

相关推荐

  • 通过WebAssembly在浏览器运行PHP

    演示地址:PIB: PHP in Browser,你可以在上面运行PHP代码,然后通过复制地址栏分享代码。 项目地址:oraoto/pib 某天晚上,在看Emscripten的文档,发现Emscri...

    2 年前
  • 追随潮流:WebAssembly快速上手

    WebAssembly 简介 先看官网 webassembly.org/ 介绍: WebAssembly (abbreviated Wasm) is a binary instruction for...

    3 个月前
  • 辅导案例-CSSE1001/7030

    辅导案例-CSSE1001 CSSE1001 Supplementary Programming Exercise Create a simple banking application with a...

    1 年前
  • 辅导案例-CSSE1001

    辅导案例-CSSE1001 CSSE1001 Supplementary Programming Exercise Create a simple banking application with a...

    1 年前
  • 用WebAssembly提升前端应用解压缩性能的尝试

    一、背景 3D形象展示项目的图片及模型等资源以压缩包的形式提供,需要下载并解压后再用Three.js加载并展示出来,其中的解压缩环节使用的是GitHub上获得5.6k Star的JS开源组件库JSZi...

    8 个月前
  • 用 WebAssembly 为 Web 应用提速20倍!(案例研究)

    翻译:疯狂的技术宅 原文:https://www.smashingmagazine.... 本文首发微信公众号:前端先锋 欢迎关注,每天都给你推送新鲜的前端技术文章 在本文中,我们将探讨如何通...

    1 年前
  • 理解 Node.js Stream 模块

    流概念是学习 Node 绕不过去的概念之一,它的底层代码也非常复杂,它能够优化对于文件或者数据处理的内存优化与流程优化,本文主要是讲述了对于 Stream 的实现与使用。

    2 年前
  • 深入浅出WebAssembly(6) Binary Format

    这系列主要是我对WASM研究的笔记,可能内容比较简略。总共包括: 深入浅出WebAssembly(1) Compilation 深入浅出WebAssembly(2) Basic Api 深入浅出We...

    4 个月前
  • 深入浅出WebAssembly(5) Memory

    这系列主要是我对WASM研究的笔记,可能内容比较简略。总共包括: 深入浅出WebAssembly(1) Compilation 深入浅出WebAssembly(2) Basic Api 深入浅出We...

    4 个月前
  • 深入浅出WebAssembly(3) Instructions

    这系列主要是我对WASM研究的笔记,可能内容比较简略。总共包括: 深入浅出WebAssembly(1) Compilation 深入浅出WebAssembly(2) Basic Api 深入浅出We...

    4 个月前

官方社区

扫码加入 JavaScript 社区