express-ts-sse
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

express-ts-sse

npm version Build Status Code Climate codecov

NPM

An Express middleware for quick'n'easy server-sent events.

About

express-ts-sse is meant to keep things simple. It's use ts refactor and base on express-ts-sse library. You need to send server-sent events without too many complications and fallbacks? This is the library to do so.

Install:

pnpm i express-ts-sse

Usage example:

Options:

You can pass an optional options object to the constructor. Currently it only supports changing the way initial data is treated. If you set isSerialized to false, the initial data is sent as a single event. The default value is true.

import { EventSource } from "express-ts-sse"; // or import EventSource from "express-ts-sse"
var sse = new SSE(["array", "containing", "initial", "content", "(optional)"], {
  isSerialized: false,
  initialEvent: "optional initial event name",
});

Server:

var EventSource = require('express-ts-sse');
var sse = new EventSource(["array", "containing", "initial", "content", "(optional)"]);

...

app.get('/stream', sse.init);

...

sse.send(content);
sse.send(content, eventName);
sse.send(content, eventName, customID);
sse.updateInit(["array", "containing", "new", "content"]);
sse.serialize(["array", "to", "be", "sent", "as", "serialized", "events"]);

Client:

var es = new EventSource('/stream');

es.onmessage = function (event) {
  ...
};

es.addEventListener(eventName, function (event) {
  ...
});

Package Sidebar

Install

npm i express-ts-sse

Weekly Downloads

14

Version

1.0.2

License

MIT

Unpacked Size

11.9 kB

Total Files

6

Last publish

Collaborators

  • lby20010429