// Be notified if a new server joins the cluster.
// Print all the known servers and the only the ones that were discovered.
nc, err := nats.Connect("demo.nats.io",
nats.DiscoveredServersHandler(func(nc *nats.Conn) {
log.Printf("Known servers: %v\n", nc.Servers())
log.Printf("Discovered servers: %v\n", nc.DiscoveredServers())
}))
if err != nil {
log.Fatal(err)
}
defer nc.Close()
// Do something with the connection
class ServersAddedListener implements ConnectionListener {
public void connectionEvent(Connection nc, Events event) {
if (event == Events.DISCOVERED_SERVERS) {
for (String server : nc.getServers()) {
System.out.println("Known server: "+server);
}
}
}
}
public class ListenForNewServers {
public static void main(String[] args) {
try {
Options options = new Options.Builder().
server("nats://demo.nats.io:4222").
connectionListener(new ServersAddedListener()). // Set the listener
build();
Connection nc = Nats.connect(options);
// Do something with the connection
nc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
let nc = NATS.connect("nats://demo.nats.io:4222");
nc.on('serversDiscovered', (urls) => {
t.log('serversDiscovered', urls);
});
# Asyncio NATS client does not support discovered servers handler right now
# The Ruby NATS client does not support discovered servers handler right now
nc.on('serversChanged', (ce) => {
t.log('servers changed\n', 'added: ',ce.added, 'removed:', ce.removed);
});