Failed to set remote answer sdp: Called in wrong state: stable

Tags: , , , ,

I am trying to write a WebRTC application using

The signalling server is written in python and looks like this.

import socketio
import uvicorn
from starlette.applications import Starlette

ROOM = 'room'

sio = socketio.AsyncServer(async_mode='asgi', cors_allowed_origins='*')
star_app = Starlette(debug=True)
app = socketio.ASGIApp(sio, star_app)

async def connect(sid, environ):
    await sio.emit('ready', room=ROOM, skip_sid=sid)
    sio.enter_room(sid, ROOM)

async def data(sid, data):
    await sio.emit('data', data, room=ROOM, skip_sid=sid)

async def disconnect(sid):
    sio.leave_room(sid, ROOM)

if __name__ == '__main__':, host='', port=8003)

The client side looks like this

    const SIGNALING_SERVER_URL = '';
    // WebRTC config: you don't have to change this for the example to work
    // If you are testing on localhost, you can just use PC_CONFIG = {}
    const PC_CONFIG = {};

    // Signaling methods
    let socket = io(SIGNALING_SERVER_URL, {autoConnect: false});

    socket.on('data', (data) => {
        console.log('Data received: ', data);

    socket.on('ready', () => {
        // Connection with signaling server is ready, and so is local stream

    let sendData = (data) => {
        socket.emit('data', data);

    // WebRTC methods
    let pc;
    let localStream;
    let remoteStreamElement = document.querySelector('#remoteStream');

    let getLocalStream = () => {
        navigator.mediaDevices.getUserMedia({audio: true, video: true})
            .then((stream) => {
                console.log('Stream found');
                localStream = stream;
                // Connect after making sure that local stream is availble
            .catch(error => {
                console.error('Stream not found: ', error);

    let createPeerConnection = () => {
        try {
            pc = new RTCPeerConnection(PC_CONFIG);
            pc.onicecandidate = onIceCandidate;
            pc.onaddstream = onAddStream;
            console.log('PeerConnection created');
        } catch (error) {
            console.error('PeerConnection failed: ', error);

    let sendOffer = () => {
        console.log('Send offer');
            (error) => {
                console.error('Send offer failed: ', error);

    let sendAnswer = () => {
        console.log('Send answer');
            (error) => {
                console.error('Send answer failed: ', error);

    let setAndSendLocalDescription = (sessionDescription) => {
        console.log('Local description set');

    let onIceCandidate = (event) => {
        if (event.candidate) {
            console.log('ICE candidate');
                type: 'candidate',
                candidate: event.candidate

    let onAddStream = (event) => {
        console.log('Add stream');
        remoteStreamElement.srcObject =;

    let handleSignalingData = (data) => {
        // let msg = JSON.parse(data);
        switch (data.type) {
            case 'offer':
                pc.setRemoteDescription(new RTCSessionDescription(data));
            case 'answer':
                pc.setRemoteDescription(new RTCSessionDescription(data));
            case 'candidate':
                pc.addIceCandidate(new RTCIceCandidate(data.candidate));

    // Start connection

Also i use this code for client as

When two people are in the connection, everything works great. But as soon as a third user tries to connect to them, the streaming stops with an error

Uncaught (in promise) DOMException: Failed to execute ‘setRemoteDescription’ on ‘RTCPeerConnection’: Failed to set remote answer sdp: Called in wrong state: stable

I don’t have much knowledge of javascript, so I need your help. Thanks.

P.S. I see this error in all browsers.


The reason why you are getting this error message is because when a third user joins, it sends an offer to the 2 previously connected users, and therefore, it receives 2 answers. Since one RTCPeerConnection can only establish one peer-to-peer connection, it will complain when it tries to setRemoteDescription on the answer that arrived later, because it already has a stable connection with the peer whose SDP answer arrived first. To handle multiple users, you will need to instantiate a new RTCPeerConnection for every remote peer.

That said, you can manage multiple RTCPeerConnections using some sort of dictionary or list structure. Through your signalling server, whenever a user connects you can emit a unique user id (could be the socket id). When receiving this id, you just instantiate a new RTCPeerConnection and map the received id to the newly created peer connection and then when you will have to setRemoteDescription on all entries of your data structure.

This would also remove the memory leaks in your code every time a new user joins when you overwrite the peer connection variable ‘pc’ when it is still in use.

Notice though, that this solution is not scalable at all, since you are going to be creating new peer connections exponentially, with ~6 the quality of your call will already be terrible. If your intention is to have a conference room, you should really look into using an SFU, but be aware that usually, it is quite cumbersome to set it up.

Checkout Janus videoroom plugin for an open-source SFU implementation.

Source: stackoverflow