RTCPeerConnection.createAnswer()

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

The createAnswer() method on the RTCPeerConnection interface creates an answer to an offer received from a remote peer during the offer/answer negotiation of a WebRTC connection. Once the answer is created, it should be sent to the source of the offer to continue the negotiation process.

Syntax

JavaScript
aPromise = RTCPeerConnection.createAnswer([options]);

RTCPeerConnection.createAnswer(successCallback, failureCallback[, options]); <span title="This deprecated API should no longer be used, but will probably still work.">class="icon-thumbs-down-alt"> </span>

Parameters

options Optional
An object which contains options which customize the answer; this is based on the RTCAnswerOptions dictionary.

Deprecated parameters

In older code and documentation, you may see a callback-based version of this function. This has been deprecated and its use is strongly discouraged. You should update any existing code to use the Promise-based version of createAnswer() instead. The parameters for this form of createAnswer() are described below, to aid in updating existing code.

successCallback
An RTCSessionDescriptionCallback which will be passed a single RTCSessionDescription object describing the newly-created answer.
failureCallback
An RTCPeerConnectionErrorCallback which will be passed a single DOMError object explaining why the request to create an answer failed.
options Optional
An optional RTCOfferOptions object providing options requested for the answer.

Exceptions

NotReadableError
The identity provider wasn't able to provide an identity assertion.
OperationError
Generation of the SDP failed for some reason; this is a general failure catch-all exception.

Return value

A Promise whose fulfillment handler is called with a RTCSessionDescription object which contains the SDP answer to be delivered to the other peer.

Example

Here is a segment of code taken from the code that goes with the article Signaling and video calling. This code comes from the handler for the message sent to carry an offer to another peer across the signaling channel.

Keep in mind that this is part of the signaling process, the transport layer for which is an implementation detail that's entirely up to you. In this case, a WebSocket connection is used to send a JSON message with a type field with the value "video-answer" to the other peer, carrying the answer to the device which sent the offer to connect. The contents of the object being passed to the sendToServer() function, along with everything else in the promise fulfillment handler, depend entirely on your design

JavaScript
pc.createAnswer().then(function(answer) {
  return pc.setLocalDescription(answer);
})
.then(function() {
  // Send the answer to the remote peer through the signaling server.
})
.catch(handleGetUserMediaError);

This asks RTCPeerConnection to create and return a new answer. In our promise handler, the returned answer is set as the description of the local end of the connection by calling setLocalDescription().

Once that succeeds, the answer is sent to the signaling server using whatever protocol you see fit.

Promise.catch() is used to trap and handle errors.

See "Handling the invitation" in Signaling and video calling to see the complete code, in context, from which this snippet is derived; that will help you understand the signaling process and how answers work.

Specifications

Specification Status Comment
WebRTC 1.0: Real-time Communication Between Browser
The definition of 'createAnswer()' in that specification.
Working Draft Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Yes) [1] 22 (22) [2] ? ? ?
Promise-based version 52.0 ? ? ? ?
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support No support (Yes) [1] 24.0 (24) [2] ? [2] ? ? ? (Yes) [1]
Promise-based version No support 52.0 ? ? ? ? ? 52.0

[1] The callback-based version of this method was deprecated in Chrome 49.

[2] The callback-based version of this method was deprecated in Firefox 37.

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/rtcpeerconnection/createanswer

API createAnswer Experimental Media Method Reference RTCPeerConnection Video video Web WebRTC