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

Can not answer the call when new user join room #18

Open
Khangithub opened this issue Oct 10, 2020 · 7 comments
Open

Can not answer the call when new user join room #18

Khangithub opened this issue Oct 10, 2020 · 7 comments

Comments

@Khangithub
Copy link

i am following their tutorial at this point https://youtu.be/ZVznzY7EjuY?t=6023

Before that, everything worked perfectly, but i can not answer the call when new user join room and their is no new video tag appended in the video grid.

This is my server.js

const express = require('express')
const app = express()
const server = require('http').Server(app)
const io = require('socket.io')(server)
const { v4: uuidv4 } = require('uuid')
const { ExpressPeerServer } = require('peer')
const peerServer = ExpressPeerServer(server, {
  debug: true
})

app.set('view engine', 'ejs')
app.use(express.static('public'))

app.use("/peerjs", peerServer)
app.get('/', (req, res) => {
  res.redirect(`/${uuidv4()}`)
})

app.get('/:room', (req, res) => {
  res.render('room', { roomId: req.params.roomId })
})

io.on('connection', socket => {
  socket.on('join-room', (roomId, userId) => {
    socket.join(roomId)
    socket.to(roomId).broadcast.emit('user-connected', userId)
  })
})

server.listen(3030)

this is my script.js

const socket = io('/')
const videoGrid = document.getElementById('video-grid')
const myVideo = document.createElement('video')
myVideo.muted = true

var peer = new Peer(undefined, {
  path: '/peerjs',
  host: '/',
  port: '3030'
})

let myVideoStream

navigator.mediaDevices
  .getUserMedia({
    video: true,
    audio: true
  })
  .then(stream => {
    myVideoStream = stream
    addVideoStream(myVideo, stream)

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

    peer.on('call', call => {
      call.answer(stream)
      const video = document.createElement('video')
      call.on('stream', userVideoStream => {
        addVideoStream(video, userVideoStream)
      })
    })
  })

peer.on('open', id => {
  socket.emit('join-room', ROOM_ID, id)
})

function connectToNewUser (userId, stream) {
  const call = peer.call(userId, stream)
  const video = document.createElement('video')
  call.on('stream', userVideoStream => {
    addVideoStream(video, userVideoStream)
  })
}

function addVideoStream (video, stream) {
  video.srcObject = stream
  video.addEventListener('loadedmetadata', () => {
    video.play()
  })
  videoGrid.append(video)
}

as i tried to console.log(userMediaStream) it returned nothing in this code block

   peer.on('call', call => {
      call.answer(stream)
      const video = document.createElement('video')
      call.on('stream', userVideoStream => {
        addVideoStream(video, userVideoStream)
      })
    })

i dont know how to deal with it, please tell me why, thank you so much 👍

@tryforjunk273824724
Copy link

Have you found the solution? I have the same problem.

@rockytechtips
Copy link

Change this in script.js

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

with

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

@umangagarwal11
Copy link

umangagarwal11 commented Dec 26, 2020

Change this in script.js

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

with

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

This still does not work
Has anyone found a genuine solution?

@tryforjunk273824724
Copy link

No, the script.js change does not do anything. I do not have a solution.

@engesin
Copy link

engesin commented Jan 16, 2021

just move user-connected and call event out of promise..

final code is as following..

navigator.mediaDevices.getUserMedia({
    video: true,
    audio: false
}).then(stream => {
    myVideoStream = stream;
    addVideoStream(myVideo, stream)

})

peer.on('call', call => {
    console.log("answer");
    call.answer(myVideoStream)
    const video = document.createElement('video')
    call.on('stream', userVideoStream => {
        addVideoStream(video, userVideoStream)
    })
})

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

@markwagdy
Copy link

I had the same problem I solved it by making the Peer constructor empty var peer=new Peer(undefined)

@engesin
Copy link

engesin commented Jan 17, 2021

I had the same problem I solved it by making the Peer constructor empty var peer=new Peer(undefined)

Yes this works also 👍

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