webrtc

The webrtc module (built on pion/webrtc) manages peer connections and data channels. DTLS-SRTP, ICE, and STUN are handled internally — you drive the SDP offer/answer exchange and read/write data channels. Peers are referenced by an integer handle. Import it with import webrtc.

Functions

FunctionSignatureDescription
newPeerwebrtc.newPeer(opts?) → intCreate a peer connection; opts may include iceServers. Returns a handle
createOfferwebrtc.createOffer(h: int) → stringCreate an SDP offer
createAnswerwebrtc.createAnswer(h: int) → stringCreate an SDP answer
setRemoteDescriptionwebrtc.setRemoteDescription(h: int, sdp: string, type: string) → boolApply the remote SDP (type is "offer" or "answer")
localDescriptionwebrtc.localDescription(h: int) → objectThe local description as {sdp, type}, or nil
addDataChannelwebrtc.addDataChannel(h: int, label: string, fn) → intOpen a data channel; fn(msg) fires on each message. Returns a channel id
sendDatawebrtc.sendData(h: int, channelId: int, payload: string) → boolSend on a data channel
onDataChannelwebrtc.onDataChannel(h: int, fn)fn(channelId, label) fires when the remote opens a channel
onConnectionStateChangewebrtc.onConnectionStateChange(h: int, fn)fn(state) fires on connection-state changes
closewebrtc.close(h: int) → boolClose the peer connection

Callbacks fire safely against your program even though pion delivers them from its own goroutines — see native callbacks.

Example

import webrtc

let peer = webrtc.newPeer({
    iceServers: ["stun:stun.l.google.com:19302"],
})

webrtc.onConnectionStateChange(peer, fn(state) {
    println("state: " + state)
})

let chan = webrtc.addDataChannel(peer, "chat", fn(msg) {
    println("got: " + msg)
})

let offer = webrtc.createOffer(peer)
// exchange the offer/answer SDP with the remote peer via your signaling channel
Standard library · View as Markdown · llms-full.txt