Skip to main content

Reactor Class

The core class for connecting to and interacting with Reactor machines.

Constructor

new Reactor(options: Options)

Options

ParameterTypeRequiredDescription
modelNamestringYesThe name of the Reactor model to connect to
queueingbooleanNoWhen true, users are queued (FIFO) if no machines are available. When false, users are disconnected immediately if no machines are available (default: false)
insecureApiKeystringYesYour Reactor API key for authentication

Methods

connect()

Connects to the Reactor coordinator and waits for GPU assignment. Once assigned, establishes connection to the GPU machine.
reactor.connect(): Promise<void>
Throws: Error if already connected or if authentication fails Example:
try {
  await reactor.connect();
  console.log("Connected successfully");
} catch (error) {
  console.error("Connection failed:", error);
}

disconnect()

Disconnects from both the coordinator and GPU machine. Safe to call multiple times.
reactor.disconnect(): Promise<void>
Example:
await reactor.disconnect();

sendMessage()

Sends a message to the model. Provided that the message is valid for the selected model, the message will be used by the model.
reactor.sendMessage(message: any): Promise<void>
Parameters:
  • message - Any JSON-serializable data to send to the machine
Throws: Error if not in ready state (except in development mode)
Always check that the reactor status is "ready" before sending messages to ensure they are processed correctly.
Example:
await reactor.sendMessage({
  type: "set_brightness",
  data: {
    brightness: 0.5
  }
});

publishVideoStream()

Publishes a video stream to the machine. The video stream will be sent to the GPU for processing by video-to-video models.
reactor.publishVideoStream(videoStream: MediaStream): Promise<void>
Parameters:
  • videoStream - A MediaStream object containing video tracks (typically from getUserMedia() or other video sources)
Throws: Error if not in ready state (except in development mode)
This method is particularly useful for video-to-video (V2V) models like StreamDiffusionV2 that transform incoming video streams in real-time.
Example:
// Capture webcam video
const stream = await navigator.mediaDevices.getUserMedia({
  video: { width: 1280, height: 720 }
});

// Publish to Reactor
await reactor.publishVideoStream(stream);

unpublishVideoStream()

Stops publishing the video stream to the machine. This unpublishes the video track that was previously sent.
reactor.unpublishVideoStream(): Promise<void>
Example:
// Stop publishing video
await reactor.unpublishVideoStream();

// Clean up the stream
stream.getTracks().forEach(track => track.stop());

on()

Registers an event listener for Reactor events.
reactor.on(event: ReactorEvent, handler: (...args: any[]) => void): void
Parameters:
  • event - The event type to listen for
  • handler - Callback function to execute when event fires
Example:
reactor.on("statusChanged", (status) => {
  console.log("Status changed to:", status);
});

reactor.on("newMessage", (message) => {
  console.log("Received message:", message);
});

off()

Removes an event listener.
reactor.off(event: ReactorEvent, handler: (...args: any[]) => void): void
Example:
const handler = (status) => console.log(status);
reactor.on("statusChanged", handler);
// Later...
reactor.off("statusChanged", handler);

getStatus()

Returns the current connection status.
reactor.getStatus(): ReactorStatus
Returns: One of "disconnected", "connecting", "waiting", or "ready" Example:
const status = reactor.getStatus();
if (status === "ready") {
  await reactor.sendMessage({ action: "start" });
}

getState()

Returns the complete current state including status, error, and waiting information.
reactor.getState(): ReactorState
Returns: Object with status, waitingInfo, and lastError properties Example:
const state = reactor.getState();
console.log("Status:", state.status);
if (state.waitingInfo) {
  console.log("Queue position:", state.waitingInfo.position);
}
if (state.lastError) {
  console.log("Last error:", state.lastError.message);
}

getWaitingInfo()

Returns queue information when in waiting state.
reactor.getWaitingInfo(): ReactorWaitingInfo | undefined
Returns: Waiting information or undefined if not waiting Example:
const waitingInfo = reactor.getWaitingInfo();
if (waitingInfo?.position) {
  console.log(`Position in queue: ${waitingInfo.position}`);
}

getLastError()

Returns the most recent error that occurred.
reactor.getLastError(): ReactorError | undefined
Returns: Error details or undefined if no errors Example:
const error = reactor.getLastError();
if (error && error.recoverable) {
  console.log("Retrying after error:", error.message);
  await reactor.connect();
}
I