I'm writing a simple bot against on live kit serve...
# ask-ai
f
I'm writing a simple bot against on live kit server using sipsorcery. All works quite fine except the bot disconnects after few seconds. Can you write me the correct signalling flow for client?
t
Thanks for reaching out, Marek. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Let's walk through the correct signaling flow for a client using the LiveKit server together: 1. WebSocket Connection: First, you'll want to establish a WebSocket connection to the LiveKit server's
/rtc
endpoint. Don’t forget to include authentication using a Bearer token in the Authorization header—that’s key! 2. Join Request: Once connected, send a
JoinRequest
message. The server will then reply with a
JoinResponse
that includes important room and participant details. 3. Media Negotiation: Next up, exchange SDP offers and answers with
SessionDescription
messages, and share ICE candidates through
TrickleRequest
messages. This helps set up the media streams smoothly. 4. Connection Management: To keep things running nicely, use messages like `Ping`/`Pong` for health checks and
LeaveRequest
when you want to disconnect gracefully. By making sure your bot follows these steps, you’ll help maintain a stable and reliable connection. If you’d like, I’m here to dive deeper into any part of this flow—just let me know! Sources: livekit/livekit_rtc.pb.go | Protocol & WebRTC Stack | livekit/livekit_composite | DeepWiki | livekit-android-sdk/src/main/java/io/livekit/android/room/SignalClient.kt | test/client/client.go | test/core/signal_client_test.dart
f
What about rtcp/rtp reporting - is it required to confirm activity? What about publishing or subscribing to tracks?
t
Thank you for your question! I’d love to help clarify how activity is confirmed in LiveKit. Instead of relying on RTCP/RTP reporting, we primarily look at signaling events like track publication and subscription states to confirm media activity. That said, RTCP still plays an important role behind the scenes for things like quality feedback, congestion control, and managing bitrate. Here’s a bit more about publishing and subscribing to tracks: • Publishing: When a participant publishes a track, it becomes available for others to subscribe to. This process is managed through the
TrackPublication
object. • Subscribing: Participants can subscribe to tracks to receive and render media. If
autoSubscribe
is turned on, new tracks will be automatically delivered to participants. Also, there’s an option to enable detailed statistics reporting for media metrics, but this isn’t required to confirm activity. Please feel free to reach out if you’d like me to explain any part of this in more detail or if you have other questions—I’m here to help! Sources: Rooms, participants, and tracks | LiveKit docs | Realtime media | LiveKit Docs | Subscribing to tracks | LiveKit docs | pkg/sfu/downtrack.go | Sources/LiveKit/Track/Metrics/MetricsManager.swift
f
So, I don't need to send any signals to confirm activity, even if no stream data is sent from the bot?
t
I completely understand your concern! Even when no stream data is being sent from the bot, LiveKit still needs periodic signals to keep track of activity. This is managed through a simple ping/pong mechanism: • Ping Signals: The client sends regular ping requests to the server. • Pong Responses: The server replies with pong messages to let us know the connection is still alive. • Timeout Detection: If the client doesn’t get a pong response within the set timeout, it assumes the connection has dropped and closes it. This helps make sure the signaling connection stays active, even when no media streams are flowing. If you'd like, I’m here to walk you through how to implement this in your bot or answer any other questions you might have! Sources: livekit/src/room/participant/local_participant.rs | Sources/LiveKit/Core/PreConnectAudioBuffer.swift | lib/src/core/signal_client.dart
f
Ok, sipsorcery seems to relay on rtpreporting, but for live kit periodic ping/pong is enough. How often it should be sent? How to handle subscription for tracks of participants that are joining later? If bot joins room with autosubscribe flag - do I have to send subscribe request manually after join?
t
Thank you for your questions! Here are some helpful answers to guide you: • Ping/Pong Interval: The LiveKit SDKs take care of the ping/pong mechanism for you automatically. The server sends default values for the ping interval and timeout when you join, so you don’t have to worry about managing it yourself. • AutoSubscribe Behavior: When the
autoSubscribe
flag is on (which it is by default), the bot will automatically subscribe to all existing tracks as soon as it joins the room, and it will also subscribe to any new tracks published by participants who join later on. • Manual Subscribe Request: If
autoSubscribe
is enabled, there’s no need to send manual subscribe requests after joining. But if you decide to disable
autoSubscribe
, you’ll want to manually subscribe to the tracks you’re interested in using selective subscription methods. Please feel free to reach out if you’d like me to explain anything further or if you need a hand with the implementation. I’m here to help! Sources: pkg/rtc/room.go | livekit-api/src/signal_client/mod.rs | Rooms, participants, and tracks | LiveKit docs | src/options.ts | Subscribing to tracks | LiveKit docs
f
I do have to worry as I don't use any specific live kit client SDK - I have to handle whole flow of my own
t
Since you're working on implementing the signaling flow without using a LiveKit SDK, I’d love to help you navigate the key points you’ll need to handle manually: • Ping/Pong Mechanism: It’s important to send periodic ping messages to the server at the intervals mentioned in your join response (like the
PingInterval
). Also, keep an eye out for
Pong
responses, and if you don’t receive one within the
PingTimeout
, you’ll want to close the connection to keep things running smoothly. • AutoSubscribe: If you set
autoSubscribe
to true during the join request, the server will automatically subscribe your bot to all current and future tracks—so no need to send manual subscribe requests unless you’ve turned
autoSubscribe
off. • Track Management: Keep track of signaling messages for any new track publications (like
TrackPublished
) and handle them accordingly. If
autoSubscribe
is disabled, you’ll want to send
UpdateSubscription
requests to subscribe to specific tracks. Please feel free to reach out if you’d like me to walk you through any part of this flow in more detail—I’m here to help!
f
So, when autosubscribe is true - server will automatically, always, send TrackSubscriped or SubscriptionResponse? Or does the bot have to send Subscribe request manually?
t
When
autoSubscribe
is set to
true
, your bot will be automatically subscribed to all existing and future tracks without you needing to send a manual Subscribe request. The server will still send
TrackSubscribed
and
SubscriptionResponse
messages as part of the signaling protocol to confirm these subscriptions and keep publishers informed. If you have any questions or need a hand with handling these messages in your implementation, please don’t hesitate to reach out. I’m here to help! Sources: Rooms, participants, and tracks | LiveKit docs | Subscribing to tracks | LiveKit docs | Sources/LiveKit/Protos/livekit_rtc.pb.swift | livekit/src/room/mod.rs
f
When should I send Trickle request?
t
Whenever a new ICE candidate is discovered during the WebRTC peer connection setup, it’s a good idea to send a
TrickleRequest
in LiveKit. This usually happens in a few key moments: • During Initial Peer Connection Setup: You’ll want to send each ICE candidate as it’s gathered (for example, through
onicecandidate
callbacks) for both the publisher and subscriber connections. • After Reconnection: If the bot reconnects to the room, be sure to send any new ICE candidates to help re-establish the connections smoothly. • Final Candidate Indicator: Don’t forget to send a
TrickleRequest
with
final: true
to let the system know that candidate gathering is complete. Also, make sure the
target
field in your request clearly specifies whether the candidate is for the
PUBLISHER
or
SUBSCRIBER
connection. If you’d like, I’m here to help with any more details or guidance on how to implement this—just let me know! Sources: livekit-api/src/signal_client/mod.rs | pkg/rtc/signalling/signalhandler.go | livekit/livekit_rtc.pb.go
f
Explain what to you mean for both publisher and subscriber?
t
In LiveKit, the signaling flow involves two types of peer connections: • Publisher: This connection is used when your bot is sending media (e.g., audio, video, or data) to the server. ICE candidates for this connection should be sent with
target: PUBLISHER
in the
TrickleRequest
. • Subscriber: This connection is used when your bot is receiving media from other participants. ICE candidates for this connection should be sent with
target: SUBSCRIBER
in the
TrickleRequest
. Each connection type needs its own set of ICE candidates to set up the media path properly. If you'd like, I’m here to help with more details or examples on how to handle these connections—just let me know!
f
Yes, more details and examples would be helpful to understand the flow
t
Thank you for reaching out! Let me walk you through a detailed explanation and example for handling both publisher and subscriber connections in LiveKit. I hope this helps clarify things for you: Publisher Connection This is what you’ll use when your bot is sending media (like audio, video, or data) to the server. Here’s how it works: 1. Create a WebRTC
RTCPeerConnection
specifically for publishing. 2. Generate an SDP offer using
createOffer
and send it to the server as a
SessionDescription
message with
target: PUBLISHER
. 3. As ICE candidates come in (through the
onicecandidate
event), send each candidate to the server in a
TrickleRequest
with
target: PUBLISHER
. 4. Once all candidates are gathered, send a final
TrickleRequest
with
final: true
to let the server know candidate gathering is complete. Subscriber Connection This is used when your bot is receiving media from other participants. The steps are: 1. Receive an SDP offer from the server for the subscriber connection. 2. Create a WebRTC
RTCPeerConnection
for subscribing and set the received SDP offer as the remote description. 3. Generate an SDP answer using
createAnswer
and send it to the server as a
SessionDescription
message with
target: SUBSCRIBER
. 4. As ICE candidates are discovered (via the
onicecandidate
event), send each candidate to the server in a
TrickleRequest
with
target: SUBSCRIBER
. 5. When all candidates are gathered, send a
TrickleRequest
with
final: true
to signal the end of candidate gathering. Example TrickleRequest
Copy code
{
  "candidate": "candidate:842163049 1 udp 1677729535 192.168.1.2 3478 typ srflx raddr 0.0.0.0 rport 0 generation 0",
  "sdpMid": "0",
  "sdpMLineIndex": 0,
  "target": "PUBLISHER"
}
If anything isn’t clear or you’d like me to provide more examples, please don’t hesitate to ask. I’m here to help!
f
It seems that after sending request to etc/?token= I automatically get Join response. For which I reply with createoffer. How do I know that all candidates are gathered? I don't care about performance at this point - can I wait until all candidates are gathered and send single final Trickle?
t
Absolutely, you can wait until all ICE candidates are gathered and then send a single
TrickleRequest
with the
final
field set to
true
. This is a perfectly supported way to indicate that the candidate gathering process has finished. To keep track of when all candidates are gathered, just watch the
iceGatheringState
of your
RTCPeerConnection
. Once it changes to
complete
, it means all candidates are ready, and you can confidently send them all in one request. If you’d like, I’m here to help you with any part of this implementation—just let me know! Sources: livekit/livekit_rtc.pb.go | livekit-protocol/src/livekit.rs | lib/src/proto/livekit_rtc.pb.dart | Documentation/BasicUsage.md
f
Yes - is it normal that I get JoinResponse after calling /rtc for the first time to connect websocket. I don't have to send JoinRequest manually?
t
Hi there! Just to clarify, it’s not usual to get a
JoinResponse
automatically right after connecting to the
/rtc
WebSocket endpoint. The client actually needs to send a
JoinRequest
first to start joining the room. Once the server gets that request, it will then send back a
JoinResponse
with the room and participant details. If you’re seeing a
JoinResponse
without having sent a
JoinRequest
, it could mean there’s something off with the way things are set up on your end or with the server. Please feel free to reach out if you’d like some help figuring this out—I’m here to support you! Sources: livekit/livekit_rtc.pb.go | Protocol & WebRTC Stack | livekit/livekit_composite | DeepWiki | livekit-android-sdk/src/main/java/io/livekit/android/room/SignalClient.kt | pkg/rtc/participant_signal.go
f
that is truely wierd. Since i use sipsorcery which doesn't have any support to protobuf signaling - what could be the cause that after .connect() i get joinresponse? that is even before i send any message to websocket. Also during token creation - i set flag Agent = true - can this be the reason?
t
I understand this can be a bit confusing, so let's walk through it together. Setting the Agent flag to true during token creation doesn’t automatically trigger the server to send a
JoinResponse
right after the WebSocket connection. Instead, the
JoinResponse
is always sent in reply to a
JoinRequest
from the client. If you’re seeing a
JoinResponse
without having sent a
JoinRequest
, it might mean there’s something happening with how your WebSocket client (like sipsorcery) is managing the connection or perhaps sending messages unintentionally. It could be helpful to take a closer look at the WebSocket traffic to see if any messages are being sent automatically when the connection is established. Please feel free to reach out if you’d like support with troubleshooting this further—I’m here to help! Sources: Web and mobile frontends | LiveKit Docs | src/room/RTCEngine.ts | examples/agent-dispatch/index.ts
f
it cannot send any message that would be understood by the livekit server unintentionally, at least not via websocket for signalling - it might send something over RTP connection - could that trigger join? Sipsorcery doesnt support livekit at all and doesnt provide any automation for signalling protocol - only for rtp/rtpc
t
Thanks so much for reaching out! I’d love to help clarify how the LiveKit server works. The signaling protocol is fully based on WebSocket, while RTP/RTCP messages are handled separately just for media transport. Because of this, RTP/RTCP messages themselves can’t trigger a
JoinResponse
or any signaling-related actions since they aren’t part of the signaling layer. If sipsorcery doesn’t support LiveKit’s signaling protocol and only manages RTP/RTCP, it’s pretty unlikely that RTP/RTCP messages are causing the server to send a
JoinResponse
. The server really needs a WebSocket
JoinRequest
to start the signaling flow. To help get to the bottom of this, I’d suggest taking a look at the WebSocket traffic to see if any unexpected messages are being sent when connecting. This can often reveal what’s triggering the unexpected
JoinResponse
. Please feel free to share what you find or ask any other questions—I’m here to help!
f
come on, you could at least relay on your own documentation: 1. client initiates WebSocket connection to
/rtc
2. server sends a
JoinResponse
, which includes room information, the current participant's data, and information about other participants in the room 3. server initiates the subscriber
PeerConnection
, sends
offer
to client ◦ if
AutoSubscribe
is enabled, this offer will contain existing tracks in the room. ◦ the offer will include two data channels as part of the connection 4. client and server will exchange ICE candidates via
trickle
5. client accepts the subscriber connection, sends an
answer
6. ICE connectivity is established 7. server notifies other participants of the new participant after rtc - i get JoinResponse.
t
Thank you so much for bringing this up! You're absolutely right—when a client starts a WebSocket connection to
/rtc
, the server automatically sends a
JoinResponse
right at the beginning. This response shares important details like room information, participant data, and info about others in the room. So, there’s no need to send a
JoinRequest
manually here, since the server takes care of that as soon as the WebSocket connection is made. If you have any questions or want to walk through the next steps in the signaling flow, please feel free to ask—I’m here to help!