onlinecompiler.io

WebSocket: run code from the browser

Connect to wss://api.onlinecompiler.io with Socket.IO and a widget key, emit a runcode event with the code, compiler id and input, and the result arrives in a codeoutput event: output, errors, exit code, run time and memory. Widget keys are made for public pages, so they can't call the REST API.

For real-time results without polling, connect via Socket.IO. The WebSocket server authenticates your API key, forwards code to the execution engine, and pushes results back instantly.

Requires a WebSocket key. Create one from the Widgets page in your dashboard. REST API keys will not work for WebSocket connections.

WebSocket URL

wss://api.onlinecompiler.io

Emit Event

runcode

Listen Event

codeoutput
websocket-client.js
import { io } from "socket.io-client";

const socket = io("wss://api.onlinecompiler.io", {
  auth: { token: "YOUR_WIDGET_KEY" }
});

socket.on("connect", () => {
  socket.emit("runcode", {
    compiler: "python-3.14",
    code: 'print("Hello via WebSocket!")',
    input: ""
  });
});

socket.on("codeoutput", (result) => {
  if (result.code === "account_limit") return console.log("Your account can't run this now."); // see your dashboard
  console.log(result.output);    // "Hello via WebSocket!"
  console.log(result.error);     // ""
  console.log(result.status);    // "success"
  console.log(result.exit_code); // 0
  console.log(result.signal);    // null
  console.log(result.truncated); // false
  console.log(result.time);      // "0.0248"
  console.log(result.total);     // "0.0330"
  console.log(result.memory);    // "8192"
});

Connecting

Pass the widget key as auth.token when you connect. The connection is refused when the key is missing or inactive, when it is a REST API key, or when the key has an allowed domain and the page's origin isn't on it. Every run on the connection uses that key.

The runcode event

FieldRequiredDescription
compilerYesA compiler id from the compilers list, such as python-3.14.
codeYesThe source code, up to 100 KB.
inputNoStandard input for the program, up to 100 KB. Empty if left out.
filesNoFiles for the program (data or helper source), each {name, content, encoding} as on the run endpoints: up to 20 files and 1 MB in total. One event, files included, can be at most 2 MB.

runcode does not pass command-line args or apply a memory_limit; for those, use runtests below, or the REST endpoints from your server.

The codeoutput event

When the program has run, codeoutput carries the same fields as the sync endpoint's response: output, error, status, exit code, signal, truncated, run time, total time and memory, plus the files the program wrote to $OUTPUT_DIR and files_truncated. Each file is in the event as base64 and its url is null: these runs store no files, since widget keys can't call the REST API to fetch them. When the run couldn't start, it carries only error, with the reason: more than one run a second on the connection, a request that isn't valid, or a queue that's unavailable.

When your account can't take the run (its monthly allowance or the key's monthly limit is used up, its per-minute or pending limit is reached, a runtests request has more tests than your plan takes in one request, or your plan is paused), the event is {"error": "The site's account can't run this right now. Try again later.", "code": "account_limit"}, for every widget key, whether or not it has an allowed domain. A runcode run that was queued and then found all of your account's runs at once still in progress after its retries gets the same error and code in an otherwise ordinary codeoutput, with exit_code -1 and nothing printed. A widget key answers a browser, so whoever is on the page never sees your plan, its limits or a billing link; check code to tell this refusal apart from the others, which have no code. The full reason is in your dashboard, the usage emails and the REST API (GET /api/usage/). The widget shows its own message for it, in the page's language.

Test cases: runtests and testresults

To judge a program against test cases from the browser, emit runtests with the same body as POST /api/run-tests/: compiler, code, tests and its optional fields, a test's args and the request's memory_limit included. The answer arrives in a testresults event with that endpoint's response (verdict, score, passed, total and a verdict per test), without output files. A request the endpoint would refuse gets {error} instead (a tests field that isn't a list of tests reads tests must be a list of at least one test), and one your account can't take, more tests than your plan takes in one request included, gets the same neutral answer as codeoutput, with code account_limit. Each test counts as one run on the key's account, and runcode and runtests share the limit of one a second on the connection. The widget's runTests() does this for you.

