Skip to content
Advertisement

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

I am trying to write a WebRTC application using socket.io.

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

JavaScript

The client side looks like this

JavaScript

Also i use this code for client as socket.io

https://github.com/socketio/socket.io/blob/master/client-dist/socket.io.js

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.

See this repository

https://github.com/pfertyk/webrtc-working-example

See this instructions

https://pfertyk.me/2020/03/webrtc-a-working-example/

Advertisement

Answer

I’ve answered this question above in details as to why you are having this issue. But seems like what you are really looking for is some sample working code on how to fix it… so here you go:

index.html: Slightly update the HTML page, so now we have a div that we will append incoming remote videos.

JavaScript

app.py: updated data and ready event handlers a bit so that we emit the socket id to other peers correctly.

JavaScript

main.js: Created this peers object to map socket ids to RTCPeerConnections and updated some of the functions to use that instead of the pc variable.

JavaScript

I tried to change your code as little as possible, so you should be able to just copy-paste and have it working.

Here’s my working code: https://github.com/lnogueir/webrtc-socketio

If you have any issues running it, let me know or open an issue there and I’ll do my best to help.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement