# Sending message asynchronously? How to send payload once an event occurs?

**URL:** https://discourse.nodered.org/t/sending-message-asynchronously-how-to-send-payload-once-an-event-occurs/38599
**Category:** General
**Created:** [5 January 2021 16:01 UTC](https://discourse.nodered.org/t/sending-message-asynchronously-how-to-send-payload-once-an-event-occurs/38599 "2021-01-05T16:01:43Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![adamelli](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/adamelli/32/31793_2.png) [@adamelli](https://discourse.nodered.org/u/adamelli)
#### Post date: [5 January 2021 16:01 UTC](https://discourse.nodered.org/t/sending-message-asynchronously-how-to-send-payload-once-an-event-occurs/38599/1 "2021-01-05T16:01:43Z")

</div>

Using the Dashboard, a user selects a sequence of commands (say for robot movement). Once the user is satisfied with their selected sequence, I want that string to be published. Only once the user is done though.

So once the user pushes the "start" button on the Dashboard, then the sequence of commands is sent to the microcontroller. I think the `node.send(msg)` is used somehow: [Making flows asynchronous by default : Node-RED](https://nodered.org/blog/2019/08/16/going-async) and [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously)

Yes? No? Any suggetions on how?

```auto
var commands = flow.get('commands') || [] // variable

msg.topic = 'collect'

switch(msg.payload)
{
    case 'moveForward':
    case 'moveBackward':
    case 'moveRight':
    case 'moveLeft':
        commands.push(msg.payload) // concatenates payload
        break
    
    case 'clear':
        commands = []
        break
    
    case 'run':
        msg.topic = 'start'
        break
}
flow.set('commands',commands)

// if(msg.topic == 'start' || commands == 'clear')
// {
    msg.payload = commands
// }
return msg

```

This program is thanks to hotNipi:

> [@User programmed commands](https://discourse.nodered.org/t/user-programmed-commands/35632):
>
> Is there a way to allow users to select a timeline of commands for a device to do? For example, say the application is a wheeled robot. The user wants it to go forward for one yard, then move left for two yards, forward for two yards, whatever. Is there a way to implement a GUI that allows the user to self-program commands? I am thinking something like a drag and drop boxes (similar to how Trello boards work). Although, just allowing a user to parse "film strips" together--somehow--and see the …

---

<div class="post-metadata">

### Author: ![adamelli](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/adamelli/32/31793_2.png) [@adamelli](https://discourse.nodered.org/u/adamelli)
#### Post date: [5 January 2021 16:34 UTC](https://discourse.nodered.org/t/sending-message-asynchronously-how-to-send-payload-once-an-event-occurs/38599/2 "2021-01-05T16:34:26Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/d/2d062df4aaedd62a44363829abe2638dcf43bc4f.png)

Switch node:  
msg.topic  
== [string] start  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/4/24b010f2ac117eb46c4ea7536e7a500557b7fe4c.png)

---

<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: [19 January 2021 16:34 UTC](https://discourse.nodered.org/t/sending-message-asynchronously-how-to-send-payload-once-an-event-occurs/38599/3 "2021-01-19T16:34:26Z")

</div>

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