Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mpetrunic/fastify-sse-v2
Provide Server-Sent Events to Fastify
https://github.com/mpetrunic/fastify-sse-v2
fastify fastify-plugin server-sent-events sse typescript
Last synced: about 7 hours ago
JSON representation
Provide Server-Sent Events to Fastify
- Host: GitHub
- URL: https://github.com/mpetrunic/fastify-sse-v2
- Owner: mpetrunic
- License: mit
- Created: 2020-04-07T13:02:38.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-12-05T13:59:10.000Z (17 days ago)
- Last Synced: 2024-12-15T07:05:57.687Z (7 days ago)
- Topics: fastify, fastify-plugin, server-sent-events, sse, typescript
- Language: TypeScript
- Homepage:
- Size: 344 KB
- Stars: 100
- Watchers: 4
- Forks: 21
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Fastify SSE Plugin
![CI check](https://github.com/NodeFactoryIo/fastify-sse-v2/workflows/CI%20check/badge.svg?branch=master)
[![npm version](https://badge.fury.io/js/fastify-sse-v2.svg)](https://badge.fury.io/js/fastify-sse-v2)Fastify plugin for sending [Server-sent events](https://en.wikipedia.org/wiki/Server-sent_events).
For `[email protected]` use [[email protected]](https://github.com/NodeFactoryIo/fastify-sse-v2/tree/1.x)!
### How to use?
```terminal
yarn add fastify-sse-v2
```Register fastify-sse-v2 plugin into your fastify instance:
```javascript
import { FastifySSEPlugin } from "fastify-sse-v2";const server = fastify();
server.register(FastifySSEPlugin);
```#### Sending events from AsyncIterable source
```javascript
import { FastifySSEPlugin } from "fastify-sse-v2";const server = fastify();
server.register(FastifySSEPlugin);server.get("/", function (req, res) {
res.sse(
(async function* source() {
for (let i = 0; i < 10; i++) {
sleep(2000);
yield { id: String(i), data: "Some message" };
}
})()
);
});
```#### Sending individual events
```javascript
import { FastifySSEPlugin } from "fastify-sse-v2";const server = fastify();
server.register(FastifySSEPlugin);server.get("/", async function (req, res) {
for (let i = 0; i < 10; i++) {
await sleep(2000);
res.sse({ id: String(i), data: "Some message" });
}
});fastify.get("/listenForChanges", {}, (request, reply) => {
const listenStream = fastify.db
.watch("doc-uuid")
.on("data", (data) => reply.sse({ data: JSON.stringify(data) }))
.on("delete", () => reply.sse({ event: "close" }));
request.socket.on("close", () => listenStream.end());
});
```##### Note
- When sending individual events, the connection is kept open until you call `reply.sseContext.source.end()` to terminate the stream.
##### Sending events from EventEmmiters
- [not supported in all nodejs versions](https://nodejs.org/api/events.html#events_events_on_emitter_eventname_options)
```javascript
import { FastifySSEPlugin } from "fastify-sse-v2";
import { on } from "events";const server = fastify();
server.register(FastifySSEPlugin);server.get("/", function (req, res) {
res.sse(
(async function* () {
for await (const [event] of on(eventEmmitter, "update")) {
yield {
event: event.name,
data: JSON.stringify(event),
};
}
})()
);
});
```##### Note
- to remove event listeners (or some other cleanup) when client closes connection,
you can listen on connection closing event: `request.socket.on('close', () => abortController.abort());
`##### Change server send retry behavior
```javascript
import { FastifySSEPlugin } from "fastify-sse-v2";const server = fastify();
server.register(FastifySSEPlugin) // retryDelay default 3000
server.register(FastifySSEPlugin, {
retryDelay: false // disable retryDelay
retryDelay: 5000 // override 5000
})
```##### Change default highWaterMark
```javascript
import { FastifySSEPlugin } from "fastify-sse-v2";const server = fastify();
server.register(FastifySSEPlugin) // highWaterMark defaults to 16384 bytes (16kb)
server.register(FastifySSEPlugin, {
highWaterMark: 1024 // override default setting of 16384 (16kb) with 1024 (1kb)
})
```##### Note
- You can set parameter `retryDelay` to `false` to disable the default behavior of sending retry, or set parameter `retryDelay` to `milliseconds` override the default 3000 retry interval .
- You can set parameter `highWaterMark` to define the buffer size (in bytes) that determines when the buffer is full and a 'flush' should be performed. Default is 16kb. (![Learn more](https://nodejs.org/en/learn/modules/backpressuring-in-streams#how-does-backpressure-resolve-these-issues))