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 { PipecatClient } from "@pipecat-ai/client-js";
import { SmallWebRTCTransport } from "@pipecat-ai/small-webrtc-transport";

const pcClient = new PipecatClient({
  transport: new SmallWebRTCTransport ({
    // Optional configuration for the transport
    iceServers: ["stun:stun.l.google.com:19302"],
  }),
  enableCam: false, // Default camera off
  enableMic: true, // Default microphone on
  callbacks: {
    // Event handlers
  },
});

await pcClient.connect({
    endpoint: 'https://your-server/connect',
});

API Reference

Constructor Options

interface SmallWebRTCTransportConstructorOptions {
  iceServers?: RTCIceServer[];
  waitForICEGathering?: boolean;
  connectionUrl?: string;
  audioCodec?: string;
  videoCodec?: string;
}

Properties

TransportConnectionParams

export type SmallWebRTCTransportConnectionOptions = {
  connectionUrl?: string;
};

On connect(), the SmallWebRTCTransport optionally takes a set of connection parameters. This can be provided directly or via a connection endpoint passed to the PipecatClient’s connect method. If using an endpoint, your endpoint should return a JSON object matching the SmallWebRTCTransportConnectionOptions type, which currently expects a single connectionUrl property.

pcClient.connect({
  connectionUrl: 'https://your-pipecat-webrtc-server'
});
// OR...
pcClient.connect({
  endpoint: 'https://your-server/api/offer',
});

Methods

For most operations, you will not interact with the transport directly. Most methods have an equivalent in the PipecatClient and should be called from the PipecatClient. However, there are a few transport-specific methods that you may need to call directly. When doing so, be sure to access your transport via the transport property of the PipecatClient instance.

setAudioCodec
method

Sets the preferred audio codec.

transport.setAudioCodec("opus");
setVideoCodec
method

Sets the preferred video codec.

transport.setVideoCodec("VP8");

Events

The transport implements the various PipecatClient event handlers.

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