Skip to content

Real-time progress with SSE

Receive the progress of file translation jobs (/jobs) in real time through SSE (Server-Sent Events). When SSE is not available, the connection returns 404; poll the job status instead.

The browser’s EventSource cannot send custom headers, so credentials take two steps:

  1. Your server exchanges the API key for a ticket (valid for 30 seconds, usable once)
  2. Connect to SSE with the ticket (this can happen in the browser; the ticket is not a long-lived credential)
POST /api/v1/sse/ticket/{job_id} → {"ticket": "..."}
GET /api/v1/sse/jobs/{job_id}?ticket=...

Keep the API key on the server: the server gets the ticket and hands it to the browser.

// Server (Node.js 18+): exchange the key for a ticket
async function createSseTicket(jobId) {
const response = await fetch(
`https://omnidoc.top/api/v1/sse/ticket/${jobId}`,
{ method: 'POST', headers: { 'X-API-Key': process.env.OMNIDOC_API_KEY } }
);
if (!response.ok) throw new Error(`ticket failed: ${response.status}`);
return (await response.json()).ticket;
}
// Browser: connect to SSE with the ticket from your server
const source = new EventSource(
`https://omnidoc.top/api/v1/sse/jobs/${jobId}?ticket=${encodeURIComponent(ticket)}`
);
// The events are named, so use addEventListener (onmessage does not receive them)
source.addEventListener('job_update', (event) => {
const data = JSON.parse(event.data);
console.log(`status: ${data.status}, stage: ${data.stage}, progress: ${data.stage_progress}%`);
});
source.addEventListener('job_done', (event) => {
const data = JSON.parse(event.data);
console.log(`finished: ${data.status}`);
source.close();
});
EventDescription
job_updateStatus or stage changed (a snapshot of the current state is sent right after connecting)
job_doneThe job ended (completed or failed); you can close the connection
timeoutThe stream reached its maximum duration; reconnect
errorServer-side problem; get a new ticket and reconnect later

The server’s stage_message is in Chinese:

event: job_update
data: {"status":"processing","stage":"translating","stage_progress":45,"stage_message":"正在翻译..."}
event: job_done
data: {"status":"completed","stage":"completed","stage_progress":100,"stage_message":""}

When the job completes, get the result list with GET /api/v1/artifacts?job_id=....

Terminal window
pip install requests sseclient-py
import json
import requests
import sseclient # sseclient-py
BASE = "https://omnidoc.top/api/v1"
HEADERS = {"X-API-Key": "sk_your_key"}
job_id = "q3f8s6c2a9m1k4tx"
ticket_response = requests.post(
f"{BASE}/sse/ticket/{job_id}", headers=HEADERS, timeout=20
)
ticket_response.raise_for_status()
ticket = ticket_response.json()["ticket"]
# Read timeout longer than the 15-second heartbeat; no API key while connected
response = requests.get(
f"{BASE}/sse/jobs/{job_id}",
params={"ticket": ticket},
stream=True,
timeout=(10, 60),
)
response.raise_for_status()
client = sseclient.SSEClient(response)
for event in client.events():
data = json.loads(event.data)
print(f"[{event.event}] {data.get('stage')} {data.get('stage_progress', 0)}%")
if event.event in {"job_done", "timeout", "error"}:
break # timeout / error: get a new ticket and reconnect, or poll instead
  • A ticket works once; get a new one before reconnecting
  • Each account can keep at most 6 SSE connections open at a time and exchange at most 30 tickets a minute; beyond that the response is 429, so wait for Retry-After or poll instead
  • When there are no job events for a long time, the server sends a timeout event; get a new ticket and reconnect
  • While the connection is idle the server sends heartbeat comments; clients should ignore comment lines
  • If the connection drops, polling GET /jobs/{id} works as a fallback