# WebSocket Client

> Send message to WebSocket Server.

Source: https://docs.sommatic.ai/en/nodes/io.websocket/

- Slug: `io.websocket`
- Family: Input and output
- Version: 1.0.0
- Status: Available
- Execution: async

Send message to WebSocket Server.

## Configuration

- `connection_id` (string, required): Credential for the WebSocket endpoint. Accepts credentials of type `api-key`, `basic-auth`, or `object` (custom handshake headers).
- `message` (string, required): Message to send
- `waitForResponse` (boolean, optional): Default: `false`.

## Output ports

- `out` (default): Message sent or response received
- `error`: Connection or transmission error

## Input contract

- `connection_id` (string, required)
- `message` (string, required)
- `waitForResponse` (boolean, optional)

## Output contract

- `result` (string, optional): Ack or Response

## In expressions

What a later node can read from this node's output with `$("WebSocket Client").item.json.<path>` (write your node's label instead of `WebSocket Client`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("WebSocket Client").item.json.result` (string) = `"{\"type\":\"ack\",\"id\":\"msg_881\"}"`: Ack or Response


## How it works

Opens a WebSocket connection, sends one message and either closes at once or waits for the first message back.

1. Resolves the config. `connection_id`, `message` and `waitForResponse` (default false) fall back to the same keys of the incoming payload; `connection_id` and `message` are required.
2. Resolves the credential: the URL is `config.url`; when the secret has `token`, the handshake sends `Authorization: Bearer <token>`.
3. Opens the connection and sends `message` as soon as it is open.
4. Without `waitForResponse`, closes and returns `{ result: "sent" }`. With it, waits for the first incoming message, closes and returns it as text in `result`.

## In a workflow

### Node

```json
{
  "node_id": "node-push-event",
  "label": "Push event",
  "operator_slug": "io.websocket",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "message": "{{ JSON.stringify($json.event) }}",
    "waitForResponse": true
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "result": "{\"type\":\"ack\",\"id\":\"msg_881\"}"
  },
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

The message was sent (`result` is `"sent"`) or, with `waitForResponse`, the first reply arrived (`result` is its text).

```json
{
  "result": "{\"type\":\"ack\",\"id\":\"msg_881\"}"
}
```

### `error`

A required field is missing, the credential cannot be resolved, or the connection failed.

```json
{
  "error": "Unexpected server response: 401"
}
```

## Behaviour in the code

- `connection_id`: The URL comes from the credential config `url`; only `token` is read from the secret. `api-key` and `basic-auth` secrets add no handshake header. A value starting with `ws://` or `wss://` is used as the URL itself, without authentication.
- `message`: Must be a string. To send JSON, build the string first, for example `{{ JSON.stringify($json.event) }}`.
- `waitForResponse`: Waits without a time limit. If the server never sends a message, or closes without one, the node does not finish.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Unexpected server response: <status>` | `error` | The server refused the upgrade (for example 401 or 404). Check the URL and the token in the credential. |
| `message is required` | `error` | The message is empty in the config and in the incoming payload (`connection_id is required` is the equivalent for the credential). |
| `Failed to resolve connection <connection_id>: <detail>` | `error` | The credential does not exist or this workflow cannot use it. |

## Notes and limits

- Only the first incoming message is returned; later messages are discarded. A binary reply is converted to text.
- A new connection is opened and closed on every run. No retries.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "io.websocket",
  "name": "WebSocket Client",
  "category": "io",
  "version": "1.0.0",
  "description": "Send message to WebSocket Server.",
  "capabilities": [
    "websocket",
    "realtime",
    "messaging"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "WebSocket Credential",
        "description": "Credential for the WebSocket endpoint. Accepts credentials of type `api-key`, `basic-auth`, or `object` (custom handshake headers).",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "api-key",
            "basic-auth",
            "object"
          ],
          "types": [
            "websocket"
          ]
        }
      },
      "message": {
        "type": "string",
        "description": "Message to send"
      },
      "waitForResponse": {
        "type": "boolean",
        "default": false
      }
    },
    "required": [
      "connection_id",
      "message"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "message": {
        "type": "string"
      },
      "waitForResponse": {
        "type": "boolean"
      }
    },
    "required": [
      "connection_id",
      "message"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "result": {
        "type": "string",
        "description": "Ack or Response"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Sent/Response",
      "description": "Message sent or response received",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Connection or transmission error",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard-io",
    "realtime"
  ],
  "tags": [
    "realtime",
    "messaging"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "WebSocket Credential",
      "description": "Credential for the WebSocket endpoint. Accepts credentials of type `api-key`, `basic-auth`, or `object` (custom handshake headers).",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "api-key",
          "basic-auth",
          "object"
        ],
        "types": [
          "websocket"
        ]
      }
    },
    "message": {
      "type": "string",
      "description": "Message to send"
    },
    "waitForResponse": {
      "type": "boolean",
      "default": false
    }
  },
  "required": [
    "connection_id",
    "message"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "message": {
      "type": "string"
    },
    "waitForResponse": {
      "type": "boolean"
    }
  },
  "required": [
    "connection_id",
    "message"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "result": {
      "type": "string",
      "description": "Ack or Response"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Sent/Response",
    "description": "Message sent or response received",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Connection or transmission error",
    "is_default": false,
    "supports_condition": false
  }
]
```
