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

nestjs-nats-transporter

v0.0.8

Published

[![License](https://img.shields.io/badge/Licence-Apache%202.0-blue.svg)](./LICENSE) [![JSDoc](https://img.shields.io/badge/JSDoc-reference-blue)](https://nats-io.github.io/nats.js/) ![example workflow](https://github.com/nats-io/nats.js/actions/workflow

Downloads

356

Readme

NATS Transporter - The NATS client for NestJS

License JSDoc example workflow Coverage Status

[!IMPORTANT]

This project reorganizes the NATS Base Client library (originally part of nats.deno), into multiple modules, and on-boards the supported transports (Deno, Node/Bun, and WebSocket).

Welcome to the new NATS.js repository! Beginning with the v3 release of the JavaScript clients, the NATS.js repository reorganizes the NATS JavaScript client libraries into a formal mono-repo.

Overview

NATS is a simple, secure and high performance open source messaging system for cloud native applications, IoT messaging, and microservices architectures. The NATS server is written in the Go programming language, but client libraries to interact with the server are available for dozens of major programming languages. NATS supports both At Most Once and At Least Once delivery. It can run anywhere, from large servers and cloud instances, through edge gateways and even Internet of Things devices.

Installation

To start building microservice with nats transporter, first install required packages:

$ npm i --save nestjs-nats-transporter nats

Getting started

To instantiate a microservice, use the createMicroservice() method of the NestFactory class:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './modules/app/app.module';
import { NatsTransporter } from 'nestjs-nats-transporter';

async function bootstrap() {
  const app = await NestFactory.createMicroservice(AppModule, {
    strategy: new NatsTransporter({
      servers: ['nats://0.0.0.0:4222'],
      user: 'nats_user',
      pass: 'nats_password',
    }),
  });
  await app.listen();
}
bootstrap();

Patterns

Microservices recognize both messages and events by patterns. A pattern is a plain value, for example, a literal object or a string. Patterns are automatically serialized and sent over the network along with the data portion of a message. In this way, message senders and consumers can coordinate which requests are consumed by which handlers.

Client Proxy

import { Module } from '@nestjs/common';
import { ClientsModule } from '@nestjs/microservices';
import { NatsClientProxy } from 'nestjs-nats-transporter';
import { ConnectionOptions } from 'nats';

@Module({
  imports: [
    ClientsModule.register([
      {
        name: 'NATS_CLIENT',
        customClass: NatsClientProxy,
        options: {
          servers: ['nats://0.0.0.0:4222'],
          user: 'nats_user',
          pass: 'nats_password',
        },
      },
    ]),
  ],
  controllers: [ExampleController],
  providers: [ExampleService],
})
export class ExampleModule {}

Injection

import { Inject, Injectable } from '@nestjs/common';
import { NatsClientProxy } from 'nestjs-nats-transporter';

@Injectable()
export class ExampleService {
  
  @Inject('NATS_CLIENT')
  private client: NatsClientProxy
}

Sending messages

RxJs

@Injectable()
export class ExampleService {

  getHelloWorld(): Observable<number> {
    const payload = [1, 2, 3];
    return this.client.send<number>('subject', payload); 
  }
}

Promise

@Injectable()
export class ExampleService {

  async getHelloWorld(): Promise<string> {
    const payload = [1, 2, 3];
    const result = await lastValueFrom(
      this.client.send<string>('subject', payload)
    );
    console.log(result) //should be "Hello World!"
    return result;
  }
}

Publishing events