Skip to Content
Runlane
RealtimeReact hooks

React hooks

Install the React package, add the provider, and pick the right hook.

@runlane/react gives React components live access to runs. It has one provider and two hooks.

Install

npm install @runlane/react @runlane/realtime

The hooks need React 19. You also need a mounted realtime handler. See set up realtime.

Add the provider

Wrap the part of your app that shows runs in RunlaneProvider. Set baseUrl to the path where you mounted the handler.

src/progress-provider.tsx
'use client'

import type { ReactNode } from 'react'

import { RunlaneProvider } from '@runlane/react'

export function ProgressProvider({ children }: { children: ReactNode }) {
  return <RunlaneProvider baseUrl="/api/progress">{children}</RunlaneProvider>
}

Render <ProgressProvider> around the pages that show runs, for example in a layout. Keep it in its own 'use client' file. In Next.js, a server layout cannot pass functions such as refreshAccessToken to a client component.

That is all you need for session cookies. For tokens, also pass refreshAccessToken, as shown in refresh expired tokens.

PropMeaning
baseUrlRequired. The handler's mount path, such as /api/progress, or a full URL on another origin.
refreshAccessTokenOptional. Returns a fresh token for a run. See refresh expired tokens.
fetchOptional. A custom fetch function.
telemetry, onTelemetryErrorOptional. Record client metrics. See telemetry.

The provider creates one shared client. Inline functions are fine: changing refreshAccessToken does not reopen readers.

A hook used outside a provider throws configuration_invalid during render. To skip the provider, pass a client with the client option instead.

Pick a hook

HookUse it to showGuide
useRunStatus, progress from metadata, and the final outputRun updates in React
useRunStreamText or data the task streams while it runsStreams in React

Each hook opens its own connection. A component that uses both opens two.

Shared options

Both hooks take these options:

OptionMeaning
accessTokenToken for the first connection. Leave it out when you use cookies.
enabledSet to false to open nothing. Defaults to true.
throttleHow long to group updates before a re-render, as a duration string such as '100ms'. Defaults to contractDefaults.realtime.renderThrottle.
onConnectionChangeCalled with each connection change: connecting, open, reconnecting, or closed.
clientA client from createRunlaneClient(). Use it instead of the provider.

Both hooks also return:

FieldMeaning
errorA RunlaneError if reading failed, otherwise undefined
isConnectedtrue while the connection is open
stop()Closes the connection and keeps the last data

When a hook reconnects

A hook opens a new reader, and clears its data, only when these change:

  • the run ID, enabled, or client;
  • for useRunStream, the stream name, mode, or maxParts;
  • the provider's baseUrl or fetch.

Changing accessToken, throttle, or a callback keeps the reader open. Callbacks always use their newest version.

Unmounting a component closes its reader. Pass undefined as the run ID to open nothing until you have one.

Server rendering

During server rendering, the hooks return empty data with isConnected: false. They make no network requests. Readers start after React renders the component in the browser.

Stopping is not cancelling

stop() and unmounting only close the connection. The run keeps going on the worker. To cancel a run, call runlane.runs.cancel(runId) on your server.

Errors

error is always a RunlaneError. Check its code:

CodeWhat happened
access_deniedThe reader is not allowed to read this run, or its token could not be refreshed
run_not_foundThe run does not exist, or it was pruned
disconnectedThe client gave up after too many failed reconnects
validation_failedThe run ID or stream name is not valid
wire_invalidThe response was not from the realtime handler. Check baseUrl. See troubleshooting.

Show your own message to users. Do not show error.cause; it can hold internal details. See the reference for every code.

A failed run is not an error. It arrives as data with run.status set to failed.

On this page