Powered By GitBook
Listening for Reconnect Events
Because reconnect is primarily under the covers, many libraries provide an event listener you can use to be notified of reconnect events. This event can be especially important for applications sending a lot of messages.
Go
Java
JavaScript
Python
Ruby
TypeScript
C
1
// Connection event handlers are invoked asynchronously
2
// and the state of the connection may have changed when
3
// the callback is invoked.
4
nc, err := nats.Connect("demo.nats.io",
5
nats.DisconnectErrHandler(func(nc *nats.Conn, err error) {
6
// handle disconnect error event
7
}),
8
nats.ReconnectHandler(func(nc *nats.Conn) {
9
// handle reconnect event
10
}))
11
if err != nil {
12
log.Fatal(err)
13
}
14
defer nc.Close()
15
16
// Do something with the connection
Copied!
1
Options options = new Options.Builder().
2
server("nats://demo.nats.io:4222").
3
connectionListener((conn, type) -> {
4
if (type == Events.RECONNECTED) {
5
// handle reconnected
6
} else if (type == Events.DISCONNECTED) {
7
// handle disconnected, wait for reconnect
8
}
9
}).
10
build();
11
Connection nc = Nats.connect(options);
12
13
// Do something with the connection
14
15
nc.close();
Copied!
1
let nc = NATS.connect({
2
maxReconnectAttempts: 10,
3
servers: ["nats://demo.nats.io:4222"]
4
});
5
6
nc.on('reconnect', (c) => {
7
console.log('reconnected');
8
});
Copied!
1
nc = NATS()
2
3
async def disconnected_cb():
4
print("Got disconnected!")
5
6
async def reconnected_cb():
7
# See who we are connected to on reconnect.
8
print("Got reconnected to {url}".format(url=nc.connected_url.netloc))
9
10
await nc.connect(
11
servers=["nats://demo.nats.io:4222"],
12
reconnect_time_wait=10,
13
reconnected_cb=reconnected_cb,
14
disconnected_cb=disconnected_cb,
15
)
16
17
# Do something with the connection.
Copied!
1
require 'nats/client'
2
3
NATS.start(servers: ["nats://127.0.0.1:1222", "nats://127.0.0.1:1223", "nats://127.0.0.1:1224"]) do |nc|
4
# Do something with the connection
5
nc.on_reconnect do
6
puts "Got reconnected to #{nc.connected_server}"
7
end
8
9
nc.on_disconnect do |reason|
10
puts "Got disconnected! #{reason}"
11
end
12
end
Copied!
1
// will throw an exception if connection fails
2
let nc = await connect({
3
maxReconnectAttempts: 10,
4
servers: ["nats://demo.nats.io:4222"]
5
});
6
// first argument is the connection (same as nc in this case)
7
// second argument is the url of the server where the client
8
// connected
9
nc.on('reconnect', (conn, server) => {
10
console.log('reconnected to', server);
11
});
12
nc.close();
Copied!
1
static void
2
disconnectedCB(natsConnection *conn, void *closure)
3
{
4
// Handle disconnect error event
5
}
6
7
static void
8
reconnectedCB(natsConnection *conn, void *closure)
9
{
10
// Handle reconnect event
11
}
12
13
(...)
14
15
natsConnection *conn = NULL;
16
natsOptions *opts = NULL;
17
natsStatus s = NATS_OK;
18
19
s = natsOptions_Create(&opts);
20
21
// Connection event handlers are invoked asynchronously
22
// and the state of the connection may have changed when
23
// the callback is invoked.
24
if (s == NATS_OK)
25
s = natsOptions_SetDisconnectedCB(opts, disconnectedCB, NULL);
26
if (s == NATS_OK)
27
s = natsOptions_SetReconnectedCB(opts, reconnectedCB, NULL);
28
29
if (s == NATS_OK)
30
s = natsConnection_Connect(&conn, opts);
31
32
(...)
33
34
// Destroy objects that were created
35
natsConnection_Destroy(conn);
36
natsOptions_Destroy(opts);
Copied!
Last modified 10mo ago
Export as PDF
Copy link