Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-sse-v2

Package Overview
Dependencies
Maintainers
0
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-sse-v2 - npm Package Compare versions

Comparing version 4.0.0 to 4.1.0

4

lib/plugin.js

@@ -35,3 +35,5 @@ "use strict";

this.raw.setHeader("x-no-compression", 1);
this.raw.write((0, sse_1.serializeSSEEvent)({ retry: options.retryDelay || 3000 }));
if (options.retryDelay !== false) {
this.raw.write((0, sse_1.serializeSSEEvent)({ retry: options.retryDelay || 3000 }));
}
handleAsyncIterable(this, this.sseContext.source);

@@ -38,0 +40,0 @@ }

@@ -5,3 +5,3 @@ export interface SsePluginOptions {

*/
retryDelay?: number;
retryDelay?: false | number;
}
{
"name": "fastify-sse-v2",
"version": "4.0.0",
"version": "4.1.0",
"packageManager": "yarn@1.22.19",

@@ -5,0 +5,0 @@ "description": "Fastify plugin for sending server side events.",

# Fastify SSE Plugin
![CI check](https://github.com/NodeFactoryIo/fastify-sse-v2/workflows/CI%20check/badge.svg?branch=master)

@@ -14,5 +15,7 @@ [![npm version](https://badge.fury.io/js/fastify-sse-v2.svg)](https://badge.fury.io/js/fastify-sse-v2)

```
Register fastify-sse-v2 plugin into your fastify instance:
```javascript
import {FastifySSEPlugin} from "fastify-sse-v2";
import { FastifySSEPlugin } from "fastify-sse-v2";

@@ -26,3 +29,3 @@ const server = fastify();

```javascript
import {FastifySSEPlugin} from "fastify-sse-v2";
import { FastifySSEPlugin } from "fastify-sse-v2";

@@ -33,14 +36,17 @@ const server = fastify();

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"};
}
})());
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";
import { FastifySSEPlugin } from "fastify-sse-v2";

@@ -51,17 +57,19 @@ const server = fastify();

server.get("/", async function (req, res) {
for (let i = 0; i < 10; i++) {
await sleep(2000);
res.sse({id: String(i), data: "Some message"});
}
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())
})
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.

@@ -71,7 +79,7 @@

* [not supported in all nodejs versions](https://nodejs.org/api/events.html#events_events_on_emitter_eventname_options)
- [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";
import { FastifySSEPlugin } from "fastify-sse-v2";
import { on } from "events";

@@ -82,20 +90,38 @@ const server = fastify();

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),
};
}
})()
);
res.sse(
(async function* () {
for await (const [event] of on(eventEmmitter, "update")) {
yield {
event: event.name,
data: JSON.stringify(event),
};
}
})()
);
});
```
##### Note
##### 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());
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
})
```
##### 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 .

@@ -29,5 +29,7 @@ import { EventMessage, FastifyPluginAsync, FastifyReply } from "fastify";

this.raw.setHeader("x-no-compression", 1);
this.raw.write(
serializeSSEEvent({ retry: options.retryDelay || 3000 })
);
if (options.retryDelay !== false) {
this.raw.write(
serializeSSEEvent({ retry: options.retryDelay || 3000 })
);
}
handleAsyncIterable(this, this.sseContext.source);

@@ -34,0 +36,0 @@ }

@@ -5,3 +5,3 @@ export interface SsePluginOptions {

*/
retryDelay?: number;
retryDelay?: false | number;
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc