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-pusher

v0.2.5

Published

Lightweight Angular library for integrating Pusher into Angular applications.

Downloads

271

Readme

ngx-pusher

Lightweight Angular library for integrating Pusher into Angular applications.

GitHub release (latest by date) npm bundle size NPM CircleCI GitHub issues

Table Of Contents

About

A lightweight Angular library for adding PusherJs API to your Angular 13+ app. It allows you to seamlessly subscribe to Pusher channels and operate on Observable event streams.

Installation

npm install --save ngx-pusher

If you are using yarn

yarn add ngx-pusher

Getting Started

  1. Create custom authorizer [OPTIONAL] Your authorizer class must implement the NgxPusherAuthorizer interface.

    export class CustomPusherAuthorizer implements NgxPusherAuthorizer {
      constructor(private http: HttpClient) {}
       
      authorize(channelName: string, socketId: string): Observable<NgxPusherAuth> {
        return this.http.post<NgxPusherAuth>('/auth/pusher', {
          socketId,
          channelName,
        });
      }
    }
  2. Add NgxPusherModule to your root module

    @NgModule({
      declarations: [AppComponent],
      imports: [
        BrowserModule,
        HttpClientModule,
       
        /* import ngx-pusher */
           
        NgxPusherModule.forRoot({
          appKey: environment.pusherAppKey,
          pusherOptions: {
            cluster: environment.pusherCluster,
          },
             
          /* register custom authorizer (optional) */
             
          authorizer: {
            deps: [HttpClient],
            useFactory: (http: HttpClient) => new CustomPusherAuthorizer(http),
            // useClass: CustomPusherAuthorizer,
            // useExisting: CustomPusherAuthorizer
          },
        }),
           
      ],
      bootstrap: [AppComponent],
    })
    export class AppModule {}
  3. Default Channel [OPTIONAL] You can set up a default (private/presence) channel for the logged-in user.

    @Injectable()
    export class AuthService {
      constructor(private pusherService: NgxPuserServicee) {}
       
      userLoggedIn(user: IUser) {
        this.pusherService.subscribe(`private-${user.username}`);
      }
    }
       
  4. Subscribe to incoming events

    @Injectable()
    export class MyMessageHandlerService {
      constructor(pusherService: NgxPuserServicee) {
        // Default channel
        pusherService
          .listen<ChatMessage>('ev:message')
          .subscribe((msg: ChatMessage) => {
            console.log('Message Received', msg.content);
          });
           
        // Custom channel
        pusherService
          .listen<any>('file-processed', 'custom-channel')
          .subscribe((msg: any) => console.log(msg));
      }
    }
       
    export interface ChatMessage {
      sender: string;
      content: string;
    }
       

PusherJs instance

You can use the pusherInstance() method to get the underlying pusher instance:

import { NgxPusherService } from "./ngx-pusher.service";

@Injectable()
export class MyMessageHandlerService {
  constructor(pusherService: NgxPusherServicer) {
    // Get pusher instance
    const pusher = pusherService.pusherInstance();
  }
}

Subscribe to multiple events

You can subscribe to more than one event on the same channel by passing in an array of event names.

pusherService
  .listen<ChatMessage>(['ev:message', 'ev:meeting-request'])
  .subscribe((msg: ChatMessage) => {
    console.log('Message Received', msg.content);
});

Contributing

Any contributions to make this project better are much appreciated. Please follow the following guidelines before getting your hands dirty.

  • Fork the repository
  • Run yarn
  • Make your changes, and don't forget to add unit tests.
  • Run lint
    npm run lint
  • Run test
    npm run test
  • Commit/Push any changes to your repository
  • Open a pull request

License

Distributed under the MIT License. See LICENSE for more information.

Acknowledgements