# Message passing and interop with MachineLogic

**URL:** <https://forum.vention.io/t/message-passing-and-interop-with-machinelogic/451>\
**Category:** MachineLogic\
**Created:** [February 7, 2024, 12:17am UTC](https://forum.vention.io/t/message-passing-and-interop-with-machinelogic/451 "2024-02-07T00:17:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxw](https://yyz2.discourse-cdn.com/flex012/user_avatar/forum.vention.io/maxw/32/23_2.png) [@maxw](https://forum.vention.io/u/maxw)\
**Post date:** [February 7, 2024, 12:17am UTC](https://forum.vention.io/t/message-passing-and-interop-with-machinelogic/451/1 "2024-02-07T00:17:12Z")

</div>

Message-passing is integral to multi-agent or distributed systems. The [IO automaton](https://en.wikipedia.org/wiki/Input/output_automaton) of distributed systems, for example, constitutes a pretty decent way of modelling the interface of a machine (such as a conveyor).

MachineLogic was built upon the node.js ecosystem, which provides for a multitude of options in terms of communications protocols, transport layers and interoperability. In its current state, MachineLogic only supports MQTT, HTTP, and (more recently) Ethernet/IP. There is a multitude of communication protocols out there - if there are some you would eagerly want to see on the Vention platform, do let us know (ideally with some context)!

The rest of this article will demonstrate various flavors of message-passing currently supported in MachineLogic.

# 1. MQTT

Sending MQTT messages is as simple as using the `Output` instruction with the `Generate Event` option. The `Message` can be formatted dynamically by a [lambda function](https://forum.vention.io/t/on-the-hidden-power-and-orderly-nature-of-lambda-functions-in-machinelogic/393) (e.g. passing in variables evolving over time). In the example here, we just inline a hard-coded json object.

![image](https://canada1.discourse-cdn.com/flex012/uploads/vention2/original/1X/4a39230df5144929c172da3f4eb6f7974de9bb15.png)

Note that the [MachineMotion controller](https://vention.io/machinemotion-v2) comes with a MQTT broker. It’s possible for multiple agents to communicate via that broker.

Receiving messages is just as simple. A `WaitFor` instruction (illustrated below) is required, usually in the context of a dedicated [parallel event handler](https://forum.vention.io/t/from-simple-imperative-programs-to-event-and-data-driven-machinelogic-idioms/427#h-1-parallel-event-handlers-1) _(n.b. alternatively, a target state in a [FSM instruction](https://forum.vention.io/t/from-simple-imperative-programs-to-event-and-data-driven-machinelogic-idioms/427#h-3-finite-state-machines-3) may also be triggered by a received MQTT message)_. In the example below, the `Unpack Variable Name` establishes a sequence-local variable that will automatically be assigned the payload of the received message. You can then parse this message and extract the fields of your choice, using a lambda.

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/vention2/original/1X/d0c054f009f6f82c4ecce24ecc561288bdc5e3ea.png)

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/vention2/original/1X/9b168b0ea68d2e774a5093494e1244b62a515b56.png)

Note that the pub-sub model of MQTT provides for great flexibility, as it lets you [receive messages asynchronously](https://forum.vention.io/t/concurrency-in-no-code-machinelogic/368) from any other activity.

# 2. HTTP

Communicating with a HTTP REST server represents a simple way of extending MachineLogic, or of integrating external components. For example, you might want to delegate persistence of some values measured during the operation in MachineLogic, to an external system (\*). Another use case could be to obtain an entire [mission definition](https://forum.vention.io/t/shifting-gaze-from-machine-to-process-from-variety-to-volume/429) from a webapp _(e.g. a spatial arrangement of objects to pick or place somewhere)_, to be translated by your MachineLogic program into an actual physical process.

In any case, the pattern is similar to the one seen earlier with MQTT _(though arguably simpler since it appears in a single pseudo-synchronous request-response form; in the example below, we did not use a lambda to parse `RETURNED_PAYLOAD` - as we had just above in the MQTT case - but this would be a typical pattern)_.

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/vention2/original/1X/3f559e092de77eec791244aba6def3911f8f7591.png)

> (\*) in which case you would `POST` a `Pack Message` that you may have formatted using a lambda function accepting some of your variables
