You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
8
8
## Unreleased
9
9
10
+
## 0.15.0 - 2025-05-22
11
+
10
12
### Added
11
13
12
14
* 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).
Copy file name to clipboardExpand all lines: docs/src/content/docs/guides/batching.mdx
+22-20Lines changed: 22 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,48 +14,50 @@ While SSE is already performant and bandwidth-efficient, sending many events at
14
14
15
15
## Using the `batch` method
16
16
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:
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.
26
26
27
27
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.
28
28
29
29
<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.
31
31
</Aside>
32
32
33
33
## Create your own event buffer
34
34
35
35
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:
36
36
37
37
```typescript title="server.ts"
38
-
import { createEventBuffer } from"better-sse";
38
+
import { createEventBuffer } from"better-sse"
39
39
40
-
const buffer =createEventBuffer();
40
+
const buffer =createEventBuffer()
41
41
42
-
buffer.push("One");
43
-
buffer.push("Two");
44
-
buffer.push("Three");
42
+
buffer.push("One")
43
+
buffer.push("Two")
44
+
buffer.push("Three")
45
45
46
-
session.batch(buffer);
46
+
awaitsession.batch(buffer)
47
47
```
48
48
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:
50
50
51
51
```typescript title="server.ts"
52
-
channel.activeSessions.forEach((session) => {
53
-
session.batch(buffer);
54
-
});
52
+
awaitPromise.all(
53
+
channel.activeSessions.map((session) =>
54
+
session.batch(buffer)
55
+
)
56
+
)
55
57
```
56
58
57
-
<Asidetitle="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
+
<Asidetitle="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.
59
61
60
62
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.
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.
66
68
67
69
```typescript title="server.ts"
68
-
import { createEventBuffer } from"better-sse";
70
+
import { createEventBuffer } from"better-sse"
69
71
70
-
const buffer =createEventBuffer();
72
+
const buffer =createEventBuffer()
71
73
72
74
buffer
73
75
.retry(2400)
@@ -76,11 +78,11 @@ buffer
76
78
.data("one")
77
79
.data("two")
78
80
.data("three")
79
-
.dispatch();
81
+
.dispatch()
80
82
81
-
res.write(buffer.read());
83
+
res.write(buffer.read())
82
84
83
-
buffer.clear();
85
+
buffer.clear()
84
86
```
85
87
86
88
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