RTCPeerConnection: negotiationneeded event
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2017.
Anegotiationneeded
event is sent to theRTCPeerConnection
when negotiation of the connection through the signaling channel is required.This occurs both during the initial setup of the connection as well as any time a change to the communication environment requires reconfiguring the connection.
Thenegotiationneeded
event is first dispatched to theRTCPeerConnection
when media is first added to the connection. This starts the process ofICE negotiation by instructing your code to begin exchanging ICE candidates through the signaling server. SeeSignaling transaction flow for a description of the signaling process that begins with anegotiationneeded
event.
This event is not cancelable and does not bubble.
Syntax
Use the event name in methods likeaddEventListener()
, or set an event handler property.
addEventListener("negotiationneeded", (event) => { })onnegotiationneeded = (event) => { }
Event type
A genericEvent
.
Examples
In this example, we useaddEventListener()
to create an event handler fornegotiationneeded
. Its role is to create anSDP offer and send it through the signaling channel to the remote peer.
pc.addEventListener( "negotiationneeded", (ev) => { pc.createOffer() .then((offer) => pc.setLocalDescription(offer)) .then(() => sendSignalingMessage({ type: "video-offer", sdp: pc.localDescription, }), ) .catch((err) => { // handle error }); }, false,);
After creating the offer, the local end is configured by callingRTCPeerConnection.setLocalDescription()
; then a signaling message is created and sent to the remote peer through the signaling server, to share that offer with the other peer. The other peer should recognize this message and follow up by creating its ownRTCPeerConnection
, setting the remote description withsetRemoteDescription()
, and then creating an answer to send back to the offering peer.
You can also set an event handler for thenegotiationneeded
event by assigning the event handler function to theonnegotiationneeded
property:
pc.onnegotiationneeded = (ev) => { pc.createOffer() .then((offer) => pc.setLocalDescription(offer)) .then(() => sendSignalingMessage({ type: "video-offer", sdp: pc.localDescription, }), ) .catch((err) => { // handle error });};
For a more detailed example, seeStarting negotiation.
Specifications
Specification |
---|
WebRTC: Real-Time Communication in Browsers # dom-rtcpeerconnection-onnegotiationneeded |