Skip to main content
This guide shows how to build a real-time code execution interface using React and the EnclaveJS streaming SDK.

What You’ll Build

A code editor with:
  • Real-time syntax highlighting
  • Live execution with streaming output
  • Tool call visualization
  • Error display
  • Execution statistics

Prerequisites

  • React 18+
  • Node.js server running EnclaveJS broker
  • Basic React/TypeScript knowledge

Project Setup

Architecture

Step 1: Configure EnclaveJS Client

The client sends requests and receives events as plain JSON over HTTP: in production use an https: base URL, and send your credentials with the headers option. The client has no other options than baseUrl, headers, timeout, autoReconnect, maxReconnectAttempts, reconnectDelay, maxEventLength and fetch; any other option makes the constructor throw.

An Editor on Another Origin Than the Broker

A browser asks before it sends a request with a JSON body or an Authorization header to another origin (the CORS preflight). The broker’s session routes answer that question themselves: a page of an origin in the handler’s allowedOrigins (every origin by default) may send Content-Type, Accept, Last-Event-ID and Authorization. A credential in the Authorization header therefore works from an editor on another origin. Add it to the client of this step:
On the broker, name the origin of the editor, and any other request header your client sends:
A browser does not send a header the preflight answer does not list, so a credential in a header of your own needs its entry in allowedHeaders. The preflight is an OPTIONS request without the credential: authentication in front of the routes has to let OPTIONS requests through to them.

Step 2: Create the Provider

Step 3: Create Execution Hook

Tools run on the broker. The hook is told about each tool call; it executes nothing. The broker emits no stdout events, so output stays empty unless your server sends them. Stopping has two parts. cancel() ends the session in the client at once: wait() resolves with the code CANCELLED and the hook is idle before the broker has answered. The request that asks the broker to end the session can still fail. The client then calls onError with the code CANCEL_FAILED, and the hook keeps the message in stopError: show it, because the script may still be running on the broker. The hook shows it only for the run that is still the current one. When the user has started another run before the answer arrives, the warning would appear on a run that was not stopped, so the hook drops it; an application that has to know about every unconfirmed stop keeps them in a list of its own, by session id.

Step 4: Build the Editor Component

Step 5: Build Output Panel

Step 6: Build Tool Calls Panel

Step 7: Build Stats Panel

Step 8: Assemble the Main Component

Step 9: Add Styles

Step 10: Wire It Up

Using the Official React Package

The @enclave-vm/react package provides the provider and the hook ready-made:
useEnclaveSession returns execute, cancel, reset, state, isRunning, sessionId, result, error, stdout and events. Its cancel() behaves like the one above: the session ends in the client at once, and a stop the broker did not confirm is reported to the hook’s onError option with the code CANCEL_FAILED. See @enclave-vm/react.

Best Practices

  1. Debounce execution - Don’t run on every keystroke
  2. Show loading states - Indicate when code is running
  3. Handle disconnections - The client reconnects by itself (autoReconnect) and resumes after the last event it received
  4. Limit output - Cap what you keep of the output and the events to prevent memory issues
  5. Sanitize display - Escape HTML in output
  6. Mobile support - Handle touch events for mobile editors