A light weight package for instantiating a NATS service and functionality for connecting, subscribing and publishing nats topics.
You must have your NATS server running. The default host is localhost:4222. You can provide your own host and port number during natsConnection initiation
npm install nats-serviceconst { natsService, natsConnection } = require('nats-service');
const NATS_SERVER = 'localhost' // default localhost, if not provided
const NATS_PORT = 4222 // default 4222, if not provided
natsConnection(NATS_SERVER, NATS_PORT).then(()=>{
console.log('NATS connected...')
}).catch(error=>{
console.error(error)
})
// instantiate nats service
const service = await natsService();
// register a topic with callback
// registered callback with be called once the message arrives for the subscribed topic
service.subscribe('product.created', (payload)=>{
console.log('Event received with payload')
console.log(payload)
});
// publish a nats topic with a payload.
// the payload should always be a json
service.publish('product.created',
{id:123, name:'A new product'}
);