The Pipecat JavaScript client can send and receive arbitrary messages to/from the server running the bot. This page outlines and demonstrates both client and server code for passing and responding to custom messages as well as providing arbitrary data at connection time.

Connection-Time Configuration

Oftentimes clients need to provide configuration data to the server when connecting. This can include things like preferred language, user preferences, initial messages, or any other data that the server needs to know about the client. This must occur before the bot is started and therefore not part of the RTVI standard, but rather a custom implementation. That said, the PipecatClient makes it easy to send this data as part of the connect() method, by passing an object with the requestData property. Your server endpoint can then handle this data as needed. In the example below, we demonstrate sending an initial prompt and preferred language to the server when connecting.
try {
    pcClient.connect({
        endpoint: 'https://your-server/connect',
        requestData: {
            initial_prompt: "You are a pirate captain",
            preferred_language: 'en-US'
        }
    });
} catch (error) {
    console.error("Error connecting to server:", error);
}

Sending Custom Messages to the Server

Once connected, you can send custom messages to the server using the sendClientMessage method. This is useful for triggering specific actions or sending data that the server needs to process.
try {
    pcClient.sendClientMessage('set-language', { language: 'en-US' });
} catch (error) {
    console.error("Error sending message to server:", error);
}

Requesting Information from the Server

You can also request information from the server using the sendClientRequest method. This is useful for querying the server for specific data or triggering and action and getting a success/failure response.
try {
    const response = await pcClient.sendClientRequest('get-language');
    console.log("Current language:", response.language);
} catch (error) {
    console.error("Error requesting data from server:", error);
}

Handling Custom Messages from the Server

You can handle custom messages sent from the server using the onServerMessage callback. This allows you to process messages that the server sends back to the client, such as notifications or updates.
pcClient.onServerMessage((message) => {
    console.log("Received message from server:", message);
    if (message.data.msg === 'language-updated') {
        console.log("Language updated to:", message.data.language);
    }
});