Stored problems: problem, runsamples, submit and submission

These four events are what the widget's problem mode uses to show one of your stored problems to a candidate. Unlike the events above, each is answered by a Socket.IO acknowledgement (the callback you pass to emit) rather than by a separate event. A refusal is {error, status} with an HTTP status number, plus code account_limit when it is your account's. They open only problems of the key's own account; a problem of another account is answered as one that does not exist.

Event, with its fieldsThe acknowledgement
problem: problem (the id)The problem's public view: title, statement, languages, starter code, the visible tests and limits, and for hidden tests only their count. Each visible test's input and expected_output is cut to 4,096 characters, with input_truncated and expected_output_truncated saying whether it was. 404 when it is not found, archived or has no published version, and for a slug: the widget key is public in the page, so problems are opened by id only. It runs nothing, so it does not count against the run limit below.
runsamples: problem (the id), compiler, codeJudges the code against the visible tests now, as a synchronous submission with no webhook, and answers with the run-tests response (verdict, score, summary, a verdict per test, submission id and receipt) without output files. Each judged test is one run on the key's account.
submit: session (the token), compiler, codeUses one of the session's Submits, queues a judging of every test and answers {submission, status}. The signed webhook takes the result to your server when the session or your API key has a callback URL; otherwise read it with GET /api/submissions/{id}/. 404 for an unknown session, 409 when the session is revoked, expired or has no Submits left, when the problem is archived, or when its key is no longer active. 400 for a compiler or code the problem does not accept (the message says why). A Submit refused after it was counted gives the Submit back.
submission: session, submission (the id)The status of a submission made under that session: queued, running, finished or failed, with its result and receipt once finished (a hidden test has its verdict only) and its error if it failed. It answers until one hour after the session expires, then 409 This session has expired.; once the session is revoked, 409 This session was revoked. at once. Another session's submission is 404.
  • Limits. runsamples and submit share the connection's limit of one a second with runcode and runtests. submission has its own limit of one a second, and so does problem (a problem opened in the last minute may be answered again without counting). Too soon is 429.
  • Your account's limits stay yours. When runsamples or submit is refused by your account (its monthly allowance or a key's monthly limit, its per-minute or pending limit, a full queue, a paused plan), or a submission fails for one of those, the answer says only The site's account can't run this right now. Try again later., with code account_limit (429 where the refusal was 429). The REST API and your webhooks give the full reason.
  • Never in an answer: hidden test data, drivers, the reference solution, checker code and checker output, and the session token again.
  • Not the score of record. A browser's copy of a result is for display. Record scores from the webhook or GET /api/submissions/{id}/ on your server.
problem-events.js
// Each of these four events is answered by a Socket.IO acknowledgement (the callback).
// A problem is named by its id, never its slug.
socket.emit("problem", { problem: "prob_3x9KpQ2mW7vL4nR8tY1zA6bC" }, (view) => {
  if (view.error) return console.log(view.status, view.error); // e.g. 404 "Problem not found"
  console.log(view.title, view.statement, view.tests);          // the public view
});

socket.emit("runsamples", { problem: "prob_3x9KpQ2mW7vL4nR8tY1zA6bC", compiler: "python-3.14", code: "print(3)" }, (result) => {
  console.log(result.verdict, result.summary);
});

// A session token your server created with POST /api/problems/{problem}/sessions/.
socket.emit("submit", { session: "pst_...", compiler: "python-3.14", code: "print(3)" }, (queued) => {
  if (queued.error) return console.log(queued.status, queued.error); // e.g. 409 "This session has no submits left."
  // queued is { submission: "sub_...", status: "queued" }. Ask for the result every 2 seconds, as the widget does:
  socket.emit("submission", { session: "pst_...", submission: queued.submission }, (answer) => {
    console.log(answer.status, answer.result); // 409 once the session is revoked, or an hour after it expires
  });
});

Questions

Which key does the WebSocket need?
A widget key, created on the Widgets page of the dashboard; REST API keys are refused. A widget key can be restricted to the domain that embeds it, and it cannot call the REST API.
How often can a connection run code?
Once a second per connection, within your account's per-minute, pending and monthly limits, which the WebSocket shares with the REST API.