Files
lume/docs/content/docs/computer-sdk/computer-server/WebSocket-API.mdx
T
2025-12-24 14:19:24 -05:00

97 lines
1.9 KiB
Plaintext

---
title: WebSocket API Reference
description: Reference for the /ws WebSocket endpoint of the Computer Server.
---
# WebSocket API Reference
The Computer Server exposes a WebSocket endpoint for real-time command execution and streaming results.
- `ws://localhost:8000/ws`
- `wss://your-container.containers.cloud.trycua.com:8443/ws` (cloud)
### Authentication (Cloud Only)
For cloud containers, you must authenticate immediately after connecting:
```json
{
"command": "authenticate",
"params": {
"container_name": "your-container",
"api_key": "your-api-key"
}
}
```
If authentication fails, the connection is closed.
### Command Format
Send JSON messages:
```json
{
"command": "<command_name>",
"params": { ... }
}
```
### Example (Python)
```python
import websockets
import asyncio
import json
async def main():
uri = "ws://localhost:8000/ws"
async with websockets.connect(uri) as ws:
await ws.send(json.dumps({"command": "version", "params": {}}))
response = await ws.recv()
print(response)
asyncio.run(main())
```
### Example (Cloud)
```python
import websockets
import asyncio
import json
async def main():
uri = "wss://your-container.containers.cloud.trycua.com:8443/ws"
async with websockets.connect(uri) as ws:
await ws.send(json.dumps({
"command": "authenticate",
"params": {
"container_name": "your-container",
"api_key": "your-api-key"
}
}))
auth_response = await ws.recv()
print(auth_response)
await ws.send(json.dumps({"command": "version", "params": {}}))
response = await ws.recv()
print(response)
asyncio.run(main())
```
### Response Format
Each response is a JSON object:
```json
{
"success": true,
...
}
```
### Supported Commands
See [Commands Reference](./Commands) for the full list of commands and parameters.