Client SDK
Show AI results live in your frontend,without building the plumbing.
The client SDK connects your web app to a ModelRiver request over a WebSocket. It streams tokens, shows each workflow step, picks up again after a page refresh, and follows live conversations.
npm install @modelriver/client Or add the CDN build with a script tag, no bundler needed.
Quick start
Your backend starts the request. The SDK takes the browser the rest of the way.
- 1
Start the request on your server
Call POST /v1/ai/async with your API key, and pass the channel ID and a one-time token to the browser. The token works once and expires after about 5 minutes.
server.js// On your server, where the API key lives const res = await fetch('https://api.modelriver.com/v1/ai/async', { method: 'POST', headers: { Authorization: `Bearer ${process.env.MODELRIVER_API_KEY}` }, body: JSON.stringify({ workflow: 'support_reply', messages }), }); // Send only these four fields to the browser const { channel_id, ws_token, websocket_url, websocket_channel } = await res.json(); - 2
Connect in the browser
The SDK joins that request’s channel, keeps the connection alive with heartbeats, and reconnects after short network drops.
app.jsimport { ModelRiverClient } from '@modelriver/client'; const client = new ModelRiverClient(); client.on('response', (data) => render(data)); client.on('error', (error) => showError(error)); client.connect({ channelId: channel_id, wsToken: ws_token, websocketUrl: websocket_url, websocketChannel: websocket_channel, }); - 3
Stream the answer
If the request was sent with stream: true, show the answer as it is written. The final response event still follows.
app.js// When the async request was sent with stream: true client.on('stream_delta', (chunk, text) => renderAnswer(text)); client.on('stream_done', (done) => renderAnswer(done.accumulated_text));
Use it with your framework
Each adapter wraps the same client and gives you the response, the workflow steps and any error as state.
import { useModelRiver } from '@modelriver/client/react';
const { connect, response, steps, error, isConnected } =
useModelRiver({ persist: true });Works with React 16.8 and later.
import { useModelRiver } from '@modelriver/client/vue';
const { connect, response, steps, error, isConnected } = useModelRiver();import { ModelRiverService } from '@modelriver/client/angular';
// Provide it in a component, then read response$, steps$ and error$
constructor(public modelRiver: ModelRiverService) {
this.modelRiver.init();
}import { createModelRiver } from '@modelriver/client/svelte';
const { connect, response, steps, error, isConnected } = createModelRiver();
// response, steps, error and isConnected are Svelte stores<script src="https://cdn.modelriver.com/client/v1.7.0/modelriver.min.js"></script>
const client = new ModelRiver.ModelRiverClient();
client.on('response', (data) => render(data));The script tag exposes a global ModelRiver object.
import { subscribeToSession } from '@modelriver/client';
const live = subscribeToSession({
getSubscription: () => getLiveToken(chatId),
onMessage: (message) => addMessage(message),
onSignal: (signal) => showSignal(signal),
onStatus: (status) => setBadge(status),
});Your backend gets the token from POST /v1/sessions/:id/subscribe. The SDK asks again on every reconnect.
Reference
The methods and events you will use most. The full list is in the docs.
Methods
| Method | What it does |
|---|---|
connect(options) | Joins a request’s channel with its channel ID and one-time token. |
on(event, fn) | Listens for an event. Returns a function that stops listening. |
reconnect() | Reconnects to the saved request with the saved token. |
reconnectWithBackend() | Gets a fresh token from your backend, then reconnects. |
hasPendingRequest() | Tells you whether a request is still in progress after a reload. |
disconnect() | Closes the connection. |
destroy() | Cleans up everything the client set up. |
Events
| Event | When it fires |
|---|---|
response | The final answer arrives, or the request fails. |
step | A workflow step starts, finishes or fails. |
stream_delta | A new piece of a streamed answer arrives, with the text so far. |
stream_done | Streaming finished. The final response follows. |
connected | The WebSocket connected. |
disconnected | The WebSocket closed. |
error | Something went wrong. |
Recent releases
What changed in the last few versions.
1.7.0
Added subscribeToSession, to follow a conversation live: new turns, signals from your backend, and deletion.
1.6.0
Added token streaming events for async requests sent with stream: true, and the streamed text in getState().
1.5.1
Backend pipeline statuses no longer close the connection early. The SDK now waits for the final result.
1.4.0
Updated dependencies, and connect() now checks for a channel ID and token before connecting.
Good to know
Your API key stays on your server
The browser only gets a channel ID and a one-time token. Never put your ModelRiver API key in frontend code.
Backend pipelines
While ModelRiver waits for your backend, the SDK keeps the connection open and reports each step until the pipeline completes.
Survives a refresh
With persist on, the active request is saved in the browser’s local storage, so a reload can pick it up again.
Modern browsers
Chrome 60+, Firefox 55+, Safari 12+ and Edge 79+.
Frequently asked questions
Which frameworks are supported?
React and Vue with the useModelRiver hook, Angular with ModelRiverService, Svelte with createModelRiver, and plain JavaScript through the CDN build.
Does my API key go in the browser?
No. Your backend calls ModelRiver with the API key and passes only the channel ID and a one-time token to the browser.
What happens when the user refreshes the page?
With persist on, the SDK saves the active request. Call reconnect() to reuse the saved token, or reconnectWithBackend() to get a fresh one from your backend.
Can it stream tokens?
Yes, since version 1.6.0. Send the async request with stream: true and listen for stream_delta to show the answer as it is written.
How do I show new chat messages live?
Use subscribeToSession, added in 1.7.0. Your backend gets a token from POST /v1/sessions/:id/subscribe, and the SDK delivers new turns and signals as they happen.
Works well with
Show your first live response
Start on the free plan, install the SDK, and connect it to an async request.