when testing locally with livekit cloud, my agent ...
# ask-ai
r
when testing locally with livekit cloud, my agent connects after the participant. I am able to detect published tracks with
_self_.ctx.room.remote_participants.values()
but publication.track is None. I want to subscribe to this video track. Should I await it?
t
Thanks for reaching out, Ivan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're seeing
publication.track
as None because the remote track hasn't been subscribed (or signaled) yet. To get the actual
Track
object you should subscribe and wait for the subscription event rather than relying on
publication.track
immediately. What to do: 1. Ensure
autoSubscribe
is enabled, or call
publication.setSubscribed(True)
to request subscription. 2. Await the subscription by handling the track-subscribed event (or the SDK equivalent) and use the track provided there. Example (Python-style):
Copy code
# request subscription if needed
publication.setSubscribed(True)

# handle when the track is ready
@self.ctx.room.on("track_subscribed")
def on_track_subscribed(track, publication, participant):
    if track.kind == rtc.TrackKind.KIND_VIDEO:
        # `track` is now available for rendering/processing
        ...
If you tell me which SDK (Python, JS, iOS, Android) and whether
autoSubscribe
is on, I can give a concrete code snippet for your environment. Sources: Subscribing to tracks | LiveKit docs | client-sdk-js | livekit/livekit_composite | DeepWiki
r
can I await the subscription event for a given publication?
t
Yes — you can await a subscription for a specific publication by waiting for the track-subscribed event for that publication. Options: • If your SDK is async (Python example):
Copy code
# request subscription if needed
publication.setSubscribed(True)

# await the specific subscription event
async def wait_for_subscription(pub, room):
    fut = asyncio.get_event_loop().create_future()
    @room.on("track_subscribed")
    def _on(track, publication, participant):
        if publication == pub:
            fut.set_result(track)
    return await fut

track = await wait_for_subscription(publication, room)
# track is now available
• If using JavaScript/TypeScript (Promise + once):
Copy code
publication.setSubscribed(true);

const track = await new Promise((resolve) => {
  room.once(RoomEvent.TrackSubscribed, (t, p) => {
    if (p === publication) resolve(t);
  });
});
Use whichever pattern matches your SDK. If you tell me which client (Python/JS/iOS/Android) you're using, I can provide a concrete snippet tailored to it. Sources: Job lifecycle | LiveKit docs | client-sdk-swift | livekit/livekit_composite | DeepWiki | Subscribing to tracks | LiveKit docs