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

angular-sse-client

v0.1.1

Published

This library is a wrapper for SSE for angular.

Downloads

78

Readme

AngularSseClient

This library is a wrapper for SSE for angular.

What is SSE?

(Server-sent events - Web APIs, An EventSource instance opens a persistent connection to an HTTP server, which sends events in text/event-stream format. The connection remains open until closed by calling EventSource.close().).

NOTE:

This lib requires typescript 2.7+, see Support 'EventSource' in lib.dom.d.ts · Issue #13666 · microsoft/TypeScript

Supported Browsers

See EventSource#Browser compatibility

NOTE:

You may need to use a polyfill to make it work on more versions of browsers: EventSource/eventsource: EventSource client for Node.js and Browser (polyfill)

Usage

Install

npm install angular-sse-client --save

Code examples

  1. Inject the SseClient or create a new one, then call the get method with an SSE url:

    import { SseClient } from 'angular-sse-client';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.less']
    })
    export class AppComponent implements OnInit {
      constructor(private sseClient: SseClient) {
      }
    
      ngOnInit(): void {
        this.sseClient.get('http://localhost:8181/sse')
          .subscribe(data => {
            console.log('got data from EventSource', data);
          });
      }
    }
  2. If you want to listen to the event source for only amount of time, set duration option, i.e. the Observable will be unsubscribed after duration time:

    this.sseClient.get('http://localhost:8181/sse', { duration: 5000 })
      .subscribe(data => {
        console.log('got data from EventSource', data);
      });
  3. If you want to keep the event source alive after unsubscribing an sseClient, set keepAlive to true, so that other clients listening to the same url will not be affected, i.e. other calls of sseClient.get(url, ...) will still get response from the only one EventSource targeting to that url (by utilizing an event source pool internally):

    This option can make sse clients share the same source, so the maxium connection limitation of SSE connections will be avoided in some extent. See SSE suffers from a limitation to the maximum number of open connections, which can be specially painful when opening various tabs as the limit is per browser and set to a very low number (6)

    this.sseClient.get('http://localhost:8181/sse', { keepAlive: true })
      .subscribe(data => {
        console.log('got data from EventSource', data);
      });

    NOTE: To close the 'alive' event source manually, use:

      import { closeEventSource } from 'angular-sse-client';
    
      closeEventSource('http://localhost:8181/sse');

API

// SseClient
get(url: string, options: {
  withCredentials?: boolean,
  /**
   * Complete the observable after a period of time automatically
   */
  duration?: number,
  /**
   * If set to true, keep event source open (i.e. will not close and keep it in a event source pool for reuse) after unsubscribing
   */
  keepAlive?: boolean,
} = {}): Observable<any>

Build

Run ng build angular-sse-client to build the project. The build artifacts will be stored in the dist/ directory.

Publishing

After building your library with ng build angular-sse-client, go to the dist folder cd dist/angular-sse-client and run npm publish.

Running unit tests

Run ng test angular-sse-client to execute the unit tests via Karma.