Skip to content

Commit 9c72045

Browse files
bugfix: order of event listener in webRTC (#24)
1 parent 5f4643e commit 9c72045

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

demo/WebRTC/src/screens/HomeComponent/HomeScreen.tsx

+34-34
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ export const HomeScreen: React.FC<Props> = () => {
9595
// init PeerConnection
9696
const peerConnection = new RTCPeerConnection(peerConstraints)
9797

98+
// get location of connection between requester and current user
99+
const connectionsCollection = roomRef.collection(FirestoreCollections.connections)
100+
const connectionRef = connectionsCollection.doc(`${data.requester}-${createdUserName}`)
101+
102+
// create answerCandidates collection
103+
const answerCandidatesCollection = connectionRef.collection(
104+
FirestoreCollections.answerCandidates,
105+
)
106+
98107
// add current user's stream to created PC (Peer Connection)
99108
localStream?.getTracks().forEach(track => {
100109
peerConnection.addTrack(track, localStream)
@@ -114,9 +123,12 @@ export const HomeScreen: React.FC<Props> = () => {
114123
})
115124
})
116125

117-
// get location of connection between requester and current user
118-
const connectionsCollection = roomRef.collection(FirestoreCollections.connections)
119-
const connectionRef = connectionsCollection.doc(`${data.requester}-${createdUserName}`)
126+
// add current user's ICE Candidates to answerCandidates collection
127+
peerConnection.addEventListener('icecandidate', event => {
128+
if (event.candidate) {
129+
answerCandidatesCollection.add(event.candidate.toJSON())
130+
}
131+
})
120132

121133
// get data from requester-user's connection
122134
const connectionData = (await connectionRef.get()).data()
@@ -136,18 +148,6 @@ export const HomeScreen: React.FC<Props> = () => {
136148
}
137149
await connectionRef.update({answer})
138150

139-
// create answerCandidates collection
140-
const answerCandidatesCollection = connectionRef.collection(
141-
FirestoreCollections.answerCandidates,
142-
)
143-
144-
// add current user's ICE Candidates to answerCandidates collection
145-
peerConnection.addEventListener('icecandidate', event => {
146-
if (event.candidate) {
147-
answerCandidatesCollection.add(event.candidate.toJSON())
148-
}
149-
})
150-
151151
// collect Offer's ICE candidates from offerCandidates collection and add in PC
152152
connectionRef
153153
.collection(FirestoreCollections.offerCandidates)
@@ -191,9 +191,22 @@ export const HomeScreen: React.FC<Props> = () => {
191191
},
192192
}))
193193

194+
// init participant's remoteStream to add track data from Peer Connection later
195+
setRemoteStreams(prev => ({
196+
...prev,
197+
[participant.name]: new MediaStream([]),
198+
}))
199+
194200
// init peer connection
195201
const peerConnection = new RTCPeerConnection(peerConstraints)
196202

203+
// create connection between current user and participant
204+
const connectionsCollection = roomRef.collection(FirestoreCollections.connections)
205+
const connectionRef = connectionsCollection.doc(`${createdUserName}-${participant.name}`)
206+
207+
// create offerCandidates collection
208+
const offerCandidatesCollection = connectionRef.collection(FirestoreCollections.offerCandidates)
209+
197210
// add current user's stream to created PC (Peer Connection)
198211
localStream?.getTracks().forEach(track => {
199212
peerConnection.addTrack(track, localStream)
@@ -213,15 +226,12 @@ export const HomeScreen: React.FC<Props> = () => {
213226
})
214227
})
215228

216-
// init participant's remoteStream to add track data from Peer Connection later
217-
setRemoteStreams(prev => ({
218-
...prev,
219-
[participant.name]: new MediaStream([]),
220-
}))
221-
222-
// create connection between current user and participant
223-
const connectionsCollection = roomRef.collection(FirestoreCollections.connections)
224-
const connectionRef = connectionsCollection.doc(`${createdUserName}-${participant.name}`)
229+
// add current user's ICE Candidates to offerCandidates collection
230+
peerConnection.addEventListener('icecandidate', event => {
231+
if (event.candidate) {
232+
offerCandidatesCollection.add(event.candidate.toJSON())
233+
}
234+
})
225235

226236
// create offer SDP and set localDescription
227237
const offerDescription = await peerConnection.createOffer(sessionConstraints)
@@ -249,16 +259,6 @@ export const HomeScreen: React.FC<Props> = () => {
249259
}
250260
})
251261

252-
// create offerCandidates collection
253-
const offerCandidatesCollection = connectionRef.collection(FirestoreCollections.offerCandidates)
254-
255-
// add current user's ICE Candidates to offerCandidates collection
256-
peerConnection.addEventListener('icecandidate', event => {
257-
if (event.candidate) {
258-
offerCandidatesCollection.add(event.candidate.toJSON())
259-
}
260-
})
261-
262262
// add listener to answerCandidates collection to participant's ICE Candidates
263263
connectionRef.collection(FirestoreCollections.answerCandidates).onSnapshot(iceCandidatesSnapshot => {
264264
iceCandidatesSnapshot.docChanges().forEach(async change => {

0 commit comments

Comments
 (0)