Can I simulate RemoteParticipant for console debug...
# ask-ai
f
Can I simulate RemoteParticipant for console debugging?
t
Thanks for reaching out, Taras. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to simulate a
RemoteParticipant
for console debugging in LiveKit, you can use the
simulateParticipants
method in the JavaScript client SDK. It’s a handy way to fill a room with simulated participants, complete with dummy audio and video tracks, without actually connecting to a server. Here’s a quick example to get you started:
Copy code
await room.simulateParticipants({
  participants: {
    count: 5, // Number of simulated remote participants
    video: true, // Include video tracks
    audio: false, // Exclude audio tracks
    aspectRatios: [1.66, 1.77, 1.33] // Optional: vary video dimensions
  },
  publish: {
    useRealTracks: false // Use dummy media tracks
  }
});
Once you run this, you’ll see the
room.remoteParticipants
collection filled with these simulated participants. Your app will also get the usual
participantConnected
events, just like real folks joining in. If you want to dig in manually, you can expose the
Room
object right in your browser console like this:
Copy code
window.__lk_room = room;
That way, you can peek at
__lk_room.remoteParticipants
directly in the console whenever you like. Feel free to reach out if you want more help or need tips tailored to your specific setup—I’m here for you! Sources: src/room/Room.ts | lib/Debug.tsx | frontend/lib/Debug.tsx | livekit-agents/livekit/agents/ipc/mock_room.py | pkg/rtc/participant_internal_test.go
f
I use Python SDK
t
Hey there! If you’re working with the Python SDK and want to simulate a
RemoteParticipant
for debugging, you’re in luck. You can use the handy mocking tools in the
livekit-agents
repository. The
create_mock_room()
function sets up a fully mocked
Room
instance that already includes a
RemoteParticipant
ready to go. Here’s a quick guide on how to get started: 1. *Import and Create the Mock Room:*`from livekit.agents.ipc.mock_room import create_mock_room mock_room = create_mock_room()`This will give you a mocked
Room
object with a
RemoteParticipant
already inside. 2. *Access the RemoteParticipant:*`remote_participant = next(iter(mock_room.remote_participants.values())) print(remote_participant.identity) # Output: fake_human`You can use this participant to play around with track subscriptions, data handling, or RPC calls in your tests. 3. *Customize (Optional):*`# Example: Simulate participant metadata change remote_participant.metadata = "test_metadata"`Since it uses
unittest.mock.create_autospec
, you’ve got the flexibility to tweak the
RemoteParticipant
or its methods to fit your specific test cases. This method makes your unit tests safe, quick, and easy to repeat when dealing with participant-related features. If you want a hand with anything else, just let me know—I’m here to help! Sources: livekit-agents/livekit/agents/ipc/mock_room.py
f
how can I use inside entrypoint function?
t
Hey there! When you want to use
create_mock_room
inside an entrypoint function, you usually don’t have to call it yourself. The LiveKit Agents framework kindly takes care of providing a mock room for you when you run in console mode (like with the
--console
flag). This mock room is created behind the scenes using
create_mock_room
and you can access it easily via
ctx.room
. Here’s a simple example of how you might use it in your entrypoint:
Copy code
async def entrypoint(ctx: JobContext):\n    room = ctx.room  # This will be a real or mock room depending on execution mode\n\n    @room.on("track_subscribed")\n    def on_track_subscribed(track: rtc.Track, participant: rtc.RemoteParticipant):\n        print(f"Track {track.sid} subscribed from {participant.identity}")\n\n    await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
If you’re running with
--console
, just know that
ctx.room
is already a mock room created by
create_mock_room
, so no extra setup is needed on your part. Now, if you want to do some unit testing outside of the framework, you can manually create a mock room like this:
Copy code
from livekit.agents.ipc.mock_room import create_mock_room\n\nmock_room = create_mock_room()
That said, it’s best not to use this approach in production entrypoints. If anything’s unclear or you want to dive deeper, just give me a shout—I’m here to help! Sources: examples/voice_agents/llamaindex-rag/query_engine.py | Inside a session | LiveKit Docs
f
how can I debug room.on events in --console mode?
t
To help you debug
room.on
events in
--console
mode using the Python SDK, a great way is to use Python’s logging module to capture and display event data as it happens. Here’s a simple way to get started: 1. *Set up logging:*`import logging\nlogging.basicConfig(level=logging.INFO)\nlogger = logging.getLogger(name)` 2. *Register event listeners:*`@room.on("participant_connected")\ndef on_participant_connected(participant):\n logger.info(f"Participant connected: {participant.identity}")\n\n@room.on("track_subscribed")\ndef on_track_subscribed(track, publication, participant):\n logger.info(f"Track subscribed: {publication.sid} from {participant.identity}")` 3. Run your script: Just run your script in a terminal, and you’ll see the logged events pop up in real time. If you want to dive deeper, you can turn on debug-level logging with a
--debug
flag or even log to both the console and a file to make things easier to analyze later. You’ll find this approach in examples like
basic_room.py
and
stream_audio.py
. Feel free to reach out if you’d like me to walk you through more examples or if there’s anything else I can help with! Sources: Voice AI quickstart | LiveKit Docs | README.md | livekit-rtc/livekit/rtc/_proto/room_pb2.py | stream_audio.py | livekit-agents/livekit/agents/cli/cli.py
f
The events are not happening
t
Hey there! If your
room.on
events aren’t firing, don’t worry — here are some common reasons why and how you can fix them: 1. Event Listeners Registered After `connect()`: Make sure you set up all your event listeners before calling
room.connect()
. For example:
room = rtc.Room()\n\n@room.on("participant_connected")\ndef on_participant_connected(participant):\n    logging.info(f"Participant connected: {participant.identity}")\n\nawait room.connect(URL, TOKEN)
2. Async Callbacks: If your callbacks do async work, use
asyncio.create_task()
to handle them properly. Here’s an example:
@room.on("track_subscribed")\ndef on_track_subscribed(track, publication, participant):\n    asyncio.create_task(handle_track_async(track))
3. Connection Issues: Double-check your connection status with
room.isconnected()
and keep an eye on events like
connected
and
disconnected
. For instance:
@room.on("connected")\ndef on_connected():\n    logger.info("Successfully connected to LiveKit room")
4. Auto-subscribe Configuration: If
auto_subscribe
is off, tracks won’t subscribe automatically — you’ll need to do it manually. So, make sure
auto_subscribe=True
if that’s what you want. 5. Exception Handling: It’s a good idea to add error handling in your callbacks and turn on debug logging. This way, you won’t miss any sneaky exceptions. Give these a try, and if things still aren’t working, just let me know! Sharing your SDK version, Python version, and any logs will help me help you better. Sources: src/room/events.ts | livekit-rtc/livekit/rtc/_proto/room_pb2.pyi | terminal-client/client.py | Still Have Questions
f
provide an example of entrypoint with mock_room and working room.on evants
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!