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

@morgan-stanley/message-broker

v1.0.0

Published

Framework agnostic messagebroker for decoupled communication.

Downloads

2

Readme

MessageBroker

MessageBroker provides framework agnostic, decoupled communication between publishers and subscribers. This library is fully type safe and works in both browsers and Node.js. MessageBroker is built ontop of RxJS providing access to observables and a comprehensive list of operators.

Installation

npm install @morgan-stanley/message-broker

TypeScript

Required Typescript version: >3.4

The library depends on TypeScript's support for decorators. Therefore you must enable experimentalDecorators and emitDecoratorMetadata.

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
    }
}

Polyfills

This library will work with modern browsers and JavaScript run-times without the need for polyfills, however if targeting older browsers you will need to provide a polyfill for the following types.

This library also makes use of the reflect-metadata API for performing runtime introspection. Most browsers will not support this therefore you must install this yourself.

npm install reflect-metadata

And you should import this module at the root of your application.

import "reflect-metadata";

Using the MessageBroker

An instance of the MessageBroker can be created using the messagebroker function. This will return a single instance of the MessageBroker.

Example


import { messagebroker } from "@morgan-stanley/message-broker";

const messagebroker = messagebroker();

Subscribing to a channel

Listening to messages requires a subscription to a channel. Once subscribed any messages published to that channel will be received. Subscribing to a channel is done by calling the get function. The get function is ideal if you are soley interested in receiving messages. MessageBroker leverages RxJS, the get function will return an Observable that can be subscribed to.

import { messagebroker } from "@morgan-stanley/message-broker";
import { Subscription } from "rxjs";
	
private subscription: Subscription;

public subscribeToMessages(): void {
    this.subscription  =	messagebroker()
                                .get('myChannelName')
                                .subscribe(
                                    message => {}, // Next handler
                                    () => {}, // Error handler
                                    () => {} // Complete handler
                                );
}

// Clean up.
this._subscription.unsubscribe();

Publishing to a channel

Publishing messages requires a channel, this can be created using the create function. If the channel already exists the existing channel will be returned. Once the channel is created a message can be published using the publish function. Passing a payload is optional but if you choose to do so a message type can also be passed for more granular filtering.


import { messagebroker } from "@morgan-stanley/message-broker";

public publishMessage(): void {

        messagebroker()
            .create('myChannelName')
            .publish(
                { data: 'myData'}, // Payload
                'ApplicationEvent' // Message type
            );
	}

Replay Messages

Replaying will allow new subscriptions to receive the latest "n" number of messages. Using the messagebroker config the user can configure how many messages they want to be cached when subscriptions are made. Configs can only be provided when the messagebroker channels are created.

import { messagebroker } from "@morgan-stanley/message-broker";

public publishToCachedChannel(): void {
    messagebroker()
        .create('myCachedChannel', {replayCacheSize: 2}) // All subscriptions will receive the two most recent messages.
        .publish(
            { data: 'myData' },
            'ApplicationEvent'
        );
}

It is important to note that creating a channel with the same name but with different configurations will throw an error.

Typing the MessageBroker

We recommend typing the messagebroker to avoid unexpected errors during runtime. Typing the messagebroker allows you to specify a contract between the channel name and the payload type that will flow across that channel. This enforces that the types specified on the channel contract must match when using the methods of the messagebroker. If they do not match you will receive compilation errors.

Example


/* Defined our channel contract. */
    export interface IMessageChannels {
        'hello': string;
        'goodbye': undefined;
        'operation1': { foo: string };
    }

    import { messagebroker } from "@morgan-stanley/message-broker";

    const typedMessageBroker = messagebroker<IMessageChannels>();

    //Publish
    typedMessageBroker.create("operation1").publish({foo: "test"}); // OK  
    typedMessageBroker.create("operation1").publish({foo: number}); // Error: foo cannot be set to number  
    typedMessageBroker.create("unknown").publish({foo: "test"}); // Error: invalid channel name

    //Subscribe
    typedMessageBroker.get("hello").subscribe(message => console.log(message.data)); // OK: data will be of type string
    typedMessageBroker.get("invalid").subscribe(message => console.log(message.data)); // Error: invalid channel name		 

RSVP

The rsvp methods allow developers to define a request/response model using the messagebroker. The rsvp (publish) overload is synchronous and will ask all responders to respond to the message payload being provided.

RSVP (Publish)


/* Defined our channel contract. */
    export interface IMessageChannels extends IRSVPConfig {
        nonRSVPChannel : string;
        rsvp: {
            myRSVPChannel: {
                payload: { data: string };
                response: string[];
            };
    };
    }

    import { messagebroker } from "@morgan-stanley/message-broker";

    const results = messagebroker<IMessageChannels>().rsvp('myRSVPChannel', { data: 'abcde'});		 

RSVP (Respond)


/* Defined our channel contract. */
    export interface IMessageChannels extends IRSVPConfig {
        nonRSVPChannel : string;
        rsvp: {
            myRSVPChannel: {
                payload: { data: string };
                response: string[];
            };
    };
    }

    import { messagebroker } from "@morgan-stanley/message-broker";

    const results = messagebroker<IMessageChannels>().rsvp('myRSVPChannel', payload => {
        // Perform some work on payload - split string .
        return [...payload]; // Payload and response type enforced by contract.
    });		 

RSVP (Respond) with manual disconnect


/* Defined our channel contract. */
    export interface IMessageChannels extends IRSVPConfig {
        nonRSVPChannel : string;
        rsvp: {
            myRSVPChannel: {
                payload: { data: string };
                response: string[];
            };
    };
    }

    import { messagebroker } from "@morgan-stanley/message-broker";

    const results = messagebroker<IMessageChannels>().rsvp('myRSVPChannel', payload => this.doWork(payload));		
    
    // Manually disconnect the responder to avoid handling further rsvp requests.
    responder.disconnect();

Dependency Injection (DI)

The MessageBroker class is decorated with @Injectable from @morgan-stanley/needle. This means that it can be constructed by different DI frameworks. For more information please refer to the documentation here

Development

Here are a list of commands to run if you are interested in developing or contributing to the project. For guidelines on how to contribute please click here.


npm install // Install all package dependencies.

npm run test // Run tests on the command line.

npm run watch-test // Run tests in watch mode.

npm run lint // Checks the code for lint errors.

npm run build // Run a simple build.

npm run watch-build // Run build in watch mode.

npm run build-release // Run a full build (Compile, Tests, Lint).