# Using node-red-contrib-semaphore

**URL:** <https://discourse.nodered.org/t/using-node-red-contrib-semaphore/4398>\
**Category:** General\
**Created:** [29 October 2018 22:01 UTC](https://discourse.nodered.org/t/using-node-red-contrib-semaphore/4398 "2018-10-29T22:01:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [29 October 2018 22:01 UTC](https://discourse.nodered.org/t/using-node-red-contrib-semaphore/4398/1 "2018-10-29T22:01:43Z")

</div>

Trying not to go OT from this thread....

> [@Ring Doorbell not verry stable](https://discourse.nodered.org/t/ring-doorbell-not-verry-stable/4394/4):
>
> An alternative to your solution of using the queue, which might be simpler I think, would be to use node-red-contrib-semaphore.

The node readme is very basic, and has no example flow.  
@Colin I'm struggling to understand how this node would work in a flow environment?

Paul

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 October 2018 22:15 UTC](https://discourse.nodered.org/t/using-node-red-contrib-semaphore/4398/2 "2018-10-29T22:15:31Z")

</div>

I will post a flow in the morning, is late here.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 October 2018 07:44 UTC](https://discourse.nodered.org/t/using-node-red-contrib-semaphore/4398/3 "2018-10-30T07:44:57Z")

</div>

Here is an example of using node-red-contrib-semaphore. A semaphore is like a token used on a single track railway to ensure only one train is on the line at a time. The semaphore take node allows the semaphore to be taken or acquired, once that happens then messages will be queued until the semaphore is released in the semaphore leave node. The effect is that the next message is not allowed to pass until the protected task completes.  
One thing you have to make sure is that every exit from the task does send a message (and only one) to the semaphore leave node, otherwise the flow will grind to a halt. It may therefore be a good idea to include a catch node as I have done in case the task generates an error and does not send any output.

 ![Screenshot-2018-10-30-07%3A45%3A12](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/736241dbc66d5a1956299a402e869a3884356bd7.png)

```auto
[{"id":"52da7849.5a04d","type":"semaphore-take","z":"6dc690a3.1abc88","config":"f1723a93.b1fee","name":"","x":208.5,"y":360,"wires":[["b742bf19.8905a"]]},{"id":"c992389.f77ad48","type":"inject","z":"6dc690a3.1abc88","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90.5,"y":295,"wires":[["c1900da7.2f2828"]]},{"id":"c1900da7.2f2828","type":"function","z":"6dc690a3.1abc88","name":"Generate multiple messages","func":"msg1 = {payload: \"message 1\"}\nmsg2 = {payload: \"message 2\"}\nmsg3 = {payload: \"message 3\"}\nmsg4 = {payload: \"message 4\"}\nreturn [[msg1,msg2,msg3,msg4]]","outputs":1,"noerr":0,"x":290.5,"y":296,"wires":[["52da7849.5a04d"]]},{"id":"f04b86a1.1245d8","type":"debug","z":"6dc690a3.1abc88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":689,"y":434,"wires":[]},{"id":"b742bf19.8905a","type":"delay","z":"6dc690a3.1abc88","name":"Task that should only run message at a time","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":489.5,"y":359,"wires":[["8f1b395a.fda968","f04b86a1.1245d8"]]},{"id":"8f1b395a.fda968","type":"semaphore-leave","z":"6dc690a3.1abc88","config":"f1723a93.b1fee","name":"","x":456.5,"y":472,"wires":[[]]},{"id":"c85afe13.e94a4","type":"catch","z":"6dc690a3.1abc88","name":"","scope":["b742bf19.8905a"],"x":351.5,"y":402,"wires":[["8f1b395a.fda968"]]},{"id":"b1fb2135.8e2728","type":"comment","z":"6dc690a3.1abc88","name":"node-red-contrib-semaphore example","info":"","x":458.5,"y":257,"wires":[]},{"id":"f1723a93.b1fee","type":"semaphore-config","z":"","name":"My semaphore","capacity":"1"}]

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [30 October 2018 14:44 UTC](https://discourse.nodered.org/t/using-node-red-contrib-semaphore/4398/4 "2018-10-30T14:44:19Z")

</div>

Thanks Colin. I was almost there!!!  
And yes it does seem easier to use than `node-red-contrib-simple-message-queue` so I'll probably change to using it.

Paul
