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.
WebSocket URL
wss://api.onlinecompiler.ioEmit Event
runcodeListen Event
codeoutputimport { 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) => {
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
| Field | Required | Description |
|---|---|---|
| compiler | Yes | A compiler id from the compilers list, such as python-3.14. |
| code | Yes | The source code, up to 100 KB. |
| input | No | Standard input for the program, up to 100 KB. Empty if left out. |
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. When the run couldn't start, it carries only error, with the reason: more than one run a second on the connection, an account limit, a request that isn't valid, or a queue that's unavailable.
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.