Listening for Reconnect Events

    Go

    1. server("nats://demo.nats.io:4222").
    2. connectionListener((conn, type) -> {
    3. if (type == Events.RECONNECTED) {
    4. // handle reconnected
    5. } else if (type == Events.DISCONNECTED) {
    6. // handle disconnected, wait for reconnect
    7. }
    8. }).
    9. build();
    10. Connection nc = Nats.connect(options);
    11. // Do something with the connection

    JavaScript

    1. nc = NATS()
    2. async def disconnected_cb():
    3. print("Got disconnected!")
    4. async def reconnected_cb():
    5. # See who we are connected to on reconnect.
    6. print("Got reconnected to {url}".format(url=nc.connected_url.netloc))
    7. await nc.connect(
    8. servers=["nats://demo.nats.io:4222"],
    9. reconnect_time_wait=10,
    10. reconnected_cb=reconnected_cb,
    11. # Do something with the connection.

    Ruby

    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. nc.close();

    C