Hello! I am trying to start an audio-only RoomComp...
# ask-ai
f
Hello! I am trying to start an audio-only RoomCompositeEgress to an RTMP endpoint using the Node.js "livekit-server-sdk". I am getting a ReferenceError and I'm confused about the correct way to structure the API call. Here is my current code in room.service.js: JavaScript // My imports at the top of the file: const { roomServiceClient, egressClient } = require('../utils/livekitClient'); const { AccessToken } = require('livekit-server-sdk'); const { v4: uuidv4 } = require('uuid'); // ... other imports // My function to start the egress: exports.startCompositeEgress = async (roomName, roomId) => { try { const streamKey = uuidv4(); const rtmpUrl = `rtmp//127.0.0.11935/live/${streamKey}`; // This is the part that is failing const egressInfo = await egressClient.startRoomCompositeEgress(roomName, { stream: new StreamOutput({ protocol: StreamProtocol.RTMP, // This line causes the error urls: [rtmpUrl], }), }, { audioOnly: true }); console.log(
Egress started with ID: ${egressInfo.egressId}
); } catch (error) { console.error('Egress start failed:', error); } }; The Error I'm getting is: ReferenceError: StreamOutput is not defined (and also StreamProtocol is not defined). My Question: What is the correct way to import StreamOutput and StreamProtocol? And what is the exact, correct structure for the startRoomCompositeEgress call for an audio-only RTMP stream? Could you please provide the final, correct code snippet, including all necessary require statements from the livekit-server-sdk?
t
Thanks for reaching out, Priyanshu. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! To get your audio-only RoomCompositeEgress up and running smoothly with the Node.js "livekit-server-sdk" and stream it to an RTMP endpoint, you just need to make sure your imports and API call are set up right. Here’s a friendly, fixed-up code snippet for you:
Copy code
// Import necessary modules
const { EgressClient, StreamOutput, StreamProtocol } = require('livekit-server-sdk');
const { v4: uuidv4 } = require('uuid');

// Create an instance of EgressClient
const egressClient = new EgressClient(
  <LIVEKIT_SERVER_URL>, // Swap this out for your LiveKit server URL
  <API_KEY>, // Your API key goes here
  <API_SECRET> // And your API secret here
);

// Function to start the egress
exports.startCompositeEgress = async (roomName) => {
  try {
    const streamKey = uuidv4();
    const rtmpUrl = `rtmp://127.0.0.1:1935/live/${streamKey}`;

    // Define the RTMP output
    const streamOutput = new StreamOutput({
      protocol: StreamProtocol.RTMP,
      urls: [rtmpUrl],
    });

    // Start the egress
    const egressInfo = await egressClient.startRoomCompositeEgress(
      roomName,
      { stream: streamOutput },
      { audioOnly: true }
    );

    console.log(`Egress started with ID: ${egressInfo.egressId}`);
  } catch (error) {
    console.error('Oops, starting the egress didn’t work:', error);
  }
};
Just a heads-up: don’t forget to replace those placeholders like
<LIVEKIT_SERVER_URL>
,
<API_KEY>
, and
<API_SECRET>
with your real LiveKit server details. This setup ensures your RTMP stream is configured just right for audio-only streaming. If you hit any bumps, I’m here to help! Sources: packages/livekit-server-sdk/src/EgressClient.ts | RoomComposite Egress | LiveKit Docs