Skip to content

Commit 7598a10

Browse files
authored
Merge pull request #94 from MatthewWid/fetch-docs
Fetch API Documentation
2 parents 2f5d5d2 + 2c9673b commit 7598a10

File tree

13 files changed

+634
-244
lines changed

13 files changed

+634
-244
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
## 0.15.0 - 2025-05-22
11+
1012
### Added
1113

1214
* Added support for the `Session` constructor to be able to take in a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) (and optionally a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)) object from the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).

docs/src/content/docs/guides/batching.mdx

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,50 @@ While SSE is already performant and bandwidth-efficient, sending many events at
1414

1515
## Using the `batch` method
1616

17-
To batch and send multiple events at a time, simply invoke [`Session#batch`](/better-sse/reference/api#sessionbatch-batcher-eventbuffer--buffer-eventbuffer--void--promisevoid--promisevoid) method and pass a callback that takes an [event buffer](/better-sse/reference/api#eventbuffer) as its first argument:
17+
To batch and send multiple events at a time, simply invoke the [`Session#batch`](/better-sse/reference/api#sessionbatch-batcher-eventbuffer--buffer-eventbuffer--void--promisevoid--promisevoid) method and pass a callback that takes an [event buffer](/better-sse/reference/api#eventbuffer) as its first argument:
1818

1919
```typescript title="server.ts"
2020
await session.batch(async (buffer) => {
2121
await buffer.iterate(["My", "huge", "event", "list"]);
2222
});
2323
```
2424

25-
You can use the same helper methods as you would with the session itself ([`push`](/better-sse/reference/api#sessionpush-data-unknown-eventname-string-eventid-string--this), [`stream`](/better-sse/reference/api#sessionstream-stream-readable-options-object--promiseboolean) and [`iterate`](/better-sse/reference/api#sessioniterate-iterable-iterable--asynciterable-options-object--promisevoid)) with the buffer.
25+
You can use the same helper methods as you would with the session itself ([`push`](/better-sse/reference/api#sessionpush-data-unknown-eventname-string-eventid-string--this), [`stream`](/better-sse/reference/api#sessionstream-stream-streamreadable--readablestream-options-object--promiseboolean) and [`iterate`](/better-sse/reference/api#sessioniterate-iterable-iterable--asynciterable-options-object--promisevoid)) with the buffer.
2626

2727
When the callback finishes execution - or resolves if it returns a promise - every event created with the buffer will be sent to the client all at once in a single network transmission.
2828

2929
<Aside>
30-
Events created with event buffers do not use the underlying `Session#push` method and will *not* trigger the `push` event to be emitted on the session instance.
30+
Events created with event buffers do **not** use the underlying `Session#push` method and will *not* trigger the `push` event to be emitted on the session instance.
3131
</Aside>
3232

3333
## Create your own event buffer
3434

3535
You can also create an event buffer outside of the context of a single session and then write its contents to one or many sessions later on. To do so, create an event buffer and then pass it directly to the `batch` method:
3636

3737
```typescript title="server.ts"
38-
import { createEventBuffer } from "better-sse";
38+
import { createEventBuffer } from "better-sse"
3939

40-
const buffer = createEventBuffer();
40+
const buffer = createEventBuffer()
4141

42-
buffer.push("One");
43-
buffer.push("Two");
44-
buffer.push("Three");
42+
buffer.push("One")
43+
buffer.push("Two")
44+
buffer.push("Three")
4545

46-
session.batch(buffer);
46+
await session.batch(buffer)
4747
```
4848

49-
Or send the buffer contents to every session on a [channel](/better-sse/guides/channels), for example:
49+
Or send the buffer contents to every session on a channel, for example:
5050

5151
```typescript title="server.ts"
52-
channel.activeSessions.forEach((session) => {
53-
session.batch(buffer);
54-
});
52+
await Promise.all(
53+
channel.activeSessions.map((session) =>
54+
session.batch(buffer)
55+
)
56+
)
5557
```
5658

57-
<Aside title="Sanitize your data" type="caution" >
58-
Keep in mind that, unless you explicitly pass them via [the `sanitizer` and/or `serializer` constructor arguments](/better-sse/reference/api/#new-eventbufferoptions-), event buffers do *not* have the same data serializer and sanitizer functions as the session being called.
59+
<Aside title="Sanitize your data" type="caution">
60+
Keep in mind that, unless you explicitly pass them via [the `sanitizer` and/or `serializer` constructor arguments](/better-sse/reference/api/#new-eventbufferoptions-eventbufferoptions), event buffers do **not** have the same data serializer and sanitizer functions as the session being called.
5961

6062
This does not apply when you use the [`Session#batch`](/better-sse/reference/api/#sessionbatch-batcher-eventbuffer--buffer-eventbuffer--void--promisevoid--promisevoid) method as it will automatically copy the serializer and sanitizer from the session to the event buffer instance it creates for you.
6163
</Aside>
@@ -65,9 +67,9 @@ channel.activeSessions.forEach((session) => {
6567
For users needing more fine-grained control over the exact data being sent over the wire, event buffers also allow you to write [raw spec-compliant SSE fields](https://html.spec.whatwg.org/multipage/server-sent-events.html#processField) into a string that can be read and sent over the wire as-is.
6668

6769
```typescript title="server.ts"
68-
import { createEventBuffer } from "better-sse";
70+
import { createEventBuffer } from "better-sse"
6971

70-
const buffer = createEventBuffer();
72+
const buffer = createEventBuffer()
7173

7274
buffer
7375
.retry(2400)
@@ -76,11 +78,11 @@ buffer
7678
.data("one")
7779
.data("two")
7880
.data("three")
79-
.dispatch();
81+
.dispatch()
8082

81-
res.write(buffer.read());
83+
res.write(buffer.read())
8284

83-
buffer.clear();
85+
buffer.clear()
8486
```
8587

8688
This is an advanced use-case. For most users, you should still stick with using [`Session`](/better-sse/reference/api#sessionstate) by default.

0 commit comments

Comments
 (0)