Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server receives CANDIDATE message from client, but never responds to it #864

Closed
karmeleon opened this issue Jun 23, 2021 · 7 comments
Closed
Labels
Milestone

Comments

@karmeleon
Copy link

karmeleon commented Jun 23, 2021

I'm closely following the example project by initializing a peer on both the client and the server in my app, then using peer.on('connection', () => console.log('connection received')) on the server to listen for connections and peer.connect(serverId) on the client to attempt a connection. The client appears to be working correctly, since it sends an offer to the server and outputs a lot of data to the console:

PeerJS:  Creating RTCPeerConnection.
peerjs.min.js:formatted:1 PeerJS:  Listening for ICE candidates.
peerjs.min.js:formatted:1 PeerJS:  Listening for data channel
peerjs.min.js:formatted:1 PeerJS:  Listening for remote stream
peerjs.min.js:formatted:1 PeerJS:  add connection data:dc_fytb6plg2ks to peerId:rtckaraoke-JPCH
peerjs.min.js:formatted:1 PeerJS:  Created offer.
peerjs.min.js:formatted:1 PeerJS:  Set localDescription: RTCSessionDescription {type: "offer", sdp: "v=0\r\no=- 6234492669936511433 2 IN IP4 127.0.0.1\r\ns…:0\r\na=sctp-port:5000\r\na=max-message-size:262144\r\n"} for:rtckaraoke-JPCH
peerjs.min.js:formatted:1 PeerJS:  Received ICE candidates for rtckaraoke-JPCH: RTCIceCandidate {candidate: "candidate:1860011601 1 udp 2113937151 5a87baf9-1bd…typ host generation 0 ufrag yYzs network-cost 999", sdpMid: "0", sdpMLineIndex: 0, foundation: "1860011601", component: "rtp", …}
peerjs.min.js:formatted:1 PeerJS:  Socket open
peerjs.min.js:formatted:1 PeerJS:  Server message received: {type: "OPEN"}
peerjs.min.js:formatted:1 PeerJS:  Received ICE candidates for rtckaraoke-JPCH: RTCIceCandidate {candidate: "candidate:842163049 1 udp 1677729535 148.64.106.15… rport 0 generation 0 ufrag yYzs network-cost 999", sdpMid: "0", sdpMLineIndex: 0, foundation: "842163049", component: "rtp", …}

Looking in the Network tab of the server's window, I can see that it receives the CANDIDATE message and it even logs it to the console, but it doesn't send any response or have any other indication that it cares about the message it received. The server never fires the peer.on('connection') event, and the client never fires the connection.on('open') event. Both windows are on the same computer, and I've tried Edge, Firefox, and Safari across a Mac and Windows machine and gotten the same results. I'm using a local PeerJS server instance, since when I try to use the public instance the server never receives the CANDIDATE message.

The code for the server starts here, the client's code is here, and you can play with the result live here. I've been at this for hours and have gotten nowhere, so I'd really appreciate any help I can get. Thanks!

EDIT: I should add that years ago this exact same code worked (with the public server, too, and version 1.0.0 of the library), but cloning the old repo again and running it gave the same broken result. I'm fairly confident my own code is working correctly here, so the issue likely lies in either the server, or changes in browsers or the OS that subtly broke something important.

@ChrisAcrobat
Copy link

I also face this problem. But CANDIDATE is only visible on my client-peer log, my host-peer only logs this:

PeerJS:  Socket open
node_modules\peerjs\dist\peerjs.min.js:48 PeerJS:  Server message received: {type: "OPEN"}
p2p.service.ts:42 My peer ID is: 13b9f269-5829-4f75-95f7-56b86fde0d11

But that is maybe the same for you.

@ChrisAcrobat
Copy link

I just debugged the working app, and what I found that it's (peer-client) loggs appears in a different order then mine.

PeerJS:  Socket open
peerjs.js:7144 PeerJS:  Server message received: {type: "OPEN"}
peerjs.js:7144 PeerJS:  Creating RTCPeerConnection.
peerjs.js:7144 PeerJS:  Listening for ICE candidates.
peerjs.js:7144 PeerJS:  Listening for data channel
peerjs.js:7144 PeerJS:  Listening for remote stream
peerjs.js:7144 PeerJS:  add connection data:dc_n9tbm4x4g0f to peerId:b38da1aa-3c98-4e30-9963-9e9bed382d68
peerjs.js:7144 PeerJS:  Created offer.

In my project the first two rows appear last, the same as @karmeleon.

PeerJS:  Socket open
peerjs.js:7144 PeerJS:  Server message received: {type: "OPEN"}

Could that be the problem? That socket is not opened in time?

@ChrisAcrobat
Copy link

ChrisAcrobat commented Jul 8, 2021

I think that's it. I have added a few setTimeouts, and now it's working. I will troubleshoot it some more and write a (for me) working solution.

// This does not work.
let peer = new Peer();
peer.connect(id);
// This works.
let peer = new Peer();
setTimeout(()=>peer.connect(id), 1000);

I have currently a few more setTimeout, but I removed them for readability.

@ChrisAcrobat
Copy link

ChrisAcrobat commented Jul 9, 2021

I think I have solved it now. Wait until peer.on('open')' is executed, then you can connect. As I mentioned before: it seams to be a racing problem.
⚠ Warning: This will spam the console log.

function createPeer(id, callback=()=>{}){
	let peer = new Peer(id.toLocaleLowerCase());
	peer.on('open', (ID:string)=>{
		console.log('My peer ID is: ' + ID);
		callback();
	});
	return peer;
}
function host(id) {
	let peer = this.createPeer(id, ()=>{
		peer.on('connection', (connection)=>{
			connection.on('data', (data)=>{
				console.log(data);
				connection.send('good day?');
			});
		});
	});
}
function client(id) {
	let peer = this.createPeer('', ()=>{
		const connection = peer.connect(id);
		connection.on('open', ()=>{
			connection.send('hi!');
			connection.on('data', (data)=>{
				console.log(data);
				connection.send('good day!');
			});
		});
	});
}

@karmeleon
Copy link
Author

Great find, this fixes the issue for me too! Maybe the docs should be updated to point this out?

@afrokick
Copy link
Member

It's probably a bug, because it should works without waiting for the open event(the outgoing messages should be queued).

I'll check it and write a test.

@afrokick afrokick added the bug label Feb 20, 2022
@afrokick afrokick added this to the 2.0.0 milestone Feb 20, 2022
afrokick added a commit that referenced this issue Feb 20, 2022
@afrokick
Copy link
Member

Fixed in the latest beta.

npm i peerjs@beta

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants