:::info Sample code
Working examples on GitHub — [call_wait](https://github.com/obniz/obniz/tree/master/examples/nodejs/plugin/call_wait), [cloud_transaction](https://github.com/obniz/obniz/tree/master/examples/nodejs/plugin/cloud_transaction), [command](https://github.com/obniz/obniz/tree/master/examples/nodejs/plugin/command).
:::

This document describes how to exchange data between a Lua plugin running on the obniz OS and the obniz JavaScript SDK.

## Overview

You can facilitate communication between a Lua plugin operating on the device OS and the JavaScript SDK. This allows for bidirectional data transfer and efficient handling of large data sets.


## obniz.plugin.send()

Sends a binary data sequence from the **obniz.js** side to the **Lua plugin**.

### Javascript Example

```javascript
// Send string data
obniz.plugin.send("obniz.js send data get device?");

// Send byte array
obniz.plugin.send([0x00, 0x01, 0x02]);

```

### Lua Plugin Side

On the Lua side, use the `on_command()` function to receive the data.

```lua
function on_command(command)
  -- 'command' contains the data sent from JS
  os.log(command)
end

```


## obniz.plugin.onreceive

This event handler receives a binary data sequence sent from the **Lua plugin** to the **obniz.js** side.

### Javascript Example

```javascript
obniz.plugin.onreceive = (data) => {
  console.log(data);
};

```

### Lua Plugin Side

Use `cloud.pluginSend()` to send data to the JavaScript SDK.

```lua
cloud.pluginSend("123");

```

## obniz.plugin.onFrameStart / onFrameEnd

These handlers are triggered when frame information is sent from the plugin side.

These functions are particularly useful for segmenting and managing large data transfers.

* `onFrameStart`: Receives the `frame_id` and the total `length` of the data.
* `onreceive`: For large payloads, the data will be received in multiple chunks through this handler.
* `onFrameEnd`: Called once all data defined in the frame has been completely received.

### Javascript Example

```javascript
// Handle the start of a frame
obniz.plugin.onFrameStart = (frame_id, length) => {
  console.log(`Frame start: ID=${frame_id}, Total Length=${length}`);
};

// Handle the end of a frame
obniz.plugin.onFrameEnd = () => {
  console.log("Frame reception completed");
};

// Receive chunks of data
obniz.plugin.onreceive = (data) => {
  console.log(`Received chunk: ${Buffer.from(data).toString()}`);
};

```

### Lua Plugin Side

To use framing in a Lua plugin, use the following commands:

```lua
local frame_id = 1
local total_length = 9

cloud.pluginSendFrameStart(frame_id, total_length); -- Start framing 

cloud.pluginSend("123");
cloud.pluginSend("456");
cloud.pluginSend("789");

cloud.pluginSendFrameEnd(); -- End framing 

```

## Request and Reply (Transactions)

> Available for OS7.1.0 and later

To send a request and wait for its reply, use `obniz.plugin.callWait()` (obniz.js → Lua) and `cloud.transactionWait()` / `obniz.plugin.onCloudTransaction` (Lua → obniz.js). See [Transaction](./transaction) for details.
