Skip to content

Commit b5d30d9

Browse files
krismeistermarcospds
authored andcommitted
bug: server response ends with non descript error (#30)
Problem When the stream ends purposefully by the server ( response.end() ) a non descript error is sent to the subscriber. Proposed Fix Depending on the mode of the keepAlive flag the observable will either dispatch a friendly error message, or will complete() immediately.
1 parent 1a59049 commit b5d30d9

File tree

6 files changed

+49
-5
lines changed

6 files changed

+49
-5
lines changed

lib/projects/ngx-sse-client/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ this.sseClient.stream('/subscribe', { responseType: 'text' }).subscribe((data) =
110110
111111
## CHANGELOG
112112

113+
### 19.0.1
114+
115+
- fixed server response ends with non descript error on `keepAlive` _false_.
116+
113117
### 19.0.0
114118

115119
:warning: Official minimum `Angular` version support changed to **19.1.3**!

lib/projects/ngx-sse-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-sse-client",
3-
"version": "19.0.0",
3+
"version": "19.0.1",
44
"description": "A simple SSE (Server Sent Events) client for Angular applications.",
55
"keywords": [
66
"angular",

lib/projects/ngx-sse-client/src/lib/sse-client-subscriber.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ export class SseClientSubscriber {
8181
this.chunk = '';
8282
this.progress = 0;
8383

84-
this.dispatchStreamData(this.errorEvent(), observer);
84+
if (this.sseOptions.keepAlive) {
85+
const message = `Server response ended, will reconnect in ${this.sseOptions.reconnectionDelay}ms`;
86+
this.dispatchStreamData(this.errorEvent({ status: 1, message }), observer);
87+
} else {
88+
observer.complete();
89+
}
8590
}
8691

8792
private parseEventData(part: string, observer: Subscriber<string>) {

lib/projects/sample/src/app/app.component.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ small {
55
.events {
66
display: flex;
77
justify-content: space-between;
8-
max-width: 1200px;
8+
font-family: monospace;
9+
max-width: 1800px;
10+
padding-right: 10px;
911
}
1012

1113
.buttons {

lib/projects/sample/src/app/app.component.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ <h1>
66
<div *ngFor="let event of sseEvents">{{ event }}</div>
77
</div>
88

9+
<div class="event">
10+
<h1>SSE Events <small>(using <code>ngx-sse-client</code> with <code>keepAlive: false</code>):</small></h1>
11+
<div *ngFor="let event of sseEventsWithoutKeepalive">{{ event }}</div>
12+
</div>
13+
914
<div class="event">
1015
<h1>SOURCE Events <small>(not compatible with IE):</small></h1>
1116
<div *ngFor="let event of sourceEvents">{{ event }}</div>

lib/projects/sample/src/app/app.component.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,44 @@ export class AppComponent {
1414
private apiBaseUrl = environment?.apiBaseUrl || '';
1515

1616
public sseEvents: string[] = [];
17+
public sseEventsWithoutKeepalive: string[] = [];
1718
public sourceEvents: string[] = [];
1819

1920
constructor(private httpClient: HttpClient, private sseClient: SseClient) {
20-
this.sseClient.stream(`${this.apiBaseUrl}/subscribe`).subscribe((e) => {
21+
this.sseClient.stream(`${this.apiBaseUrl}/subscribe`).subscribe({
22+
next:(e) => {
2123
if (e.type === 'error') {
2224
const event = e as SseErrorEvent;
2325
this.sseEvents.push(`ERROR: ${event.message}, STATUS: ${event.status}, STATUS TEXT: ${event.statusText}`);
2426
} else {
2527
const data = (e as MessageEvent).data;
26-
this.sseEvents.push(data);
28+
this.sseEvents.push(data);
29+
}
30+
},
31+
error: (e) => {
32+
console.error(e);
33+
},
34+
complete: () => {
35+
this.sseEvents.push('COMPLETE - this should never happen');
36+
}
37+
});
38+
39+
40+
this.sseClient.stream(`${this.apiBaseUrl}/subscribe`, { keepAlive: false }).subscribe({
41+
next:(e) => {
42+
if (e.type === 'error') {
43+
const event = e as SseErrorEvent;
44+
this.sseEventsWithoutKeepalive.push(`ERROR: ${event.message}, STATUS: ${event.status}, STATUS TEXT: ${event.statusText}`);
45+
} else {
46+
const data = (e as MessageEvent).data;
47+
this.sseEventsWithoutKeepalive.push(data);
48+
}
49+
},
50+
error: (e) => {
51+
console.error(e);
52+
},
53+
complete: () => {
54+
this.sseEventsWithoutKeepalive.push('COMPLETE');
2755
}
2856
});
2957

0 commit comments

Comments
 (0)