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

need to add breakpoint or setTimeout method to get it work #56

Open
monthAndEclipse opened this issue Mar 12, 2021 · 5 comments
Open

need to add breakpoint or setTimeout method to get it work #56

monthAndEclipse opened this issue Mar 12, 2021 · 5 comments

Comments

@monthAndEclipse
Copy link

I run the code locally, use chrome to vist http://localhost:3000/ , and it redirect to the room address 'http://localhost:3000/fe3b88cb-8749-44a4-ab6e-aed79bd23fe4' .
then I open another tab in incognito mode with the same room address, but I can only see one video element in each tab .

F12, open my devtool , and add breakpoint on this line (in both two tab) ,refresh one of the page.

  socket.on('user-connected', userId => {
   connectToNewUser(userId, stream) //add breakpoint 
  })

the program stop at this line ,I resume. then there are two video element, it works!

I was thinking there migth be a race problem. the connectToNewUser get executed before the myPeer.on('call') , so I add a setTimeout method.

  socket.on('user-connected', userId => {
    setTimeout(connectToNewUser,1000,userId,stream)
  })
function connectToNewUser(userId, stream) {
  //send my own stream to other user
  const call = myPeer.call(userId, stream)
  const video = document.createElement('video')
  //receive remotestream from someone else 
  call.on('stream', userVideoStream => {
    addVideoStream(video, userVideoStream)
  })
  call.on('close', () => {
    video.remove()
  })

  peers[userId] = call
}

and this time , without adding the breakpoint , there are two video elements in each tab.

does anyone have any ideas about what's happening here or is there a better solution in this case?
cause it seems setTimeout it's a bad practice .

@ArmanHZ
Copy link

ArmanHZ commented Mar 14, 2021

Thanks. I was also facing the same problem. The myPeer.on("call") methods was also not getting triggered so I taught there was a problem with PeerJS.

This fixes the issue.

@ssshake
Copy link

ssshake commented Apr 5, 2021

I believe the issue is a race condition between then the peer connection is ready and when the webcam/usermedia promise resolved.

Because of this, the other peers try to call the new users before they've subscribed to the on.call event.

A cheap solution is moving the entire user media promise chain into the on.open event.

myPeer.on('open', id => {
    navigator.mediaDevices.getUserMedia({
        video: true,
        audio: true
      }).then(stream => {
          console.log("__Webcam Ready")
          
          addVideoStream(myVideo, stream)
      
        myPeer.on('call', call => {
          console.log("INCOMING CALL")
          call.answer(stream)
          const video = document.createElement('video')
          call.on('stream', userVideoStream => {
            addVideoStream(video, userVideoStream)
          })
        })
      
        socket.on('user-connected', userId => {
          connectToNewUser(userId, stream)
        })

        console.log("__Peer Open")
        socket.emit('join-room', ROOM_ID, id)
      })    
})

@antonio-leblanc
Copy link

antonio-leblanc commented May 11, 2021

Thanks. I was facing the same problem, your Timeout solution fixed it :)

  socket.on('user-connected', userId => {
    setTimeout(connectToNewUser,1000,userId,stream)
  })

@igorumeda
Copy link

I believe the issue is a race condition between then the peer connection is ready and when the webcam/usermedia promise resolved.

Because of this, the other peers try to call the new users before they've subscribed to the on.call event.

A cheap solution is moving the entire user media promise chain into the on.open event.

myPeer.on('open', id => {
    navigator.mediaDevices.getUserMedia({
        video: true,
        audio: true
      }).then(stream => {
          console.log("__Webcam Ready")
          
          addVideoStream(myVideo, stream)
      
        myPeer.on('call', call => {
          console.log("INCOMING CALL")
          call.answer(stream)
          const video = document.createElement('video')
          call.on('stream', userVideoStream => {
            addVideoStream(video, userVideoStream)
          })
        })
      
        socket.on('user-connected', userId => {
          connectToNewUser(userId, stream)
        })

        console.log("__Peer Open")
        socket.emit('join-room', ROOM_ID, id)
      })    
})

God

@KsHuyZ
Copy link

KsHuyZ commented Jan 21, 2022

I believe the issue is a race condition between then the peer connection is ready and when the webcam/usermedia promise resolved.

Because of this, the other peers try to call the new users before they've subscribed to the on.call event.

A cheap solution is moving the entire user media promise chain into the on.open event.

myPeer.on('open', id => {
    navigator.mediaDevices.getUserMedia({
        video: true,
        audio: true
      }).then(stream => {
          console.log("__Webcam Ready")
          
          addVideoStream(myVideo, stream)
      
        myPeer.on('call', call => {
          console.log("INCOMING CALL")
          call.answer(stream)
          const video = document.createElement('video')
          call.on('stream', userVideoStream => {
            addVideoStream(video, userVideoStream)
          })
        })
      
        socket.on('user-connected', userId => {
          connectToNewUser(userId, stream)
        })

        console.log("__Peer Open")
        socket.emit('join-room', ROOM_ID, id)
      })    
})

It's working with me, but create 1 video undefined

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

No branches or pull requests

6 participants