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

rmqbusjs

v1.0.32

Published

A package to make rpc calls and publish async events via rabbitmq

Downloads

140

Readme

RMQBusJS

This is a helper package for providing quick access to some commenly used event based communications interfaces in a microservices architeecture like RPC and Publish & Subscribe. This package also has the boilerplate code to auto register responders and consumers. Responders are the function which are bound to RPC call , whereas Consumers are functions which listen to published events.

Setting up your apigateway

import  RMQBroker  from  "rmqbusjs";

new  RMQBroker()
.init(rabbitmqConf)
.then(() => {
    // Connected to Rabbitmq Server
	// Do your other setup like DB etc
});

The rabbitmqConf parameter required by init() function is a JSON object which is as follows.

"rabbitmq": {
     "url": "amqp://user:passwd@localhost:5672",
     "app": "apigateway",
     "globalExchangeName" : "myapp"
}

The url property is the url of your rabbitmq server ( not the managment url) and the app property provides a uniqe name to your service which is later used to identify the service and create queue names accordingly. The globalExchangeName is used to create a global exchange which is responsible for broadcasting messages to queues in case if you register a global conusmer.

Note - The RMQBuJs in this case is only required to provide functions to perfrom RPC calls or publish calls . And thus in your controller code you can use it like this .

Setting up your Service

Create 3 folders with names responders consumers and globalconsumers which will have your functions . Each function will be in it's own file e.g ping.tsand getPerson.ts

Responder.ts

export interface Responder {

    handleTopic: string;
    /**
     * The execute method called by topic : handleTopic, and executed with req param.
     * Res(response) object will 
     * Res(null) method fail
     */
    executeWithResult(req: any): Promise<any>;
}

ping.ts

import { Responder } from "../interfaces/Responder";
export class ping implements Responder {
    handleTopic: string = "ping";
    executeWithResult(req: any): Promise < any > {
        return new Promise < any > ((res, rej) => {
            let name = req.data;
            res("Hello " + name);
        });
    }

}

Consumer.ts

export interface Consumer {

    eventTopic: string;
    /**
     * The execute method called by topic : handleTopic, and executed with req param.
     * Res(response) object will 
     * Res(null) method fail
     */
    handleEvent(req: any): Promise<any>;
}

pingconsumer.ts

import {Consumer} from "../interfaces/Consumer"
export class pingconsumer implements Consumer {
    eventTopic: string = "pingconsumer";
    handleEvent(req: any): Promise<any> {
        return new Promise<any>((res, rej) => {
            let name = req.data;
            res("Hello " + name);
        });
    }
}

Main.ts

import  RMQBroker  from  "rmqbusjs";

new  RMQBroker()
.init(rabbitmqConf)
.then(() => {
	// Connected to Rabbitmq Server
	// Responders and Consumers have been registered
	// Do your other setup like DB etc
});

The above code will register all the above functions with respective topics mentioned in the function code as in the examples given above. Post that whenever an event is published or and rpc call is made the function code will be invoked.

Note - The global consumer function can be same as a consumer function and thus implement the same consumer interface.