WebRTC client
Web Real-Time Communication (WebRTC) is a set of standards and APIs for real-time, peer-to-peer communication in browsers and native apps.
The WebRTC client in Ktor enables real-time peer-to-peer communication in multiplatform projects. With WebRTC, you can build features such as:
Video and voice calls
Multiplayer games
Collaborative applications (whiteboards, editors, etc.)
Low-latency data exchange between clients
Add dependencies
To use WebRtclient
, you need to include the ktor-client-webrtc
artifact in the build script:
Create a client
When creating a WebRtcClient
, choose an engine based on your target platform:
JS/Wasm:
JsWebRtc
– uses browserRTCPeerConnection
and media devices.Android:
AndroidWebRtc
– usesPeerConnectionFactory
and Android media APIs.
You can then provide platform-specific configuration similar to HttpClient
. STUN/TURN servers are required for ICE to work correctly. You can use existing solutions such as coturn:
Create a connection and negotiate SDP
After creating a WebRtcClient
, the next step is to create a peer connection. A peer connection is the core object that manages the real-time communication between two clients.
To establish a connection, WebRTC uses the Session Description Protocol (SDP). This involves three steps:
One peer (the caller) creates an offer.
The other peer (the callee) responds with an answer.
Both peers apply each other’s descriptions to complete the setup.
Exchange ICE candidates
Once SDP negotiation is complete, peers still need to discover how to connect across networks. Interactive Connectivity Establishment (ICE) allows peers to find network paths to each other.
Each peer gathers its own ICE candidates.
These candidates must be sent to the other peer through your chosen signaling channel.
Once both peers add each other’s candidates, the connection can succeed.
Use a data channel
WebRTC supports data channels, which let peers exchange arbitrary messages. This is useful for chat, multiplayer games, collaborative tools, or any low-latency messaging between clients.
Creating a channel
To create a channel on one side, use the .createDataChannel()
method:
You can then listen for data channel events on the other side:
Sending and receiving messages
Channels use a Channel
-like API, familiar to Kotlin developers:
Add and observe media tracks
In addition to data channels, WebRTC supports media tracks for audio and video. This allows you to build applications such as video calls or screen sharing.
Creating local tracks
You can request audio or video tracks from local devices (microphone, camera):
On the web, this uses navigator.mediaDevices.getUserMedia
. On Android, it uses the Camera2 API and you must request microphone/camera permissions manually.
Receiving remote tracks
You can also listen for remote media tracks:
Limitations
The WebRTC client is experimental and has the following limitations:
Signaling is not included. You need to implement your own signaling (for example, with WebSockets or HTTP).
Supported platforms are JavaScript/Wasm and Android. iOS, JVM desktop, and Kotlin/Native support are planned in future releases.
Permissions must be handled by your application. Browsers prompt users for microphone and camera access, while Android requires runtime permission requests.
Only basic audio and video tracks are supported. Screen sharing, device selection, simulcast, and advanced RTP features are not yet available.
Connection statistics are available but differ across platforms and do not follow a unified schema.