Event format
Each WebSocket message is a JSON object. Events are emitted every 500ms during solving (or faster for small instances).
json
// Stream event structure
{
"type": "progress",
"iteration": 4500,
"energy": -142.3,
"best_energy": -147.8,
"best_solution": [1, 0, 1, 1, 0, ...], // current best assignment
"temperature": 0.082,
"timestamp": "2026-04-19T12:03:11.482Z"
}
// Final event when done
{
"type": "completed",
"result": {
"objective": -147.8,
"solution": [1, 0, 1, 1, 0, ...],
"runtime_s": 8.4,
"gpu_seconds": 8.4
}
}Browser JavaScript
javascript
const jobId = 'job_abc123';
const apiKey = 'nrx_sk_...';
const ws = new WebSocket(
`wss://api.driftrail.com/v1/jobs/${jobId}/stream?token=${apiKey}`
);
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
if (msg.type === 'progress') {
console.log(`Iter ${msg.iteration}: best=${msg.best_energy.toFixed(2)}`);
updateLiveChart(msg.iteration, msg.best_energy); // your UI update
}
if (msg.type === 'completed') {
ws.close();
renderFinalRoute(msg.result.solution);
}
};
ws.onerror = (err) => console.error('Stream error', err);Python streaming
python
import nerox
client = nerox.Client()
job = client.optimize.tsp(distance_matrix=matrix, solver="gpu")
# Sync streaming — iterates events as they arrive
for event in job.stream():
print(f"Iter {event.iteration:6d} best={event.best_energy:.2f}")
if event.best_energy < my_target:
job.cancel()
break
result = job.resultAsync streaming
python
import asyncio
import nerox
async def stream_job():
async with nerox.AsyncClient() as client:
job = await client.optimize.tsp(distance_matrix=matrix, solver="gpu")
async for event in job.astream():
print(f"{event.iteration}: {event.best_energy:.2f}")
return await job.result()
result = asyncio.run(stream_job())Use cases for streaming
Live route visualization — animate tour improvement on a map as the solver runs
Early stopping — cancel as soon as your quality target is met, saving GPU-seconds
Progress bars — show solver convergence in your application UI
Logging — record energy-vs-iteration curves for benchmarking and debugging
