{"library":"ngx-sse-client","title":"Angular Server-Sent Events Client","description":"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.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install ngx-sse-client"],"cli":null},"imports":["import { SseClient } from 'ngx-sse-client';","import { SseOption } from 'ngx-sse-client';","import { HttpHeaders } from '@angular/common/http';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Component, OnInit } from '@angular/core';\nimport { HttpHeaders } from '@angular/common/http';\nimport { SseClient } from 'ngx-sse-client';\n\n@Component({\n  selector: 'app-root',\n  template: `<h1>SSE Stream Example</h1><p>Check console for events.</p>`,\n})\nexport class AppComponent implements OnInit {\n  constructor(private sseClient: SseClient) {}\n\n  ngOnInit(): void {\n    const headers = new HttpHeaders().set('Authorization', `Basic YWRtaW46YWRtaW4=`);\n\n    // Example of subscribing to an SSE stream with custom options and POST method\n    this.sseClient.stream(\n      '/api/subscribe', // Replace with your SSE endpoint\n      { keepAlive: true, reconnectionDelay: 1_000, responseType: 'event' },\n      { headers },\n      'POST'\n    ).subscribe({\n      next: (event) => {\n        if (event.type === 'error') {\n          const errorEvent = event as ErrorEvent;\n          console.error('SSE Error Event:', errorEvent.error, errorEvent.message);\n        } else {\n          const messageEvent = event as MessageEvent;\n          console.info(`SSE Message (${messageEvent.type}):`, messageEvent.data);\n        }\n      },\n      error: (err) => {\n        console.error('SSE Subscription Error:', err);\n      },\n      complete: () => {\n        console.log('SSE Stream Completed.');\n      }\n    });\n  }\n}","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}