question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

nats.js should reconnect when one of nats servers are restarting

See original GitHub issue

Run nats cluster:

Server A:

nats-server -p 4222 -cluster nats://localhost:4248 -routes nats://localhost:4248 -m 8222

Server B:

nats-server -p 5222 -cluster nats://localhost:5248 -routes nats://localhost:4248

Run nats-streaming-server in cluster mode:

nats-streaming-server -ns nats://0.0.0.0:4222 --cluster_id nats-streaming --clustered --cluster_bootstrap --cluster_node_id nats-streaming-1 --cluster_raft_logging --debug --dir store-b --store file

Go to examples and start it using yarn start: when it start and running try to kill cluster A

Actual results:

2020-08-25T00:47:14 ERROR NatsError: Could not connect to server: Error: connect ECONNREFUSED 127.0.0.1:4222
    at Socket.<anonymous> (/Users/andrii/workspace/src/github.com/astronomer/houston-api/node_modules/nats/lib/nats.js:801:26)
    at Socket.emit (events.js:315:20)
    at Socket.EventEmitter.emit (domain.js:483:12)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)

Expected results:

fallback to one of available nodes e.g. cluster B

P.S. golang client for nats works as expected:

go run main.go -s nats://127.0.0.1:4222 -t sub
Listening on [sub]
2020/08/25 02:17:22 Disconnected due to:EOF, will attempt reconnects for 10m
2020/08/25 02:17:22 Reconnected [nats://192.168.1.232:5222]
2020/08/25 02:17:37 Disconnected due to:EOF, will attempt reconnects for 10m
2020/08/25 02:17:37 Reconnected [nats://127.0.0.1:4222]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
aricartcommented, Aug 25, 2020

Similar exercise to yours:

node notifications.js
connect to localhost:4222
connect to localhost:4222
disconnect
disconnect
reconnect to 192.168.86.52:5222
reconnect to 192.168.86.52:5222
disconnect
disconnect
reconnect to localhost:4222
reconnect to localhost:4222
disconnect
disconnect
reconnect to 192.168.86.52:5222
reconnect to 192.168.86.52:5222
disconnect
disconnect
reconnect to localhost:4222
reconnect to localhost:4222
const {connect} = require("nats");

const nc = connect({waitOnFirstConnect: true, reconnectTimeWait: 2000});

nc.on('error', (err) => {
  console.log(err);
});

nc.on('connect', (nc) => {
  console.log(`connect to ${nc.currentServer.url.host}`);
});

nc.on('disconnect', () => {
  console.log('disconnect');
});

nc.on('reconnect', (nc) => {
  console.log(`reconnect to ${nc.currentServer.url.host}`);
});

nc.on('close', function () {
  console.log('close');
});
0reactions
aricartcommented, Aug 26, 2020

Excellent!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reconnect issues while server restarts #138 - nats-io/stan.js
When you get a notification on close , you have to re-initialize the STAN connection, there's no reconnection after a close event.
Read more >
Set the Number of Reconnect Attempts - NATS Docs
After a successful reconnect to a server, the client will reset that server's failed reconnect attempt count. If a server was removed from...
Read more >
NATS JetStream - ThinkMicroservices.com
If the consumer restarts, the server will continue at the last stream position. Pull subscriptions require a durable consumer. Its also ...
Read more >
Nodejs Express API with NATS? - Stack Overflow
I have a nats-server running in my terminal. This is my test code: var express = require("express"); var app = ...
Read more >
node-nats-streaming | Yarn - Package Manager
Stan.js - Node.js client for NATS Streaming. NATS Streaming Server is an extremely performant, lightweight reliable streaming platform powered by NATS.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found