@adhityan/gc-nats
v3.0.2
Published
Typescript Node.js client for NATS streaming server
Downloads
114
Readme
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);
}
}