@plmtest/cosmos-common-nats-streaming
v1.0.3
Published
PLM Fleet Nats Streaming API for PLM Fleet
Readme
NATS Streaming
The NATS Streaming Library manages message publish/subscribe with NATS.
Prerequisite
- NodeJS
Dependencies
deepmergenode-nats-streaming@plmtest/es-api-logger
Installation
npm install --save @plmtest/cosmos-common-nats-streamingDocumentation
Run the message pipe with docker
$ docker run -p 4222:4222 -p 8222:8222 nats-streaming -cid portalPublish a message
const Nats = require('@plmtest/cosmos-common-nats-streaming')
const natsOptions = {
configuration: {
host: '127.0.0.1',
port: 4222,
clusterId: 'portal',
clientId: 'example-publisher'
},
logger: {
name: 'example-publisher',
level: 'info'
}
}
const stan = new Nats(natsOptions).connect(() => {
const msg = { foo: 'bar' }
const promise = stan.publish('example-subject', msg)
.then(result => console.log(`Published message with id: ${result}`))
.catch(err => console.log(`Publishing message failed: ${err.message}`))
promise.then(() => {
stan.close()
})
})Subscribe to messages
const Nats = require('@plmtest/cosmos-common-nats-streaming')
const natsOptions = {
configuration: {
host: '127.0.0.1',
port: 4222,
clusterId: 'portal',
clientId: 'example-subscriber'
},
logger: {
name: 'example-subscriber',
level: 'info'
}
}
const stan = new Nats(natsOptions).connect(() => {
stan.subscribe('example-subject', (msg) => {
console.log(msg)
})
})