Skip to content

The WebSocket API provides real-time bidirectional communication for receiving real-time data streams from USV.

ws://localhost:8081

Authenticate using JWT Token:

const token = 'your-jwt-token';
const ws = new WebSocket(`ws://localhost:8081?token=${token}`);

Send:

{
"action": "subscribe",
"device_id": "USV-2024-001",
"streams": ["telemetry", "sensors"]
}

Response:

{
"type": "subscription_ack",
"streams": ["telemetry", "sensors"]
}
{
"type": "telemetry",
"device_id": "USV-2024-001",
"timestamp": "2024-01-15T10:30:00Z",
"data": {
"latitude": 31.2304,
"longitude": 121.4737,
"speed": 5.2,
"heading": 90,
"battery": 78
}
}
{
"type": "sensor",
"device_id": "USV-2024-001",
"sensor_type": "water_quality",
"timestamp": "2024-01-15T10:30:00Z",
"data": {
"ph": 7.2,
"dissolved_oxygen": 8.5,
"temperature": 25.3
}
}
const ws = new WebSocket('ws://localhost:8081?token=your-token');
ws.onopen = () => {
console.log('Connected to WebSocket');
// Subscribe to data stream
ws.send(JSON.stringify({
action: 'subscribe',
device_id: 'USV-2024-001',
streams: ['telemetry', 'sensors']
}));
};
ws.onmessage = (event) => {
const message = JSON.parse(event.data);
switch(message.type) {
case 'telemetry':
updateMap(message.data);
break;
case 'sensor':
updateSensorDisplay(message.data);
break;
}
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
};
ws.onclose = () => {
console.log('WebSocket closed');
};
import asyncio
import websockets
import json
async def stream_data():
uri = "ws://localhost:8081?token=your-token"
async with websockets.connect(uri) as websocket:
# Subscribe to data stream
await websocket.send(json.dumps({
"action": "subscribe",
"device_id": "USV-2024-001",
"streams": ["telemetry", "sensors"]
}))
# Receive data
async for message in websocket:
data = json.loads(message)
print(f"Received: {data}")
asyncio.run(stream_data())

See more at API Overview.

Table of Contents

Pages