npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-sse-client

v18.0.0

Published

A simple SSE (Server Sent Events) client for Angular applications.

Downloads

23,011

Readme

NGX SSE Client

A simple SSE (Server Sent Events) client for Angular applications to replace the use of EventSource.

This library uses the HttpClient to make the stream request and uses Observable to receive data from server. That way, all requests can be intercepted correctly with HttpInterceptor. It is also possible to decide which request will be made, GET or POST for example, and send other options as in other HttpClient requests.

Basic Usage

Inject the SseClient service to your component and execute the stream method, passing the url string to connect with. Here's a basic example:

import { Component, OnInit } from '@angular/core';
import { SseClient } from 'ngx-sse-client';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
})
export class AppComponent implements OnInit {
  constructor(private sseClient: SseClient) {
    const headers = new HttpHeaders().set('Authorization', `Basic YWRtaW46YWRtaW4=`);

    this.sseClient.stream('/subscribe', { keepAlive: true, reconnectionDelay: 1_000, responseType: 'event' }, { headers }, 'POST').subscribe((event) => {
      if (event.type === 'error') {
        const errorEvent = event as ErrorEvent;
        console.error(errorEvent.error, errorEvent.message);
      } else {
        const messageEvent = event as MessageEvent;
        console.info(`SSE request with type "${messageEvent.type}" and data "${messageEvent.data}"`);
      }
    });
  }
}

stream parameters

Here's a list of possible parameters for the stream method:

| # | name | description | mandatory | | --: | ---------------- | ----------------------------------------- | :-------: | | 1 | url | the endpoint URL | * | | 2 | options | an object of SseOption | | | 3 | requestOptions | the HTTP options to send with the request | | | 4 | method | the HTTP method, default is GET | |

SseOption object

The SseOption is an object with specific options for the SseClient service. Bellow there's the list of possible options:

| name | description | default | | ------------------- | -------------------------------------------------- | :-------: | | keepAlive | true to reconnect after the request is completed | true | | reconnectionDelay | defines a delay before reconnecting | 3 seconds | | responseType | request response type, event or text | event |

keepAlive

When set to true, will automatically reconnect when the request is closed by timeout or completed. In this case, to close the connection is necessary to unsubscribe manually.

reconnectionDelay

Defines a delay before reconnecting with the server. This is only useful when keepAlive is true.

responseType

Defines the response type to be cast from the server to client.

event

A MessageEvent will be returned with the message sent from the server - the type will obey the one of the message.

Otherwise in case of errors, an ErrorEvent with type error will be returned

For example:

this.sseClient.stream('/subscribe').subscribe((event) => {
  if (event.type === 'error') {
    const errorEvent = event as ErrorEvent;
    console.error(errorEvent.error, errorEvent.message);
  } else {
    const messageEvent = event as MessageEvent;
    console.info(`SSE request with type "${messageEvent.type}" and data "${messageEvent.data}"`);
  }
});

text

In this case, only the message data will be returned. For example:

this.sseClient.stream('/subscribe', { responseType: 'text' }).subscribe((data) => console.log(data));

:warning: It is important to know that, if the response type is set to text, no errors will be returned, only the data from successful requests.

CHANGELOG

18.0.0

:warning: Official minimum Angular version support changed to 18.1.2!

17.0.1

  • fixed warning @types/node version, changed to ^18.0.0.
  • fixed the issue when using multiple concurrent streams.

17.0.0

:warning: Official minimum Angular version support changed to 17.3.11!

16.0.0

:warning: Official minimum Angular version support changed to 16.2.12!

15.0.0

:warning: Official minimum Angular version support changed to 15.2.10!

14.0.0

:warning: Official minimum Angular version support changed to 14.3.0!

13.0.0

:warning: Official minimum Angular version support changed to 13.4.0!

:information_source: Change of nomenclature to be consistent with the Angular version. Version 13.x.x of ngx-sse-client is compatible with v13 of Angular, and so on.

3.1.0

3.0.0

:warning: Official minimum Angular version support changed to 12.2.0!

:star: Improvements

  • added support to the context attribute on HttpRequest options.

2.1.1

:beetle: Bug fixes

  • reset progress to zero when recovering from a previous error.

2.1.0

:beetle: Bug fixes

  • unsubscribe the stream request if a server error occurs.

:star: Improvements

  • added the status and statusText attributes to the ErrorEvent, these will hold details from server errors;
  • changed responseType default to event;
  • changed reconnectDelay default to 3 seconds.

Please, feel free to send your contributions. :)