generated from ChainSafe/typescript-lib-template
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Fastify SSE Plugin | ||
|
||
 | ||
[](https://badge.fury.io/js/fastify-sse-v2) | ||
|
||
|
@@ -11,9 +12,11 @@ For `[email protected]` use [[email protected]](https://github.com/NodeFactoryIo/fast | |
```terminal | ||
yarn add 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"; | ||
|
||
const server = fastify(); | ||
server.register(FastifySSEPlugin); | ||
|
@@ -22,74 +25,95 @@ server.register(FastifySSEPlugin); | |
#### Sending events from AsyncIterable source | ||
|
||
```javascript | ||
import {FastifySSEPlugin} from "fastify-sse-v2"; | ||
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"}; | ||
} | ||
})()); | ||
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"; | ||
|
||
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"}); | ||
} | ||
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. | ||
|
||
##### Sending events from EventEmmiters | ||
|
||
* [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"; | ||
|
||
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), | ||
}; | ||
} | ||
})() | ||
); | ||
res.sse( | ||
(async function* () { | ||
for await (const [event] of on(eventEmmitter, "update")) { | ||
yield { | ||
event: event.name, | ||
data: JSON.stringify(event), | ||
}; | ||
} | ||
})() | ||
); | ||
}); | ||
``` | ||
|
||
##### Parameter | ||
|
||
- 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 . | ||
|
||
```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 | ||
|
||
- 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()); | ||
` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters