# MQTT CommandResponse Action

This provides a way to publish MQTT Message based on event. Before adding MQTT CommandResponse Action, user has to define a [**MQTT Connection**](https://docs.thingsup.io/connections/mqtt-connection). User need to provide following parameters.

#### PublishTopic

* &#x20;User need to provide MQTT Topic on which data will be published.

#### SubscribeTopic

User need to provide MQTT Topic on which data will be received.

#### QoS

MQTT QoS value. Default is 1.

#### Function

* Javascript code to build mqtt message.
* "tx\_topic","rx\_topic","message" and "payload" objects are available in this code, function can be used to set message body and topic from payload before sending to MQTT.
* Setting tx\_topic and rx\_topic here overrides already defined PublishTopic and SubscribeTopic respectively.

```
//Example 1

message = payload;
tx_topic = "somenewtopic"; 
rx_topic = "someothertopic";
```

#### ResponseFunction

* Javascript code to validate received message.
* "tx\_topic","rx\_topic","rx\_message" and "tx\_message" objects are available in this code, keeping rx\_message non null will result in success response of action and assigning rx\_message to null will result in action failure.
* This function will execute for each message till success or timeout.

```
// Example 1

if(rx_message.id != tx_message.id ){
rx_message = null; 
}
```

#### Timeout

Maximum time expected in milliseconds for MQTT Response. Default value is 2000. Range is 1000 to 5000.

#### Connection ID

User need to provide a Connection ID of MQTT connection.

![Add MQTT CommandResponse Action](https://365492316-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7aTljSjWsfbXZ_OP_9%2Fuploads%2FdSPV6Z6eRZvX5bja83FI%2FMqttComResAction_1.png?alt=media\&token=03e9581e-2808-4bcc-9b53-9b2b713f0372)

Please note the following points:

{% hint style="info" %}

* "payload" object is supplied from Action Trigger like Rules, Action Command etc.
  {% endhint %}
