Home Explore Blog CI



nushell

3rd chunk of `contributor-book/plugin_protocol_reference.md`
16bff4842a65e39a503b2a245d958055883642c99af0f4eb0000000100000fa2
This feature advertises support for local socket communication, instead of stdio.

Example:

```json
{
  "name": "LocalSocket"
}
```

When local socket communication is advertised to an engine supporting the feature, the engine will cease stdio communication and launch the plugin again with the `--local-socket` command line argument. The second argument is either a path to a Unix domain socket on Linux, Android, macOS, and other Unix-like operating systems, or the name of a named pipe (without the `\\.\pipe\` prefix) on Windows.

In either case, during startup, the plugin is expected to establish two separate connections to the socket, in this order:

1. The input stream connection, used to send messages from the engine to the plugin
2. The output stream connection, used to send messages from the plugin to the engine

The connections are separate in order to facilitate ownership of the streams by separate threads. After these connections are both established, the engine will remove the socket, and will not accept further connections.

If local socket communication fails to initialize, the engine will abort, stop the plugin, and start it again with the stdio mode, even if the plugin supports local sockets. Whether local socket mode initialized successfully, and therefore the plugin is allowed to use stdio, can be observed when
[`EngineInterface::is_using_stdio()`](https://docs.rs/nu-plugin/latest/nu_plugin/struct.EngineInterface.html#method.is_using_stdio) returns `false` for Rust plugins.

## Input messages

These are messages sent from the engine to the plugin. [`Hello`](#hello) and [`Stream messages`](#stream-messages) are also included.

### `Call`

The body of this message is a 2-tuple (array): (`id`, `call`). The engine sends unique IDs for each plugin call it makes. The ID is needed to send the [`CallResponse`](#callresponse).

#### `Metadata` plugin call

Ask the plugin to send metadata about itself. Takes no arguments. Returns [`Metadata`](#metadata-plugin-call-response) or [`Error`](#error-plugin-call-response)

Example:

```json
{
  "Call": [0, "Metadata"]
}
```

#### `Signature` plugin call

Ask the plugin to send its command signatures. Takes no arguments. Returns [`Signature`](#signature-plugin-call-response) or [`Error`](#error-plugin-call-response)

Example:

```json
{
  "Call": [0, "Signature"]
}
```

#### `Run` plugin call

Tell the plugin to run a command. The argument is the following map:

| Field     | Type                                        | Description                                           |
| --------- | ------------------------------------------- | ----------------------------------------------------- |
| **name**  | string                                      | The name of the command to run                        |
| **call**  | [`EvaluatedCall`](#evaluatedcall)           | Information about the invocation, including arguments |
| **input** | [`PipelineDataHeader`](#pipelinedataheader) | Pipeline input to the command                         |

<a name="evaluatedcall"></a>

`EvaluatedCall` is a map:

| Field          | Type                                              | Description                                             |
| -------------- | ------------------------------------------------- | ------------------------------------------------------- |
| **head**       | [`Span`](#span)                                   | The position of the beginning of the command execution. |
| **positional** | [`Value`](#value) array                           | Positional arguments.                                   |
| **named**      | 2-tuple (string, [`Value`](#value) or null) array | Named arguments, such as switches.                      |

Named arguments are always sent by their long name, never their short name.

Returns [`PipelineData`](#pipelinedata-plugin-call-response) or [`Error`](#error-plugin-call-response).

Example:

```json
{
  "Call": [
    0,
    {
      "Run": {
        "name": "inc",

Title: Local Socket Communication Details and Input Messages
Summary
This section details the LocalSocket communication process, including how the engine manages failed initialization and how to detect the communication mode in Rust plugins. It then describes the input messages sent from the engine to the plugin, specifically the 'Call' message, which encapsulates different types of plugin calls: 'Metadata', 'Signature', and 'Run'. Each call type is explained with its arguments, return types, and example JSON structures. The structure of 'EvaluatedCall' used in the 'Run' call is also detailed.