> For the complete documentation index, see [llms.txt](https://developers.newbluefx.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.newbluefx.com/captivate-api/advanced-api-reference/tcp-udp-and-http-connections.md).

# TCP and HTTP Connections

Our primary API operates over a websocket connection with various JavaScript wrapper libraries. However, websocket communication is not universally supported and can be cumbersome to implement from outside the context of a browser. Therefore, we provide two additional communication methods:

* TCP socket (persistent communication)
* HTTP GET/POST endpoints (two-way communication using chunked responses)

Each of these methods gives you new ways to access the `ServiceHandler.scheduler` object, and each of these endpoints handles data communication in roughly the same way (although the method of connecting to them is different).

Before reading further, make sure you are familiar with the [Controller API](https://developers.newbluefx.com/captivate-api/javascript-api-reference.md) because this API depends upon that.

> **Note:** Over HTTP and TCP, every command needs Full API Access, except the notification commands (`subscribe`, `unSubscribe` and `getSubscriptions`). See [API Access Levels](https://developers.newbluefx.com/getting-started/api-access-levels.md).

### MDNS Discovery (Bonjour) <a href="#mdns-discovery-bonjour" id="mdns-discovery-bonjour"></a>

Captivate announces its API on the local network with MDNS (Bonjour), using the service type `_newblue._tcp.local`.

Captivate announces three services. `HOSTNAME` is the name of the Captivate computer, without `.local`:

* `Captivate-WS-API--HOSTNAME._newblue._tcp.local`: WebSocket API service
* `Captivate-HTTP-API--HOSTNAME._newblue._tcp.local`: Advanced API over HTTP
* `Captivate-TCP-API--HOSTNAME._newblue._tcp.local`: Advanced API over TCP

A `PTR` query for `_newblue._tcp.local` returns these services:

* The `SRV` record gives the port for that service.
* The address records give the IP addresses of the Captivate computer.
* The `TXT` record holds these values:
  * `proto`: `ws`, `http` or `tcp`
  * `class`: `Captivate`
  * `id`: an identifier for the Captivate computer
  * `ips`: a comma-separated list of the computer's IPv4 addresses
  * `port`: the port for that service

### Simple Variable Update method <a href="#simple-variable-update-method" id="simple-variable-update-method"></a>

Because the majority of external APIs really only need to update variables, we have implemented a simplified endpoint for doing just that.

> **Note:** This endpoint is part of the HTTP API, so it needs Full API Access.

Send a POST request with `Content-type: application/json` to `/api/action/ACTION/TARGET/TARGET_NAME` with the following:

* `ACTION` needs to be one of the [`scheduleAction`](https://developers.newbluefx.com/api-examples/sample-controllers-tour/01-schedule-action.md) actions such as `render`, `update`, `still`, `animateIn`, `animateOut`, etc.
* `TARGET` can be either `input` or `title`.
* When targeting an input, `TARGET_NAME` should be the full name of the data controller connected to the title layer.
* When targeting a title, `TARGET_NAME` may be either the textual name of the title as it shows up in the project list, or the internal title id for the title.
* The request body should be a simple mapping of variable names to values encoded as JSON.

**Example:**

```sh
curl -H 'Content-type: application/json' \
     --data '{"Home Score": 13}' \
     'http://localhost:9022/api/action/update/input/Scoreboard%20OCR%3A%20Basketball'
```

### Using the Advanced API over HTTP <a href="#using-the-advanced-api-over-http" id="using-the-advanced-api-over-http"></a>

The HTTP endpoint is exposed at port `9022` at path `/api`. This port must not be in use when Captivate launches or the HTTP endpoint will not be able to start.

The HTTP endpoint responds to GET requests at `/api` with a brief help text similar to the TCP welcome message.

To send an API command, send a POST request to `/api` with `Content-type: application/json` and a JSON-encoded string as the request body.

To subscribe to Captivate’s notification stream, send a GET request to `/api/subscribe`. The server will keep the connection open and reply with a chunked HTTP response. Each notification from Captivate will be served as a JSON-encoded string chunk.

> Note: We also support a number of extra methods over HTTP. You can see documentation for them [here](https://developers.newbluefx.com/captivate-api/advanced-api-reference/additional-http-endpoints.md).

### Using the Advanced API over TCP <a href="#using-the-advanced-api-over-tcp" id="using-the-advanced-api-over-tcp"></a>

The TCP endpoint is exposed at port `9025`. This port must not be in use when Captivate launches or the TCP endpoint will not be able to start.

When a client connects, a welcome message will be sent from Captivate. The welcome message will be something like this:

```
CAPTIVATE HELLO
=======================================
We support a subset of Titler's API over direct TCP socket communication using JSON.

All messages are terminated with CRLF. '\r\n'


To call "scheduleCommand", send a JSON-encoded string that will decode with the following fields:

  - command: The name of the command to run
  - parameters: The parameters as an object
  - variables: The variables as an object

  The results of the command will be output as a JSON-encoded string.

To call "scheduleAction", send a JSON-encoded string that will decode with the following fields:

  - action: The name of the action to schedule
  - input: The name of the input / data controller to target
  - title: The id of the title / graphic layer to target
  - variables: The variables as an object

  The results of the command will be output as a JSON-encoded string.

Additionally, as long as you are connected, we will send you Titler's notification stream.
-------------------------------------------------------------------------------------------
BEGIN DATA
```

The welcome message ends with:

* The text `BEGIN DATA`
* *Two* sets of CRLF. (`\r\n\r\n`)

After the welcome message, all messages sent to or from this socket will be JSON-encoded strings, terminated with *one* set of CRLF. (`\r\n`)

As long as this client is connected to the TCP endpoint, all notification messages and signals will be sent to the client.

> Note: If a client connects and immediately sends a message, the server will *not* send the welcome message and will also *not* subscribe the client to notification messages and signals. If you connected with an immediate message and want to subscribe to notification messages send the command `subscribe` with empty parameters and variables.

**Example for Mac:**

```sh
MSG=$'{"command": "subscribe"}\r\n'
( echo $MSG; sleep 10; ) | nc localhost 9025
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://developers.newbluefx.com/captivate-api/advanced-api-reference/tcp-udp-and-http-connections.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
