# FlowSync

FlowSync implements synchronous request handling for HTTP server nodes and OPC UA server nodes within Connectware. When activated, Connectware suspends response generation until a corresponding MQTT message is received, enabling synchronized processing across different protocols and systems. This enables a closed communication loop between machines and IT systems, confirming every step of the data journey – even when errors occur.

You can customize the behavior of FlowSync through configuration:

* Define custom timeout durations for request-response cycles
* Configure default timeout messages returned to clients
* Specify custom error messages for different error scenarios

FlowSync is part of the [Cybus:Node](https://docs.cybus.io/1-9-0/documentation/services/service-commissioning-files/structure-of-service-commissioning-files/resources/cybus-node) resource.

## Processing Flow

The following outlines the data flow from initial request to completion.

{% stepper %}
{% step %}
**Initial Request Reception**

When a request (HTTP or OPC UA) arrives at the server endpoint:

* The request remains open rather than returning an immediate response
* The system publishes an MQTT message on the `/treq` topic. The published data has the following structure:

{% code lineNumbers="true" %}

```json
{
  "id": "cb75f252-ebd8-4b33-b39d-b74953c6c99c",
  "timestamp": 1733219059372,
  "value": { ... }
}
```

{% endcode %}

* A unique identifier is generated and embedded in the message payload
  {% endstep %}

{% step %}
**Transaction Completion**

Upon receiving the MQTT response message, the system:

* Correlates it with the original request using the unique identifier
* Processes the response data on the `/tres` topic. The received data has the following structure:

{% code lineNumbers="true" %}

```json
{
  "id": "cb75f252-ebd8-4b33-b39d-b74953c6c99c",
  "timestamp": 1733219059372,
  "value": { ... }
}
```

{% endcode %}

* Completes the original protocol-specific request

The transaction remains active until either:

* A valid MQTT response is received
* The configured timeout period expires
  {% endstep %}
  {% endstepper %}

## Enabling FlowSync

FlowSync functionality is controlled through the node resource configuration in your service commissioning files.

* To implement synchronous processing, set the `operation` property of a [Cybus:Node](https://docs.cybus.io/1-9-0/documentation/services/service-commissioning-files/structure-of-service-commissioning-files/resources/cybus-node) resource to `transaction`:

{% code lineNumbers="true" %}

```yaml
node:
    operation: transaction
```

{% endcode %}

## FlowSync Example Files

The following sections provide practical examples of service commissioning files that demonstrate how to implement FlowSync with both HTTP and OPC UA connections. Each example includes a complete configuration along with explanations of key aspects to help you understand and adapt FlowSync for your specific needs.

* [Example 1 - Node with Transaction Mode (HTTP)](https://docs.cybus.io/1-9-0/documentation/services/flowsync/flowsync-example-1)
* [Example 2 - Node Responds (HTTP)](https://docs.cybus.io/1-9-0/documentation/services/flowsync/flowsync-example-2)
* [Example 3 - Node with Error (HTTP)](https://docs.cybus.io/1-9-0/documentation/services/flowsync/flowsync-example-3)
* [Example 4 - Node with Timeout Error Code and Error Message (HTTP)](https://docs.cybus.io/1-9-0/documentation/services/flowsync/flowsync-example-4)
* [Example 5 - Full Flow (HTTP)](https://docs.cybus.io/1-9-0/documentation/services/flowsync/flowsync-example-5)
* [Example 6 - Full Transactional Data Flow (OPC UA)](https://docs.cybus.io/1-9-0/documentation/services/flowsync/flowsync-example-6)
