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

@vicgrk/messenger

v1.0.3

Published

Amqp lib to send and receive messages from different applications

Downloads

10

Readme

@vicgrk/messenger

Send message and response to event with ts decorators

Before start

This library use a rabbitmq instance to communicate. You can spin up a developpment message broker with docker using:

docker run -d --rm --name my-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management

In a production environment, we recommand using a replicated RabbitMQ cluster.

Installation

Warning, this package is not a DI manager and use other Dependency Injection manager to function properly. You can run this library with:

The author will try to add support for new depency injectors in the futures.

You can install the package with just

npm i --save @vicgrk/messenger

Documentation

You can start the amqp lib by initializing the Messenger singleton

TypeDI

Init your Messenger in the entrypoint of your app (index.ts) :

import Container, { Service } from "typedi";
import { Messenger } from "@vicgrk/messenger";

Messenger.init({
  rootDir: __dirname,
  name: "my-node-name",
  verbose: process.env.NODE_ENV !== "production",
  rabbit: {
    host: process.env.RABBITMQ_HOST,
    port: process.env.RABBITMQ_PORT,
    user: process.env.RABBITMQ_USER,
    password: process.env.RABBITMQ_PASSWORD,
  },
  di: Container,
});

TSED.io

For tsed, you can init your Messenger in your main configuration

import { AfterInit } from "@tsed/common";
import { Configuration, Inject, InjectorService } from "@tsed/di";
import { Messenger } from "@vicgrk/messenger";

@Module()
export class Server implements AfterInit {
  @Inject()
  private injector: InjectorService;

  $afterInit() {
    Messenger.init({
      rootDir: __dirname,
      name: "my-node-name",
      verbose: process.env.NODE_ENV !== "production",
      rabbit: {
        host: process.env.RABBITMQ_HOST,
        port: process.env.RABBITMQ_PORT,
        user: process.env.RABBITMQ_USER,
        password: process.env.RABBITMQ_PASSWORD,
      },
      di: this.injector,
    });
  }
}

Configuration

Library configuration object

| Name | Default | Description | | ------- | ---------------------- | ---------------------------------------------------------------------------------------- | | rabbit | see below the defaults | The rabbitmq credentials used to connect to the server | | di | undefined | The di service usued in this package. | | name | undefined | The service name of your application, usued to receive message from other services | | verbose | false | Logs every transactions and payloads | | rootDir | undefined | The root directory where you implement your @Amqp decorators (get folder and subfolders) |

RabbitMQ configuration object

| Name | Default | Description | | -------- | --------- | ----------------------------------------- | | host | localhost | The IP or hostname of the rabbitmq server | | port | 5672 | The port of the rabbitmq server | | user | guest | The user of the rabbitmq server | | password | guest | The password of the rabbitmq server |

How to use

To emit to rabbitmq

import { Messenger } from "@vicgrk/messenger";

@Service()
export class MyService {
  async getMemberInformation(memberId: string) {
    const member = await Messenger.invoke<{ user: string }>(
      "payment.member-informations",
      {
        memberId,
      }
    );
    // you should receive an object from the payment service running in a different application.
  }

  // If you just send data, without receiving anything, you can call the publish method. It's much smaller in compute requirements (avoid the round trip)
  sendUserUpdate(user: unknown) {
    Messenger.publish("users.update-user", user);
  }

  // If you want to, for example, refresh a list on all available consumers, you can you the broadcast method
  broadcastListenerService(user: unknown) {
    Messenger.broadcast("users.add-user-listener", user);
  }
}

Now, this is how to receive and response to events :

In your payment service, just use the @Amqp decorarator

// first, import your mesh service
import { Amqp, Message, Origin } from "@vicgrk/messenger";

@Service()
export class MyService {
  constructor(private db: Database) {}

  // Get the raw payload sent from `MyService`
  @Amqp("member-informations")
  async getMemberInformation(@Message() payload: { memberId: string }) {
    const member = await this.db.getUserFromId(payload);
    return member;
  }

  // Get a specific property from the sent message
  @Amqp("member-informations")
  async getMemberInformation(@Message("memberId") payload: string) {
    const member = await this.db.getUserFromId(payload);
    return member;
  }

  // You can also get the instance that send the request (this is the name parameter of the service that sent the request)
  @Amqp("member-informations")
  async getMemberInformation(
    @Message("memberId") payload: string,
    @Origin() origin: string
  ) {
    const member = await this.db.getUserFromId(payload);
    return member;
  }
}