Skip to content

Instantly share code, notes, and snippets.

@CaptainYarb
Created March 26, 2020 15:18
Show Gist options
  • Save CaptainYarb/9240e5126958b74eec60f556fb24e52c to your computer and use it in GitHub Desktop.
Save CaptainYarb/9240e5126958b74eec60f556fb24e52c to your computer and use it in GitHub Desktop.
nats timeout test
'use strict';
const nats = require('nats');
const config = require('./config/nats.json');
const natsClient = nats.connect(config.connection);
const killServer = () => {
setTimeout(() => {
natsClient.options.timeout = 1;
console.log('😈 Killing connection and breaking timeout', natsClient.options.timeout);
natsClient.stream.destroy();
setTimeout(() => {
natsClient.options.timeout = config.connection.timeout;
console.log('Setting timeout back original number', natsClient.options.timeout);
}, 3500);
}, 8000);
};
natsClient.on('error', (err) => {
console.log('ERROR', err);
});
natsClient.on('reconnecting', () => {
console.log('... RECONNECTING ...');
});
natsClient.on('reconnect', () => {
console.log('! RECONNECTED !');
killServer();
});
natsClient.on('connect', () => {
killServer();
natsClient.subscribe('timeout-test', (msg) => {
console.log('<< REC', msg.rand);
});
setInterval(() => {
natsClient.publish('timeout-test', {
test: true,
rand: Math.floor(Math.random() * 20)
}, (err) => {
if(err){
return console.log('>> SEND ERR', err);
}
console.log('>> SEND');
});
}, 1000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment