Coder Social home page Coder Social logo

Comments (5)

alvestrand avatar alvestrand commented on June 1, 2024

As I understand:

LOCAL peer can create SDP, collect all ICE Candidates, and send all at once to REMOTE peer over Signaling Server. REMOTE peer know SDP offer and ICE Candidates -> can send SDP answer over WebRTC.

No, it doesn't work that way.
The REMOTE peer knows the SDP offer and the ICE candidates, so it can send ICE candidates to the LOCAL peer.
But those ICE candidates do not contain the SDP - in particular, they can't send the FINGERPRINT attribute which is critical to establishing the DTLS connection.

People have speculated about the possibility of including the necessary information in the ICE handshake, but so far, nobody (as far as I know) has published a working example of this approach, or undertaken the necessary work of standardization that would be needed to make it part of the official WebRTC spec.

from samples.

AlexeyBoiko avatar AlexeyBoiko commented on June 1, 2024

@alvestrand thank you. Could please clatify:

Imagin text chat app. Users can connect to chat by link.
To send messages to all users every user must have Peer2Peer WebRTC connection with all users. It is many-to-many.
When new user connect to chat, all current users must receive the new user's SDP. The only way current users can get new user's SDP is by using Signling Server.

Thus: every user must have alive socket connection with Signling Server. Socket connection must be alive all time user in chat.
If all users have full time alive socket connection - what's the benefit of WebRTC ?

from samples.

alvestrand avatar alvestrand commented on June 1, 2024

from samples.

fippo avatar fippo commented on June 1, 2024

The answer is simple: for that use-case WebRTC offers little benefit. Those come when you add requirements like file transfer and voice/video chat which the signaling/chat server can't handle for capacity reasons.

from samples.

AlexeyBoiko avatar AlexeyBoiko commented on June 1, 2024

@alvestrand @fippo thank you. I know my questions is not an issue report. But it is the only place where I can get answers.

Code below exchange only SDP between peers. It does not send ICE candidates. And its working,

LOCAL                      Singnaling    REMOTE
peer                       server       peer
  |                          |             |
create SDP offer             |             |
  |                          |             |
create ICE candidate 1       |             |
create ICE candidate 2       |             |
(just subscribe              |             |
onicecandidate,              |             |
wait for last ICE            |             |
don't send ICEs)             |             |
  |                          |             |
send SDP only without ICEs-->|------------>|
  |                          |     setRemoteSdp(SDP offer)
  |                          |     create SDP answer
  |<-------------------------|<----send SDP answer without ICEs
setRemoteSdp(SDP answer)                   |
  |                                        |
  |<---------Peer2Peer over WebRTC-------->|

Signaling server mock

const signaling = {
	/** @param {string} senderId,  @param {RTCSessionDescription} sdp */
	sdpAndIceChannelsSend: (senderId, sdp) => {
		console.log(`${senderId} SDP send`);
		document.dispatchEvent(new CustomEvent('sdp', {detail: { senderId, sdp } }));
	},

	/** @param {string} clientId,  @param {(sdp:RTCSessionDescription)=>void} callBack */
	sdpAndIceChannelsOnGet: (clientId, callBack) =>
		document.addEventListener('sdp', evt => {
			if (evt.detail.senderId !== clientId) {
				console.log(`${clientId} SDP onGet`);
				callBack(evt.detail.sdp);
			}
		}),
};

code:

//
// remote peer
{
	const remoteClientId = 'REMOTE';

	const remoteCon = new RTCPeerConnection();
	remoteCon.ondatachannel = evt => {
		console.log('REMOTE ondatachannel');
		const channel = evt.channel;
		channel.onopen = (event) => {
			console.log('REMOTE channel onopen');

			// send message to LOCAL
			const msg = 'Hi from remote';
			console.log(`REMOTE message send: ${msg}`);
			channel.send(msg);
		};
		channel.onmessage = (event) => {
			console.log('REMOTE onmessage:', event.data);
		};
	};

	signaling.sdpAndIceChannelsOnGet(remoteClientId, async (sdpOffer, iceCandidates) => {
		await remoteCon.setRemoteDescription(sdpOffer);
		await remoteCon.setLocalDescription();

		signaling.sdpAndIceChannelsSend(remoteClientId, remoteCon.localDescription);
	});
}

//
// local peer
{
	const localClientId = 'LOCAL';

	const localCon = new RTCPeerConnection();
	const localChannel = localCon.createDataChannel('chat');
	localChannel.onopen = evt => {
		console.log('LOCAL channel onopen');

		// send message to REMOTE
		const msg = 'Hi from local';
		console.log(`LOCAL message send: ${msg}`);
		localChannel.send(msg);
	};
	localChannel.onmessage = evt =>
		console.log('LOCAL onmessage:', evt.data);	

	// SDP
	// creates and sets SDP
	await localCon.setLocalDescription();

	signaling.sdpAndIceChannelsOnGet(localClientId, async sdpAnswer =>
		await localCon.setRemoteDescription(sdpAnswer));

	localCon.onicecandidate = evt => {
		if (!evt.candidate) {
			// all ice candidates getted			
			signaling.sdpAndIceChannelsSend(localClientId, localCon.localDescription);
		}
	};
}

Console ouput:

LOCAL SDP send
REMOTE SDP onGet
REMOTE SDP send
LOCAL SDP onGet
LOCAL channel onopen
LOCAL message send: Hi from local
REMOTE ondatachannel
REMOTE channel onopen
REMOTE message send: Hi from remote
REMOTE onmessage: Hi from local
LOCAL onmessage: Hi from remote

Why this code work if ICEs don't exchange?

from samples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.