ngx-sse-client is an Angular library providing a robust client for Server-Sent Events (SSE), designed as an alternative to the native `EventSource` API. Currently at stable version 21.0.0, this package typically releases new major versions in alignment with Angular's own major releases, ensuring compatibility with the latest Angular ecosystem. Its key differentiator lies in leveraging Angular's `HttpClient` for SSE stream requests, which enables seamless integration with `HttpInterceptor` for authentication, logging, and error handling. It also supports flexible HTTP methods (e.g., POST instead of just GET) and exposes event streams as RxJS Observables, aligning with Angular's reactive programming paradigm. This allows for more advanced control and error management compared to traditional `EventSource` implementations, which lack interceptor support and direct Observable integration.
npm install ngx-sse-clientVerified import paths — ran on the pinned version, not inferred.
Demonstrates injecting the `SseClient` service, subscribing to an SSE stream with custom options (keep-alive, reconnection delay, event response type), including HTTP headers for authorization, and using a POST method. It also shows handling both `MessageEvent` and `ErrorEvent` responses within the subscription.
Always check the `peerDependencies` in `package.json` or the changelog for `ngx-sse-client` to ensure compatibility with your Angular version before upgrading. Upgrade `ngx-sse-client` to the major version that matches your Angular version.
For comprehensive error handling, especially during development or for robust applications, use `responseType: 'event'`. This allows you to differentiate between `MessageEvent` and `ErrorEvent` and react to stream errors. Only use `responseType: 'text'` if you are certain your server will not send error events or if you prefer to handle all stream interruptions as completion events.
Ensure you call `.unsubscribe()` on the `Subscription` object obtained from `sseClient.stream().subscribe()` when the component or service using it is destroyed or no longer needs the stream. For Angular components, this typically means implementing `ngOnDestroy`.
Configure your proxy server (e.g., Nginx) to disable buffering for SSE endpoints. Common Nginx configurations include `proxy_buffering off;` and `proxy_set_header Connection '';` for the SSE location. Also, ensure your application server sends `X-Accel-Buffering: no` header.
Add `SseClient` to the `providers` array in your `AppModule` (or a feature module where it's used) to make it injectable: `providers: [SseClient]`.
Ensure your `ngx-sse-client` version is up-to-date (currently v21.0.0 for Angular 21) and that your `stream` method call matches the signature of the installed version. You may need to update the library: `npm install ngx-sse-client@latest`.
Use type guards or type assertions within your subscription callback to correctly infer the type of the event. For example: `if (event.type === 'error') { const errorEvent = event as ErrorEvent; /* ... */ } else { const messageEvent = event as MessageEvent; /* ... */ }`.