peerjs
peerjs copied to clipboard
peer.on('calll') is never being called
Peer js is not working
I am just creating peerjs video streaming application in NodeJs and ReactJs
The below code is working fine, i am able to create new peer and open
event is also working.
const peer = new Peer(undefined,{
host:"/",
port:"5001"
});
peer.on('open',id=>{
socket.emit('join-room', roomId,id);
})
On server side whenever 'join-room'
event is emitted, server will emit another event 'user-disconnected'
and passes userId (peerjs)
to client.
// server.js
socket.on('join-room',(roomId,userId)=>{
console.log(`user ${userId} joined ${roomId}`);
socket.join(roomId);
socket.to(roomId).emit('user-connected',userId);
socket.on('disconnect', () => {
socket.to(roomId).emit('user-disconnected', userId)
})
})
Whenever 'user-connected'
is triggered on client side I'm calling connectToNewUser
and up to this is working fine.
socket.on('user-connected',userId=>{
console.log("New user connected...")
connectToNewUser(userId, stream)
});
Error
This is being logged on console console.log('connectToNewUser',1222.....)
there is no error.
But, this call.on('stream')
is never being called
connectToNewUser(userId, stream) {
console.log('connectToNewUser',userId)
const call = peer.call(userId, stream);
const video = getVideo();
call.on('stream', userVideoStream => {
// never called
console.log('connectToNewUser','on','stream')
addVideoStream(video, userVideoStream)
});
call.on('close', () => {
video.remove()
})
peers[userId] = call
}
The reason of call.on('stream')
is never being called is peer.on('call')
is never being called.
peer.on('call', call => {
// never called
console.log('This peer is being called...');
call.answer(stream)
const video = getVideo();
call.on('stream', userVideoStream => {
console.log('This peer is being called...on-stream...');
addVideoStream(video, userVideoStream)
})
});
same here!
@marceloF5 What could be the issue, or should we go with pure WebRTC?
Same issue here !
I think this is not an active repository
We should go with pure WebRTC.
same here
Did a bit of digging:
If you enable logging with new Peer('someid', { debug: 3 })
you can see that the following message appears after a connection was closed (by closing the browser of the connecting peer):
PeerJS: iceConnectionState changed to disconnected on the connection with ...
The message is written here: https://github.com/peers/peerjs/blob/cfc37c7988d8ef3d2c1d7b6123562dd2af59defc/lib/negotiator.ts#L108
Also interesting: https://github.com/peers/peerjs/commit/72e7c176c8f3e43f5524cef4988d71753249c4c0 apparently removed closing the connection in this case. This might be the issue.
It might be possible to fix the issue by listening to the iceStateChanged
event:
conn.on('iceStateChanged', () => {
console.log('iceStateChanged')
conn.close()
})
Nope, wrong, iceStateChanged
is also fired when the connection is established. Hmmm... will continue investigating.
My logic for handling disconnects is like this:
private attachToConnCloseEvents(conn: DataConnection, fn: () => void) {
conn.on('close', () => fn());
// @ts-ignore
conn.on('iceStateChanged', (status: RTCIceConnectionState) => {
if (status === 'disconnected') {
fn();
}
});
}
The logic that emits this event is in negotiator.ts
If your iceConnectionState
is disconnected
, the connection may never come back, so it is a good event to handle for most applications. I have never seen an instance where the connection did come back, but maybe I am impatient, the mozilla docs:
disconnected Checks to ensure that components are still connected failed for at least one component of the RTCPeerConnection. This is a less stringent test than failed and may trigger intermittently and resolve just as spontaneously on less reliable networks, or during temporary disconnections. When the problem resolves, the connection may return to the connected state.
It seems like, DataConnectionEvents
should also include the following header
type DataConnectionEvents = {
// ...
iceStateChanged: (status: RTCIceConnectionState)
}