https://github.com/codemonument/deno_sse_codec
A module which provides methods for encoding and decoding sse-events (Server Sent Events), cross-posted to npm as @codemonument/sse-codec
https://github.com/codemonument/deno_sse_codec
Last synced: 7 months ago
JSON representation
A module which provides methods for encoding and decoding sse-events (Server Sent Events), cross-posted to npm as @codemonument/sse-codec
- Host: GitHub
- URL: https://github.com/codemonument/deno_sse_codec
- Owner: codemonument
- License: mit
- Created: 2022-05-24T17:31:44.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-11-15T20:56:21.000Z (over 3 years ago)
- Last Synced: 2025-08-31T08:55:03.061Z (10 months ago)
- Language: TypeScript
- Homepage:
- Size: 39.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: Readme.md
- Changelog: Changelog.md
- License: LICENSE
Awesome Lists containing this project
README
# SSE Codec
A module which provides encoding & decoding functionality for SSE Events (Server Sent Events), as well as providing some types.
Deployed to [https://deno.land/x/sse_codec](https://deno.land/x/sse_codec)
Cross-Copiled by deno's dnt module and posted to npm as [@codemonument/sse-codec](https://www.npmjs.com/package/@codemonument/sse-codec)
## Imports in Deno
```
// uses 'latest' version
import { encodeSSEEvent } from "https://deno.land/x/sse_codec"
// uses a specific version
import { encodeSSEEvent } from "https://deno.land/x/sse_codec@0.2.1";
```
## Imports in Node
```
// install first via npm i -S @codemonument/sse-codec
import { encodeSSEEvent } from "@codemonument/sse-codec"
```
## Usage (same in Deno & Node)
See the test files for most elaborate usage descriptions.
He'res the most important usage information.
### Usage encodeSSEEvent
```
const sseString = encodeSSEEvent({
name: "custom-event",
data: "Some simple string data",
id: "UID5346324874238475",
retry: 5000,
});
```
### Usage SSEStream class
```
const sseStream = new SSEStream();
sseStream.emit({ eventName: "event1" });
sseStream.emit({ eventName: "event2" });
// Example usage of the sseStream
// normally you would not read it yourself
// but pass the readable stream to something which uses it for something
const reader = sseStream.readableStream.getReader();
const chunk1 = await reader.read();
const chunk2 = await reader.read();
console.log(chunk1);
console.log(chunk2);
// close the sseStream after using it
sseStream.end();
```