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

@adrawash1999/event-bus-lib

v2.1.0

Published

An event bus library for Angular that enables you to choose between pubsub-js, postMessage, and custom events.

Downloads

216

Readme

EventBusLib

EventBusLib is an event bus library for Angular designed to facilitate communication between components in multirepo micro frontend architectures. It allows you to choose between different event handling strategies: pubsub-js, postMessage, and custom events. This flexibility makes it ideal for applications composed of multiple micro frontends that need to communicate with each other seamlessly.

Table of Contents

Installation

To install the library, you can use npm:

npm install @adrawash1999/event-bus-lib

Usage

Getting Started

  1. Import the EventBus and EventBusFactory: Start by importing EventBus and EventBusFactory in your Angular component or service.

    import { EventBus, EventBusFactory } from '@adrawash1999/event-bus-lib';
  2. Inject EventBusFactory: In your constructor, inject the EventBusFactory to retrieve the desired event bus implementation.

    export class YourComponent {
      private eventBus: EventBus;
    
      constructor(private eventBusFactory: EventBusFactory) {
        // Here you can choose the type of event bus you want to use
        this.eventBus = this.eventBusFactory.getEventBus('postmessage');
      }
    }

Using Different EventBus Types

The library supports the following event bus implementations:

  • pubsub: Utilizes the pubsub-js library for event handling.
  • custom-event: Uses the built-in CustomEvent API for custom event dispatching.
  • postmessage: Leverages the postMessage API for inter-window communication, making it suitable for micro frontends on different domains.

To use a specific type, call getEventBus(type) and pass the desired type:

this.eventBus = this.eventBusFactory.getEventBus('pubsub'); // or 'custom-event' or 'postmessage'

Example: Communication Between Micro Frontends

Here’s a simple example showcasing how to use the EventBusLib to establish communication between micro frontends:

Micro Frontend 1:

// In Micro Frontend 1 Component
import { Component } from '@angular/core';
import { EventBus, EventBusFactory } from '@adrawash1999/event-bus-lib';

@Component({
  selector: 'app-micro-frontend1',
  templateUrl: './micro-frontend1.component.html',
})
export class MicroFrontend1Component {
  private eventBus: EventBus;

  constructor(private eventBusFactory: EventBusFactory) {
    this.eventBus = this.eventBusFactory.getEventBus('postmessage');
    this.subscribeToEvents();
  }

  private subscribeToEvents(): void {
    this.eventBus.subscribe('shared_event', (msg, data) => {
      console.log(`Received in MF1: ${msg}`, data);
    });
  }

  public triggerEvent(): void {
    this.eventBus.publish('shared_event', { info: 'Hello from Micro Frontend 1!' });
  }
}

Micro Frontend 2:

// In Micro Frontend 2 Component
import { Component } from '@angular/core';
import { EventBus, EventBusFactory } from '@adrawash1999/event-bus-lib';

@Component({
  selector: 'app-micro-frontend2',
  templateUrl: './micro-frontend2.component.html',
})
export class MicroFrontend2Component {
  private eventBus: EventBus;

  constructor(private eventBusFactory: EventBusFactory) {
    this.eventBus = this.eventBusFactory.getEventBus('postmessage');
    this.subscribeToEvents();
  }

  private subscribeToEvents(): void {
    this.eventBus.subscribe('shared_event', (msg, data) => {
      console.log(`Received in MF2: ${msg}`, data);
    });
  }
}

API Documentation

EventBus Interface

Methods

  • publish(eventName: string, data: any): void

    • Publishes an event with the specified name and associated data.
  • subscribe(eventName: string, callback: (msg: string, data: any) => void): void

    • Subscribes to an event and executes the callback when the event is published.
  • unsubscribe(eventName: string): void

    • Unsubscribes from the specified event.

Build

Run the following command to build the library:

ng build event-bus-lib

The build artifacts will be stored in the dist/ directory.

Publishing

After building your library with ng build, navigate to the dist folder and run:

cd dist/event-bus-lib
npm publish --access public

Running Unit Tests

To execute the unit tests via Karma, run:

ng test event-bus-lib

Contributing

Contributions are welcome! Please fork the repository and submit a pull request with your changes.

For any issues or feature requests, please use the issue tracker.

License

This project is licensed under the MIT License. See the LICENSE file for details.