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. 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. 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.js
    import { 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. 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.

Chat.tsx
import { useModelRiver } from '@modelriver/client/react';

const { connect, response, steps, error, isConnected } =
  useModelRiver({ persist: true });

Works with React 16.8 and later.

Reference

The methods and events you will use most. The full list is in the docs.

Methods

MethodWhat 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

EventWhen it fires
responseThe final answer arrives, or the request fails.
stepA workflow step starts, finishes or fails.
stream_deltaA new piece of a streamed answer arrives, with the text so far.
stream_doneStreaming finished. The final response follows.
connectedThe WebSocket connected.
disconnectedThe WebSocket closed.
errorSomething went wrong.

Recent releases

What changed in the last few versions.

  1. 1.7.0

    Added subscribeToSession, to follow a conversation live: new turns, signals from your backend, and deletion.

  2. 1.6.0

    Added token streaming events for async requests sent with stream: true, and the streamed text in getState().

  3. 1.5.1

    Backend pipeline statuses no longer close the connection early. The SDK now waits for the final result.

  4. 1.4.0

    Updated dependencies, and connect() now checks for a channel ID and token before connecting.

Good to know

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.

Show your first live response

Start on the free plan, install the SDK, and connect it to an async request.