RTCPeerConnection: negotiationneeded event
WebRTC
WebRTC
A **`negotiationneeded`** event is sent to the RTCPeerConnection when negotiation of the connection through the signaling channel is required.
This occurs both during the initial setup of the connection and any time a change to the communication environment requires reconfiguring the connection.
The `negotiationneeded` event is first dispatched to the RTCPeerConnection when media is first added to the connection. This starts the process of ICE negotiation by instructing your code to begin exchanging ICE candidates through the signaling server. See [Signaling transaction flow](/docs/Web/API/WebRTC_API/Signaling_and_video_calling#signaling_transaction_flow) for a description of the signaling process that begins with a `negotiationneeded` event.
This event is not cancelable and does not bubble.
## Syntax
Use the event name in addEventListener():
```js-nolint
addEventListener("negotiationneeded", (event) => { })
```
Alternatively, assign to the [event handler property](/docs/Web/API/Document_Object_Model/Events#using_onevent_properties) of the interface:
```js-nolint
onnegotiationneeded = (event) => { }
```
## Event type
A generic Event.
## Examples
In this example, we use addEventListener() to create an event handler for `negotiationneeded`. Its role is to create an SDP offer and send it through the signaling channel to the remote peer.
```js
pc.addEventListener("negotiationneeded", (ev) => {
pc.createOffer()
.then((offer) => pc.setLocalDescription(offer))
.then(() =>
sendSignalingMessage({
type: "video-offer",
sdp: pc.localDescription,
}),
)
.catch((err) => {
// handle error
});
});
```
After creating the offer, the local end is configured by calling RTCPeerConnection.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 own RTCPeerConnection, setting the remote description with setRemoteDescription(), and then creating an answer to send back to the offering peer.
You can also set an event handler for the `negotiationneeded` event by assigning the event handler function to the `onnegotiationneeded` property (but note that this replaces other event listeners added using `onnegotiationneeded`, if any):
```js
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, see [Starting negotiation](/docs/Web/API/WebRTC_API/Signaling_and_video_calling#starting_negotiation).
## Specifications
## Browser compatibility
## See also
- [WebRTC API](/docs/Web/API/WebRTC_API)
- [Signaling and video calling](/docs/Web/API/WebRTC_API/Signaling_and_video_calling)
- [Lifetime of a WebRTC session](/docs/Web/API/WebRTC_API/Session_lifetime)
- [WebRTC connectivity](/docs/Web/API/WebRTC_API/Connectivity)
- [Introduction to WebRTC protocols](/docs/Web/API/WebRTC_API/Protocols)
Cite this
"RTCPeerConnection: negotiationneeded event". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/negotiationneeded_event JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy