8000 GitHub - dalew75/nats-wrapper: Wrapper to make NATS functions available with configuration and connection happening in one place
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Wrapper to make NATS functions available with configuration and connection happening in one place

License

Notifications You must be signed in to change notification settings

dalew75/nats-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nats-wrapper

Wrapper to make NATS functions available with configuration and connection happening in one place

Install

mkdir ~/dev/tools
git clone git@github.com:dalew75/nats-wrapper.git ~/dev/tools
mkdir ~/dev/my-project
cd ~/dev/my-project
npm init -y 
npm install /dev/tools/nats-wrapper/

Config

In your .env file define NATS HOST and PORT(4222 default)

NATS_HOST=[HOST_OR_IP_FOR_NATS_SERVER]
NATS_PORT=[CUSTOM_PORT]

import

var nats = require('nats-wrapper');

subscribe

// Will add a greeting to the name/message sent
nats.subscribe('/add-greeting', async function (message, replyTo, subject) {
    console.log(`Got message: '${message}' on subject: '${subject}', replyTo: ${replyTo}`);
    nc.publish(replyTo,`Hello ${message}!);
});

publish (with specifying reply to subject)

const replyToSubject = '/receiving-greetings';

nats.publish('/add-greeting', 'John Doe', replyToSubject => {
    if (err) {
        console.error(err);
    }
    else {
        console.log(`Received message with greeting: ${msg}`);
        //const msgObj = JSON.parse(msg); // if expecting JSON, parse it and use it
    }
});

// Will add a greeting to the name/message sent
nats.subscribe(replyToSubject, async function (message, replyTo, subject) {
    console.log(`Received message with greeting: ${msg}`);
});

publish (with callback)

nats.publish('/add-greeting', 'John Doe', async (err, msg) => {
    if (err) {
        console.error(err);
    }
    else {
        console.log(`Received message with greeting: ${msg}`);
        //const msgObj = JSON.parse(msg); // if expecting JSON, parse it and use it
    }
});

About

Wrapper to make NATS functions available with configuration and connection happening in one place

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0