# Close gate after one message has passed through

**URL:** https://discourse.nodered.org/t/close-gate-after-one-message-has-passed-through/56575
**Category:** General
**Created:** [13 January 2022 09:50 UTC](https://discourse.nodered.org/t/close-gate-after-one-message-has-passed-through/56575 "2022-01-13T09:50:35Z")
**Posts on this page:** 1
**Showing post:** 7

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [13 January 2022 15:11 UTC](https://discourse.nodered.org/t/close-gate-after-one-message-has-passed-through/56575/7 "2022-01-13T15:11:47Z")

</div>

The best solution I have found for this (thanks to @Colin) is

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/1/11e5dc6de8f68fb6dde609cf5dda75eec2927cea.png)

```auto
[{"id":"fbd3a65b056cdd0d","type":"inject","z":"523968221e696a28","name":"message in","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":170,"y":620,"wires":[["a2fe8de8ecc0501d"]]},{"id":"cc86232451944c83","type":"inject","z":"523968221e696a28","name":"open","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"open","payloadType":"str","x":350,"y":660,"wires":[["e8464d1608046ae7"]]},{"id":"a2fe8de8ecc0501d","type":"function","z":"523968221e696a28","name":"close after","func":"return [[msg,{payload:'close',topic:'control'}]]","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":620,"wires":[["e8464d1608046ae7"]]},{"id":"e8464d1608046ae7","type":"gate","z":"523968221e696a28","name":"","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","statusCmd":"status","suppressWarnings":false,"persist":false,"storeName":"memoryOnly","x":490,"y":620,"wires":[["69207b01cfe27d1f"]]},{"id":"69207b01cfe27d1f","type":"debug","z":"523968221e696a28","name":"message out","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":620,"wires":[]}]

```

The `function` node ("close after") On Message tab contains just the single line of code

```auto
return [[msg,{payload:'close',topic:'control'}]]

```

This should eliminate the possibility of a race condition. (As I understand how NR handles messages, it is not possible for any message to reach the gate in between the two sent by the `function` node.) For the record, and in case you are interested, aspects of this issue have been discussed [here](https://github.com/drmibell/node-red-contrib-simple-gate/issues/12) and [here](https://discourse.nodered.org/t/timing-question-avoiding-race-conditions/55616).

---

_[View the full topic](https://discourse.nodered.org/t/close-gate-after-one-message-has-passed-through/56575)._
