# Determine the number of wires connecting to an output port

**URL:** https://discourse.nodered.org/t/determine-the-number-of-wires-connecting-to-an-output-port/35783
**Category:** Developing Nodes
**Created:** [12 November 2020 05:22 UTC](https://discourse.nodered.org/t/determine-the-number-of-wires-connecting-to-an-output-port/35783 "2020-11-12T05:22:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Redhatter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/redhatter/32/23748_2.png) [@Redhatter](https://discourse.nodered.org/u/Redhatter)
#### Post date: [12 November 2020 05:22 UTC](https://discourse.nodered.org/t/determine-the-number-of-wires-connecting-to-an-output-port/35783/1 "2020-11-12T05:22:53Z")

</div>

Hi all,

So, a silly question, I have a node with one input port, and two output ports. The first output port is used to return the results of operations, whilst the second is used to emit debugging information.

I'd like to only emit debug messages if and only if there is something _actually_ connected to that port.

Internally, I'll probably be leaning heavily on NodeJS's `EventEmitter` class, and for that, it has a [`listenerCount`](https://nodejs.org/docs/latest-v12.x/api/events.html#events_emitter_listenercount_eventname) method which tells me how many "listeners" have been connected to a given signal.

It'd be really nice if I could, in my node, say:

```javascript
node.on("input", (msg, send, done) => {
    let myobj = new MyObject();
    if (node.getNumberOfConnectionsToOutputPort(1) > 0) {
        myobj.on('debug', (debugmsg) => {
            /* emit a debug message */
            send([[], [debugmsg]]);
        });
    }
    return myobj.doSomethingWith(msg, send).then(done).catch(done);
});

```

What I do not know, is what `getNumberOfConnectionsToOutputPort` is actually called, or even if it exists.

Is there such a function? If so, where can I read up about it?

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [12 November 2020 08:07 UTC](https://discourse.nodered.org/t/determine-the-number-of-wires-connecting-to-an-output-port/35783/2 "2020-11-12T08:07:22Z")

</div>

The `node.wires` property is an array of arrays that corresponds to the connections to the outputs , so should be what you want.

---

<div class="post-metadata">

### Author: ![Redhatter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/redhatter/32/23748_2.png) [@Redhatter](https://discourse.nodered.org/u/Redhatter)
#### Post date: [15 November 2020 22:27 UTC](https://discourse.nodered.org/t/determine-the-number-of-wires-connecting-to-an-output-port/35783/3 "2020-11-15T22:27:14Z")

</div>

Many thanks… I just tried adding the following code to my node's `input` handler.

```javascript
node.log('Wire connections: \n' + JSON.stringify(node.wires, null, 4));

```

Got this in the logs…

```auto
15 Nov 22:23:46 - [info] [widesky-cet-read-history:c2555ef3.abae88] Wire connections: 
[
    [
        "1fbb3ad7.355b85",
        "57f594c3.3a847c"
    ],
    [
        "21c7503c.5d1f9"
    ]
]

```

Looks like that has what I'm after. Many thanks. I spent quite some time looking around for the API specs on the `node` object but kept going around in circles. This helps a lot.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [29 November 2020 22:27 UTC](https://discourse.nodered.org/t/determine-the-number-of-wires-connecting-to-an-output-port/35783/4 "2020-11-29T22:27:17Z")

</div>

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