Skip to content

Instantly share code, notes, and snippets.

@aricart
Last active November 4, 2018 18:19
Show Gist options
  • Save aricart/2f1c368560c9ffa3fe93847b47572792 to your computer and use it in GitHub Desktop.
Save aricart/2f1c368560c9ffa3fe93847b47572792 to your computer and use it in GitHub Desktop.
sample nkey authentication with node-nats
const NATS = require('nats');
const nkeys = require('ts-nkeys');
// private key
var priv = nkeys.fromSeed("SUAFY3TGVXJBM5A4CLH7OE4P4N25KHLMF6BQLPJKMV5K6MIZAVVAS4JXAQ");
var ah = {};
ah.sign = function(data) {
return priv.sign(data);
};
ah.id = priv.getPublicKey();
let nc = NATS.connect({url: "tls://nats-audit.synadia.io:4222", tls: true, authHandler: ah});
nc.on('error', function(err) {
console.log(err);
});
nc.on('connect', function(nc) {
console.log(nc.info);
console.log('connected');
});
nc.on('disconnect', function() {
console.log('disconnect');
});
nc.on('reconnecting', function() {
console.log('reconnecting');
});
nc.on('reconnect', function(nc) {
console.log('reconnect');
});
nc.on('close', function() {
console.log('close');
});
mkdir /tmp/test
cd /tmp/test
npm init
# answer prompts with default
# install nkey aware node-nats
npm install --save-dev nats@next
# install nkeys library
npm install --save-dev ts-nkeys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment