# Node client: Sniffing does not wait for return routing requests potentially via discovery nodes

**URL:** <https://discuss.elastic.co/t/node-client-sniffing-does-not-wait-for-return-routing-requests-potentially-via-discovery-nodes/356119>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [March 25, 2024, 5:00pm UTC](https://discuss.elastic.co/t/node-client-sniffing-does-not-wait-for-return-routing-requests-potentially-via-discovery-nodes/356119 "2024-03-25T17:00:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 25, 2024, 5:00pm UTC](https://discuss.elastic.co/t/node-client-sniffing-does-not-wait-for-return-routing-requests-potentially-via-discovery-nodes/356119/1 "2024-03-25T17:00:04Z")

</div>

Hey,

I've been using the typescript client and wondered about the sniffing behaviour using the following code:

```auto
    const client = new Client({
        nodes: ["http://localhost:9200"],
        sniffOnStart: true,
        sniffOnConnectionFault: true,
    });

    client.on('sniff', (err, result) => {
        console.log("### SNIFF", err, result)
    })

    client.on('response', (err, result) => {
        console.log("### REQUEST", err, result)
    })

    const health = await client.cluster.health();
    console.log("### HEALTH FINISHED")

    // wait a little more until sniffing is logged...
   await new Promise(f => setTimeout(f, 2000));

```

The health will be finished, before the sniffing is done. I suppose that this is expected, but wanted to ensure.

If the master nodes are used as initial nodes to connect to a cluster, then for a few possibly hundred milliseconds (until the sniffing is done and the connection pool is updated) those will be hit with a search request.

Is there any possibility to wait for the sniffing to finish and only then be ready or is everyone good with putting a little load on the initial discovery nodes that are very likely be master nodes?

Thanks!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 26, 2024, 9:44am UTC](https://discuss.elastic.co/t/node-client-sniffing-does-not-wait-for-return-routing-requests-potentially-via-discovery-nodes/356119/2 "2024-03-26T09:44:37Z")

</div>

Answering myself a little here. One could alleviate the issue by waiting for the first sniff to finish before becoming ready - as I don't know if that sniff will always finish (or I maybe attached to it too late as there is no lifecycle, I also added a dumb two second timeout for this)

```js
    const client = new Client({
        nodes: ["http://localhost:9200"],
        sniffOnStart: true,
        sniffOnConnectionFault: true,
    });

    const sniffingPromise = new Promise<void>((resolve, reject) => {
        client.once('sniff', (err, result) => {
            resolve();
        })
    })
    const twoSecondWait = new Promise<void>(f => setTimeout(f, 2000));
    await Promise.race([sniffingPromise, twoSecondWait]);

```

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [March 29, 2024, 12:29am UTC](https://discuss.elastic.co/t/node-client-sniffing-does-not-wait-for-return-routing-requests-potentially-via-discovery-nodes/356119/3 "2024-03-29T00:29:17Z")

</div>

> [@spinscale](#):
>
> If the master nodes are used as initial nodes to connect to a cluster

I think it'd be best not to do this: ideally if you have dedicated master nodes then they're not even visible to clients. Instead I'd suggest starting with (a reasonable guess at) the nodes you actually want to use for client traffic. If it's tricky to make such a guess in your environment then consider some other layer of abstraction (e.g. round-robin DNS or a reverse proxy load balancer) to support you through the sniffing phase.

Having your application wait until sniffing is complete also seems ok, but also consider whether it's worth worrying about. You shouldn't be starting up clients very often, so a little misdirected load during the initial sniffing phase shouldn't be more than a tiny fraction of the total.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 26, 2024, 12:29am UTC](https://discuss.elastic.co/t/node-client-sniffing-does-not-wait-for-return-routing-requests-potentially-via-discovery-nodes/356119/4 "2024-04-26T00:29:27Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
