@adhityan/gc-nats
TypeScript icon, indicating that this package has built-in type declarations

3.0.2 • Public • Published

gc-nats

A TypeScript Node.js wrapper service for the NATS streaming server based on stan.js client.

Publishing messages

import "reflect-metadata";
import { JsonController, Get, Res } from 'routing-controllers';
import { Inject, Service } from 'typedi';
import { ResponseUtil } from '@app/utils';
import { NatsService } from '@buzzkey/gc-nats';
import { Logger } from '@adhityan/gc-logger';

@Service()
@JsonController('/v1')
export class PingController {
  @Inject()
  public natsService: NatsService;

  @Get('/ping')
  async ping(@Res() res: any) {
    // send test ping message to queue
    const channel: string = "ping";
    const message: any = {
      info: "testing ping"
    };

    const messageId: string = await this.natsService.publish(channel, message);
    Logger.debug(`published message: ${messageId}`);

    return ResponseUtil.ok({ message: 'Ping Successful' }, res);
  }
}

Subscribing to messages from a channel/subject

import { natsConfig } from '@app/config/nats.config';
import { Logger } from '@adhityan/gc-logger';
import { Worker } from '@adhityan/gc-nats';

const subject = "ping";

@Worker<T>(natsConfig)
export class PingWorker {
    static channel: string = subject;

    public async messageReceived (message: T) {
        Logger.debug('Ping successful. worker processing message: ', message);
    }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @adhityan/gc-nats

Weekly Downloads

4

Version

3.0.2

License

MIT

Unpacked Size

45.1 kB

Total Files

19

Last publish

Collaborators

  • adhityan
  • gc.suresh.saini
  • himanshu-gc