Skip to content

Add done event #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,23 @@ es.addEventListener("error", (event) => {
}
});

es.addEventListener("done", (event) => {
console.log("Done SSE connection.");
});

es.addEventListener("close", (event) => {
console.log("Close SSE connection.");
});
```

### Done vs Close

`done` events will fire when server closes the connection.
Reconnections will occur indefinitely, unless this behavior is disabled.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be rephrased slightly to make it more clear. Maybe something like:

By default, the client will automatically reconnect when this happens. You can disable the reconnections by setting the pollingInterval option to 0".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me. Updated.

`close` events will fire when the connection is terminated by the client, using `.close()`.

### Headers and params

If you want to use Bearer token and/or topics, look at this example (TypeScript):

```typescript
Expand Down
7 changes: 6 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type BuiltInEventType = 'open' | 'message' | 'error' | 'close';
export type BuiltInEventType = 'open' | 'message' | 'error' | 'done' | 'close';
export type EventType<E extends string = never> = E | BuiltInEventType;

export interface MessageEvent {
Expand All @@ -12,6 +12,10 @@ export interface OpenEvent {
type: 'open';
}

export interface DoneEvent {
type: 'done';
}

export interface CloseEvent {
type: 'close';
}
Expand Down Expand Up @@ -55,6 +59,7 @@ export interface EventSourceOptions {
type BuiltInEventMap = {
'message': MessageEvent,
'open': OpenEvent,
'done': DoneEvent,
'close': CloseEvent,
'error': ErrorEvent | TimeoutEvent | ExceptionEvent,
};
Expand Down
2 changes: 2 additions & 0 deletions src/EventSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class EventSource {
open: [],
message: [],
error: [],
done: [],
close: [],
};

Expand Down Expand Up @@ -115,6 +116,7 @@ class EventSource {
this._handleEvent(xhr.responseText || '');

if (xhr.readyState === XMLHttpRequest.DONE) {
this.dispatch('done', { type: 'done' });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would strongly recommend to dispatch the done event after calling _pollAgain. This would allow users to cancel the reconnection by calling .close() from the done handler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I just updated and tested that it works as expected.

this._logDebug('[EventSource][onreadystatechange][DONE] Operation done.');
this._pollAgain(this.interval, false);
}
Expand Down