We want the agent to create new publication for ea...
# ask-ai
h
We want the agent to create new publication for each say command so the client app could mange which tracks to listen and which to ignore how can we achieve this from the agent side
t
Thanks for reaching out, Gabriel. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to have the agent create a new TrackPublication for each "say" so the client can choose which utterances to subscribe to. You can do this by creating and publishing a fresh audio track for each utterance instead of reusing the agent's primary audio track. Quick steps you can implement on the agent side: 1. Create a new
AudioSource
and
LocalAudioTrack
for each say call (set sample rate/channels appropriate for your TTS). 2. Publish that track with
publish_track
(this creates a new
TrackPublication
with its own
sid
). 3. Stream TTS audio frames into the
AudioSource
(e.g., using the TTS plugin's streaming frames or converting PCM to
AudioFrame
). 4. Optionally unpublish the track when the utterance finishes (so clients can clean up). Notes the client will use: • Each publication has a unique `sid`; clients can subscribe/unsubscribe per-publication. • Listen for track-published events and inspect the publication
sid
or metadata to decide subscription. If you’d like, tell me: • which agent SDK/language you’re using (Python, JS, or other), and • which TTS engine/plugin you plan to use and I’ll provide a short, concrete code example for that stack. Sources: Subscribing to tracks | LiveKit docs
h
We are using js agent sdk with elevanlabs tts
t
Good — since you’re using the JS Agent SDK with ElevenLabs, here’s a concise, actionable approach and a short code example that creates a new TrackPublication for each utterance so clients can subscribe per-utterance. What I’m addressing: creating and publishing a fresh audio track per "say" so the client can manage subscriptions. 1) Flow summary • For each utterance: create an AudioSource + LocalAudioTrack, publish it (new TrackPublication), stream TTS audio frames into the source, then optionally unpublish when the utterance is done. • Clients can use the publication
sid
,
name
, or metadata to decide which tracks to subscribe to. 2) Minimal JS example (Agent SDK + ElevenLabs)
Copy code
import * as livekitServer from 'livekit-server-sdk'; // livekit rtc objects
import * as elevenlabs from '@livekit/agents-plugin-elevenlabs';
import { v4 as uuidv4 } from 'uuid';

async function publishUtterance(ctx: any, text: string) {
  const tts = new elevenlabs.TTS({
    voice: 'YOUR_ELEVENLABS_VOICE_ID',
    model: 'eleven_multilingual_v2',
  });

  // create a unique name for this utterance
  const trackName = `agent-utterance-${Date.now()}-${uuidv4()}`;

  // create AudioSource and LocalAudioTrack
  const sampleRate = 24000;
  const channels = 1;
  const source = new livekitServer.AudioSource(sampleRate, channels);
  const track = new livekitServer.LocalAudioTrack(trackName, source);

  // publish the track (creates new TrackPublication)
  const publication = await ctx.room.localParticipant.publishTrack(track, {
    name: trackName,
    source: livekitServer.Track.Source.Microphone,
  });

  // stream TTS audio frames and push to source
  for await (const chunk of tts.synthesize(text)) {
    // chunk.frame should be an AudioFrame compatible with AudioSource
    await source.captureFrame(chunk.frame);
  }

  // optional: unpublish/stop after finished (client can still hold onto publication until removed)
  // await ctx.room.localParticipant.unpublishTrack(track);
}
3) Tips • Use unique names or metadata (timestamp/UUID) so clients can identify each utterance. • If you want published tracks short-lived, unpublish after streaming; otherwise keep them and let clients unsubscribe. • Ensure the ElevenLabs plugin yields AudioFrame objects (or convert PCM to AudioFrame) before calling
captureFrame
. If you want, I can: • Provide a ready-to-run example adapted for your exact agent scaffolding, • Show how clients can filter/subscribe to publications by name or metadata. Which would you prefer? Sources: ElevenLabs TTS plugin guide | LiveKit Docs | Camera & microphone | LiveKit docs