Skip to content

webtech-coding/nats-service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nats Service

version

A light weight package for instantiating a NATS service and functionality for connecting, subscribing and publishing nats topics.

nats server

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

📦 Installation

npm install nats-service

🚀 Usage

const { 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'}
);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors