RTCPeerConnection.onicecandidate

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The RTCPeerConnection.onicecandidate property is an EventHandler which specifies a function to be called when the icecandidate event occurs on an RTCPeerConnection instance. This happens whenever the local ICE agent needs to deliver a message to the other peer through the signaling server. This lets the ICE agent perform negotiation with the remote peer without the browser itself needing to know any specifics about the technology being used for signaling; simply implement this method to use whatever messaging technology you choose to send the ICE candidate to the remote peer.

Syntax

RTCPeerConnection.onicecandidate = eventHandler;

Value

This should be set to a function which you provide that accepts as input an RTCPeerConnectionIceEvent object representing the icecandidate event. The function should deliver the ICE candidate, whose SDP can be found in the event's candidate property, to the remote peer through the signaling server.

If the event's candidate property is null, ICE gathering has finished.

Example

The example below, which is based on the code from the article Signaling and video calling, sets up a handler for icecandidate events to send the candidates to the remote peer.

Example and sequence of icecandidate events happening:

13:28:58.561 ice RTCPeerConnectionIceEvent {isTrusted: true, candidate: RTCIceCandidate, type: "icecandidate", …}
13:28:58.562 ice RTCPeerConnectionIceEvent {isTrusted: true, candidate: RTCIceCandidate, type: "icecandidate", …}
13:28:58.660 ice RTCPeerConnectionIceEvent {isTrusted: true, candidate: RTCIceCandidate, type: "icecandidate", …}
13:28:58.661 ice RTCPeerConnectionIceEvent {isTrusted: true, candidate: RTCIceCandidate, type: "icecandidate", …}
13:28:58.661 ice RTCPeerConnectionIceEvent {isTrusted: true, candidate: null, type: "icecandidate", …}

 

pc.onicecandidate = function(event) {
  if (event.candidate) {
    // Send the candidate to the remote peer
  } else {
    // All ICE candidates have been sent
  }
}

Specifications

Specification Status Comment
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'RTCPeerConnection.onicecandidate' in that specification.
Candidate Recommendation Initial specification.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic support
Experimental
Chrome Full support 56Edge Full support 15Firefox Full support 22IE ? Opera Full support 43
Notes
Full support 43
Notes
Notes Promise based version.
No support 37 — 43
Safari ? WebView Android Full support 56Chrome Android Full support 56Edge Mobile Full support YesFirefox Android Full support 44Opera Android Full support 43
Notes
Full support 43
Notes
Notes Promise based version.
No support 37 — 43
Safari iOS ? Samsung Internet Android Full support 6.0

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.

See also

Document Tags and Contributors

Contributors to this page: betimer, fscholz, Sheppy, jpmedley, abbycar, nils, teoli, rmnguleria
Last updated by: betimer,