Real-time message delivery via /socket.io
Real-time message delivery via WebSocket
Endpoint: /socket.io Transport: Socket.IO (WebSocket with fallback)
How to establish and authenticate a WebSocket connection.
Events your client emits to the server.
Authenticate WebSocket connection
{
"token": "string (bearer token from login)"
}
Keepalive ping
No payload
Events your client listens for from the server.
Connection established, awaiting authentication
{
"message": "Connected. Please authenticate.",
"status": "ok"
}
Authentication successful
{
"status": "ok",
"user_id": "string"
}
Game message (action results, notifications)
{
"action_id": "string (optional)",
"details": "object",
"drone_id": "string (optional)",
"message_id": "string",
"message_type": "string (action_completed, action_failed, etc.)",
"subject": "string",
"timestamp": "integer (cycle count)"
}
Error message
{
"code": "string",
"message": "string"
}
Keepalive response
{
"status": "ok"
}
Types of messages delivered via the message event.
Complete examples for connecting and receiving messages.
# pip install python-socketio requests
import socketio
import requests
# Get auth token
resp = requests.post('http://localhost:5000/api/v1/auth/login',
json={'username': 'player1', 'password': 'secret'})
token = resp.json()['token']
# Connect to WebSocket
sio = socketio.Client()
@sio.on('connected')
def on_connected(data):
print('Connected, authenticating...')
sio.emit('authenticate', {'token': token})
@sio.on('authenticated')
def on_authenticated(data):
print(f"Authenticated as {data['user_id']}")
@sio.on('message')
def on_message(data):
print(f"[{data['message_type']}] {data['subject']}")
print(f" Details: {data['details']}")
@sio.on('error')
def on_error(data):
print(f"Error: {data['message']}")
sio.connect('http://localhost:5000')
sio.wait() # Block and listen for messages
// npm install socket.io-client
import { io } from 'socket.io-client';
// Get auth token first
const loginResp = await fetch('http://localhost:5000/api/v1/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: 'player1', password: 'secret' })
});
const { token } = await loginResp.json();
// Connect to WebSocket
const socket = io('http://localhost:5000');
socket.on('connected', (data) => {
console.log('Connected, authenticating...');
socket.emit('authenticate', { token });
});
socket.on('authenticated', (data) => {
console.log(`Authenticated as ${data.user_id}`);
});
socket.on('message', (data) => {
console.log(`[${data.message_type}] ${data.subject}`);
console.log('Details:', data.details);
});
socket.on('error', (data) => {
console.error('Error:', data.message);
});