peerjs icon indicating copy to clipboard operation
peerjs copied to clipboard

Open event never fires for connection

Open xylude opened this issue 4 years ago • 7 comments

When I initiate a connection, the receiving end gets the event but the sending side never actually opens. There are no errors or anything, and the websocket traffic looks like it's working just fine. Just no 'open' event ever. I did have it working yesterday but today it just stopped for some reason.

Here is the code I'm using to send and receive:

Sending:

function initiateConnection() {
        setConnectionState(1);
        const _connection = peer.connect(code);

        setConnection(_connection);

        _connection.on('open', () => {
            // never happens!
            setConnectionState(2);
        });
        _connection.on('error', e => {
            alert(e.message);
        });
        _connection.on('close', () => {
            setConnectionState(0);
        });
        _connection.on('data', (data) => {
            console.log(data);
        })
    }

Receiving ('connection' event fires when the other machine calls initiateConnection()):

 peer.on('connection', connection => {
            setConnected(true);
            connection.on('data', chunk => {
                console.log(chunk);
            })
            connection.on('error', e => {
                alert(e.message);
            });
            connection.on('close', () => {
                setConnected(false);
            })
        })

xylude avatar Dec 26 '21 13:12 xylude

Wild guess here.... I've found that connections will work perfectly when both sides are only using 'stun' as soon as one side has to use 'turn' then connections don't materialize unless the turn server is operating and is configured properly. If you are using someone else's turn server I would spend the time to set up your own.

rushglen avatar Dec 27 '21 00:12 rushglen

Thanks, I'll give that a try this afternoon and report back if it fixes things for me. I was using the default server that the library uses, so this might not solve the issue though.

xylude avatar Dec 27 '21 10:12 xylude

If you want to run your own stun and turn server its done in less then 10 minutes with Coturn, https://github.com/coturn/coturn

For an example configuration check out https://pastebin.com/tAnnRZNs

Testing your server can be done here: https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/

newmediacrew avatar Dec 27 '21 15:12 newmediacrew

I am having the same issue, which I referenced here : #899

Browser I am using is MS Edge

TotoShampoin avatar Jan 02 '22 21:01 TotoShampoin

@xylude please reopen the issue if it still exists for you after checking with custom STUN/TURN.

afrokick avatar Feb 20 '22 12:02 afrokick

It happened with me today. for me the problem was that I was creating two peers with the same ID. changing that solved this for me.

mind0bender avatar Sep 29 '22 20:09 mind0bender

Faced the same issue, turns out that the way I set up my code it was taking a lot of time before the event listeners are attached after the peer instance initialization, that by the time they were finally attached the open event would have already fired.

HatunaPatata avatar Jul 17 '24 08:07 HatunaPatata