const ws = new WebSocket("ws://localhost:3000/execute/ws", {
headers: { Authorization: `Bearer ${API_KEY}` },
});
ws.onopen = () => {
ws.send(JSON.stringify({
type: "execute",
request: {
runtime: "python",
code: "for i in range(3): print(i)",
},
}));
};
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
if (msg.type === "stdout") process.stdout.write(msg.data);
if (msg.type === "stderr") process.stderr.write(msg.data);
if (msg.type === "exit") console.log(`\nExited: ${msg.data}`);
if (msg.type === "error") console.error(`Error: ${msg.data}`);
};
ws.onclose = (event) => {
console.log(`Closed: ${event.code} ${event.reason}`);
};
{"type":"stdout","data":"0\n"}
{"type":"stdout","data":"1\n"}
{"type":"stdout","data":"2\n"}
{"type":"exit","data":"0"}
Stream execution output over a persistent WebSocket connection with bidirectional messaging.
const ws = new WebSocket("ws://localhost:3000/execute/ws", {
headers: { Authorization: `Bearer ${API_KEY}` },
});
ws.onopen = () => {
ws.send(JSON.stringify({
type: "execute",
request: {
runtime: "python",
code: "for i in range(3): print(i)",
},
}));
};
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
if (msg.type === "stdout") process.stdout.write(msg.data);
if (msg.type === "stderr") process.stderr.write(msg.data);
if (msg.type === "exit") console.log(`\nExited: ${msg.data}`);
if (msg.type === "error") console.error(`Error: ${msg.data}`);
};
ws.onclose = (event) => {
console.log(`Closed: ${event.code} ${event.reason}`);
};
{"type":"stdout","data":"0\n"}
{"type":"stdout","data":"1\n"}
{"type":"stdout","data":"2\n"}
{"type":"exit","data":"0"}
| Concern | WebSocket (/execute/ws) | SSE (/execute/stream) |
|---|---|---|
| Direction | Bidirectional | Server → client only |
| Transport | Single persistent TCP connection | HTTP response stream |
| Future interactivity | Supports stdin and signal messages (reserved) | No client-to-server messaging |
| Proxy compatibility | Requires WebSocket-capable proxy | Works through any HTTP proxy |
| Client support | Bun, modern browsers, Node 21+ | Any HTTP client |
RemoteIsol8.executeStream() automatically attempts WebSocket first and falls back to SSE if the server doesn’t support WebSocket upgrade. You get the best available transport without manual selection.Authorization header on the WebSocket upgrade request, identical to all other authenticated endpoints.
Authorization: Bearer <api-key>
Authorization header.execute message with the code and options.stdout, stderr, exit, and error events as JSON messages.1000 after execution completes.WsClientMessage)"execute", "stdin", or "signal".executecode and runtime.poolStrategy and poolSize are always taken from server config.stdin (reserved)signal (reserved)"SIGINT" or "SIGTERM". Reserved for future use.WsServerMessage)StreamEvent format as the SSE endpoint.
stdout, stderr, exit, or error.exit, this is the exit code as a string (e.g. "0").| Code | Meaning |
|---|---|
1000 | Execution completed normally |
1003 | Client sent invalid JSON |
{"type":"error","data":"Invalid JSON message"} then closes with code 1003.{"type":"error","data":"Unknown message type: ..."} (connection stays open).{"type":"error","data":"..."} then closes with 1000.sessionId are not supported on WebSocket — use POST /execute for persistent sessions.const ws = new WebSocket("ws://localhost:3000/execute/ws", {
headers: { Authorization: `Bearer ${API_KEY}` },
});
ws.onopen = () => {
ws.send(JSON.stringify({
type: "execute",
request: {
runtime: "python",
code: "for i in range(3): print(i)",
},
}));
};
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
if (msg.type === "stdout") process.stdout.write(msg.data);
if (msg.type === "stderr") process.stderr.write(msg.data);
if (msg.type === "exit") console.log(`\nExited: ${msg.data}`);
if (msg.type === "error") console.error(`Error: ${msg.data}`);
};
ws.onclose = (event) => {
console.log(`Closed: ${event.code} ${event.reason}`);
};
{"type":"stdout","data":"0\n"}
{"type":"stdout","data":"1\n"}
{"type":"stdout","data":"2\n"}
{"type":"exit","data":"0"}
Was this page helpful?