Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
c964695
add createSseSession utility
joshmossas Dec 1, 2023
a0647c2
rename SseSession to EventStream
joshmossas Dec 1, 2023
89c4389
add jsdoc comments
joshmossas Dec 1, 2023
cab3418
Merge remote-tracking branch 'upstream/main'
joshmossas Dec 8, 2023
033938c
add support for "retry" field
joshmossas Dec 8, 2023
67caec4
add sse example
joshmossas Dec 8, 2023
1e8e551
fix "no-void" linting err
joshmossas Dec 8, 2023
4fc15fc
allow `eventStream.push()` to accept string or EventStreamMessage object
joshmossas Dec 8, 2023
0d06bbd
remove http2 incompatible header
joshmossas Dec 9, 2023
5ddc07e
add sendEventStream utility
joshmossas Dec 30, 2023
7767eb3
add sendEventStream to docs
joshmossas Dec 30, 2023
c064b55
cleanup pause and resume implementations
joshmossas Dec 30, 2023
2e9ac10
add note about additional task to complete when HTTP/2 support comes
joshmossas Dec 30, 2023
033fc33
Merge branch 'main' of https://github.com/unjs/h3
joshmossas Jan 9, 2024
5477ed6
add more complete response headers to prevent caching issues
joshmossas Jan 9, 2024
35e58b0
update notes
joshmossas Jan 9, 2024
f1f11dd
Merge branch 'main' of https://github.com/unjs/h3
joshmossas Jan 24, 2024
3aed07c
remove any cast (workaround from setResponseHeaders change)
joshmossas Jan 24, 2024
c6d52aa
Merge branch 'main' of https://github.com/unjs/h3
joshmossas Jan 30, 2024
216bf30
- add an "autoclose" option which auto closes the stream when the con…
joshmossas Feb 6, 2024
733ea17
Merge branch 'main' of https://github.com/unjs/h3
joshmossas Feb 21, 2024
fdd00f1
add way to detect if current request is http2 or not
joshmossas Feb 21, 2024
b6f6324
.push() now also accepts an array of events
joshmossas Feb 21, 2024
ebd0e70
update sse tests
joshmossas Feb 21, 2024
ab63a9f
remove unneeded comment
joshmossas Feb 21, 2024
77451c2
better detect if writer has already closed to prevent potential crashes
joshmossas Feb 23, 2024
db16b1f
Merge branch 'main' into pr/joshmossas/586
pi0 Feb 24, 2024
7133cd4
refactor structure
pi0 Feb 24, 2024
c5c0e6b
simpify example
pi0 Feb 24, 2024
c22f8d4
add experimental notice
pi0 Feb 24, 2024
590bef1
add docs with automd
pi0 Feb 24, 2024
57ad513
refactor: make it more self contained
pi0 Feb 24, 2024
3bc58d7
lint
pi0 Feb 24, 2024
065b40a
use better try/catch
pi0 Feb 24, 2024
a5e5ef6
update test
pi0 Feb 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add support for "retry" field
  • Loading branch information
joshmossas committed Dec 8, 2023
commit 033938c325871a423d717ad9374f7f64cf5aeb24
18 changes: 13 additions & 5 deletions src/utils/sse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { H3Event } from "src/event";
* ```ts
* const eventStream = createEventStream(event);
*
* // start streaming
* eventStream.start();
*
* // send messages
* const interval = setInterval(async () => {
* eventStream.push({data: "hello world"});
Expand All @@ -19,8 +22,7 @@ import { H3Event } from "src/event";
* clearInterval(interval);
* })
*
* // start streaming
* eventStream.start();

* ```
*
*/
Expand Down Expand Up @@ -86,15 +88,18 @@ export class EventStream {
/**
* Start streaming events
*/
async start() {
start() {
this.h3Event._handled = true;
await sendStream(this.h3Event, this.stream);
void sendStream(this.h3Event, this.stream);
}
}

/**
* See https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#fields
*/
export interface EventStreamMessage {
id?: string;
event?: string;
retry?: number;
data: string;
}

Expand All @@ -106,6 +111,9 @@ export function formatEventStreamMessage(message: EventStreamMessage): string {
if (message.event) {
result += `event: ${message.event}\n`;
}
if (typeof message.retry === "number" && Number.isInteger(message.retry)) {
result += `retry: ${message.retry}\n`;
}
result += `data: ${message.data}\n\n`;
return result;
}
Expand Down