SmallWebRTCTransport enables peer-to-peer WebRTC connections between clients and your Pipecat application. It implements bidirectional audio and video streaming using WebRTC for real-time communication.

This transport is intended for lightweight implementations, particularly for local development and testing. It expects your Pipecat server to include the corresponding SmallWebRTCTransport server-side implementation.

SmallWebRTCTransport is best used for testing and development. For production deployments with scale, consider using the DailyTransport, as it has global, low-latency infrastructure.

Usage

Basic Setup

import { RTVIClient } from "@pipecat-ai/client-js";
import { SmallWebRTCTransport } from "@pipecat-ai/small-webrtc-transport";

const transport = new SmallWebRTCTransport();

const rtviClient = new RTVIClient({
  transport,
  enableCam: false, // Default camera off
  enableMic: true, // Default microphone on
  callbacks: {
    // Event handlers
  },
  params: {
    baseUrl: "http://localhost:7860",
    endpoints: {
      connect: "/api/offer",
    },
  },
});

await rtviClient.connect();

API Reference

Constructor

new SmallWebRTCTransport();

The constructor does not take any parameters.

Properties

iceServers
string[]

Array of STUN/TURN server URLs for ICE connection establishment. Default is ["stun:stun.l.google.com:19302"].

// Set custom ICE servers
transport.iceServers = [
  "stun:stun.l.google.com:19302",
  "stun:stun1.l.google.com:19302",
];

Methods

Most methods have an equivalent in the RTVI client. The transport is designed to be used with the RTVI client, so in most cases you should use the RTVI client methods to interact with the transport, rather than calling these methods directly. This is especially true for initialize(), connect(), and disconnect().

setAudioCodec
method

Sets the preferred audio codec.

transport.setAudioCodec("opus");
setVideoCodec
method

Sets the preferred video codec.

transport.setVideoCodec("VP8");

States

The transport can be in one of these states:

type TransportState =
  | "initializing"
  | "initialized"
  | "connecting"
  | "connected"
  | "ready"
  | "disconnecting"
  | "disconnected"
  | "error";

Events

The transport implements the various RTVI event handlers. These events will fire when used with an RTVIClient.

Key callback events:

onConnected
callback

Called when connection to the server-side transport is established and it is ready to communicate across the data channel and stream media.

onBotReady
callback

The bot has been instantiated, its pipeline is configured, and it is receiving user media and interactions.

onDisconnected
callback

Called when connection to the server is lost.

onTransportStateChanged
callback

Called when the transport state changes.

onTrackStarted
callback

Called when a new track is received from the server.

Connection Process

The connection process follows these steps:

  1. The transport negotiates a WebRTC connection with the corresponding pipecat transport, complete with transceivers for the media and a data channel for messaging.
  2. The transport sends a message to the pipecat transport to let it know it’s ready.
  3. The Pipecat transport sends a message letting the client know it is ready.

Reconnection Handling

The transport includes automatic reconnection logic:

  • Up to 3 reconnection attempts after connection failures
  • Detection of ICE connection state changes
  • Graceful recovery from temporary disconnections
  • Graceful disconnect when reconnection attempts fail

More Information