I'm trying to make a Discord-like application where multiple users can chat and share screens in rooms. While sharing the screen, I combine video media and audio media and call other peer users. However, when I merge, the stream listener runs once again for each media and triggers the methods inside it again. How can I solve this or manage it better? screenShareStarterMethod: async startScreenShare(): Promise<MediaStream> { const screenStream = await navigator.mediaDevices.getDisplayMedia({ video: true, audio: true }); const audioStream = await navigator.mediaDevices.getUserMedia({ audio: true }); const combinedStream = new MediaStream([ ...screenStream.getVideoTracks(), ...audioStream.getAudioTracks(), ]); // Ekran paylaşımı video track'inin 'ended' olayını dinleyin const videoTrack = screenStream.getVideoTracks()[0]; videoTrack.onended = () => { console.log('Screen share stopped.'); this.stopScreenShare(); // Durdurma işlemi yap }; this.myStream = combinedStream; this.callAllConnectedPeers(); console.log('peerIds of Connected users :', this.connectedPeers); this.screenShareStatus.next(true); return this.myStream; } The call method: callAllConnectedPeers() { this.connectedPeers.forEach(peerId => { this.callUser(peerId, this.currentUser?.username || '') }); } callUser: async initMedia() { try { this.myStream = await navigator.mediaDevices.getUserMedia({ audio: { noiseSuppression: true, // Gürültü bastırma echoCancellation: true, // Yankı önleme autoGainControl: true // Otomatik ses ayarı } }); console.log('✅ Media stream initialized'); this.peer.on('call', (call) => { console.log(' Gelen çağrı:', call); if (!this.connectedPeers.includes(call.peer)) { this.connectedPeers.push(call.peer); } const callerUsername = call.metadata?.currentUser || 'Unkown User'; call.answer(this.myStream); call.on('stream', (userStream) => { console.log('Incoming stream tracks:', userStream.getTracks().length); // Generaly 2 tracks one for audio and one for video this.addAudioStream(userStream, call.peer, callerUsername); }); call.on('error', (err) => { console.error('❌ Call error:', err); }); call.on('close', () => { console.warn(' Call closed'); }); }); } catch (error) { console.error('❌ Error accessing media devices:', error); } } Continue reading...