jfurrow / koa-event-stream

Koa SSE (Server Side Events) Middleware :tada:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

koa-event-stream

Language Node Version Yarn Version Licence Info

KoaJs Slack

Koa.js middleware to stream events (using Server Sent Events) to clients without WebSockets.

  • ๐ŸŽ‰ First class Typescript support
  • ๐Ÿ“ก Realtime events over plain HTTP
  • ๐Ÿ’ก Serve as a REST endpoint route
  • โ˜๏ธ Stateless by design
  • ๐Ÿ‘Œ Simple unopinionated API

Table of Contents

Installation

$ npm install --save koa-event-stream

...
$ yarn add koa-event-stream

...

Usage

import * as Koa from "koa";
import KoaSSE from "../../src/index";

const app = new Koa();

app.use(KoaSSE());

app.use(async (ctx: Koa.Context) => {
    let n = 0;
    const interval = setInterval(() => {
        ctx.sse.send(new Date().toString());
        n++;
        if (n >= 5) {
            ctx.sse.end();
            clearInterval(interval);
        }
    }, 1000);
    ctx.sse.on("finish", () => clearInterval(interval));
});

app.listen(5000);

ctx.sse.send(data)

ctx.sse.send(data)

ctx.sse.end()

ctx.sse.end()

Examples

TBC

Support

Please open an issue for support.

Contributing

Please contribute using Github Flow. Create a branch, add commits, and open a pull request.

License

MIT : http://opensource.org/licenses/MIT

Author

Jarvis Prestidge | jarvisprestidge@gmail.com

About

Koa SSE (Server Side Events) Middleware :tada:


Languages

Language:TypeScript 100.0%