You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm encountering an issue with my peer-to-peer call functionality in a repository using PeerJS.
I'm facing a problem where I have a call between two users, where one of them is logged in, and the other is just a client who doesn't need an account. The goal is for the logged-in user to assist the non-logged-in user by starting a streaming call. When I test this locally, it works most of the time, about 98% of the time. The connection is established, and they can exchange their screens and microphone data. However, when I deploy it to the server, it doesn't work properly. Sometimes it works, sometimes it doesn't. More than 50% of the time, only one of the peers can connect. Sometimes the other peer can share their screen, sometimes neither of them can share their screen and microphone.
The expected behavior is that when both users enter the call screen, they should automatically share their microphones with each other, and when they want, they should be able to share their screens.
I'll provide the source code for the component, but I won't be able to share the repository as it's private. However, I can share the component's source code.
[Source code for the component goes here]
`import { faArrowUp, faComments, faCommentSlash, faMicrophone, faMicrophoneSlash, faShare, faTimesCircle } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { database } from "faker";
import { Peer } from "peerjs";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Alert, Button, Col, Row } from "react-bootstrap";
import { useNavigate } from "react-router-dom";
import { customConfig } from "../../../beans/turnAndStunServers";
//import Peer, { SignalData } from 'simple-peer';
import Avatar from "../../../components/lists/Avatar";
import { baseURL } from "../../../utils/axios";
import { generateId } from "../../../utils/functions/generateRandomId";
import useSocket from "../../../utils/hooks/useSocket";
import useToken from "../../../utils/hooks/useToken";
import { SupportTicketChat } from "../interfaces/ticket";
useEffect(() => {
//Esse useEffect é o efeito colateral do handleFinaly
//useEffect para fechar a stream quando o atendimento finalizar,
//pois quando o componente desmonta ele não finaliza a chamada
//dessa forma ele finaliza a stream ao desmontar o componente.
I hope this helps clarify my issue with PeerJS in my repository. Any guidance or suggestions on how to improve the reliability of the peer-to-peer connections when deployed to a server would be greatly appreciated!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello everyone! 😃
I'm encountering an issue with my peer-to-peer call functionality in a repository using PeerJS.
I'm facing a problem where I have a call between two users, where one of them is logged in, and the other is just a client who doesn't need an account. The goal is for the logged-in user to assist the non-logged-in user by starting a streaming call. When I test this locally, it works most of the time, about 98% of the time. The connection is established, and they can exchange their screens and microphone data. However, when I deploy it to the server, it doesn't work properly. Sometimes it works, sometimes it doesn't. More than 50% of the time, only one of the peers can connect. Sometimes the other peer can share their screen, sometimes neither of them can share their screen and microphone.
The expected behavior is that when both users enter the call screen, they should automatically share their microphones with each other, and when they want, they should be able to share their screens.
I'll provide the source code for the component, but I won't be able to share the repository as it's private. However, I can share the component's source code.
[Source code for the component goes here]
`import { faArrowUp, faComments, faCommentSlash, faMicrophone, faMicrophoneSlash, faShare, faTimesCircle } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { database } from "faker";
import { Peer } from "peerjs";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Alert, Button, Col, Row } from "react-bootstrap";
import { useNavigate } from "react-router-dom";
import { customConfig } from "../../../beans/turnAndStunServers";
//import Peer, { SignalData } from 'simple-peer';
import Avatar from "../../../components/lists/Avatar";
import { baseURL } from "../../../utils/axios";
import { generateId } from "../../../utils/functions/generateRandomId";
import useSocket from "../../../utils/hooks/useSocket";
import useToken from "../../../utils/hooks/useToken";
import { SupportTicketChat } from "../interfaces/ticket";
interface MainChatStreamProps {
ticketData: SupportTicketChat;
ticket: string;
setCallEnded: Function;
setIsChatVisible: React.Dispatch<React.SetStateAction>;
isChatVisible: boolean;
}
const MainChatStream = ({ ticketData, ticket, setCallEnded, setIsChatVisible, isChatVisible }: MainChatStreamProps) => {
const { socket } = useSocket();
const { user } = useToken();
const navigate = useNavigate();
const [stream, setStream] = useState();
const [isAudioShared, setIsAudioShared] = useState(true);
const [streamRemoto, setStreamRemoto] = useState();
const [isSharing, setIsSharing] = useState(false);
const [peer, setPeer] = useState<Peer | null>(null);
const localVideo = useRef(null);
const remoteVideo = useRef(null);
const [myId, setMyId] = useState<string | undefined>(generateId(6));
const [theirId, setTheirId] = useState('');
const [end, setEnd] = useState(false);
const handleFinaly = useCallback(async () => {
}, [socket]);
useEffect(() => {
//Esse useEffect é o efeito colateral do handleFinaly
//useEffect para fechar a stream quando o atendimento finalizar,
//pois quando o componente desmonta ele não finaliza a chamada
//dessa forma ele finaliza a stream ao desmontar o componente.
}, [end])
useEffect(() => {
if (user) {
socket?.on('signalForAttendant' + ticket, () => {
handleFinaly()
})
} else {
socket?.on('signalForClient' + ticket, () => {
handleFinaly()
})
}
}, [])
useEffect(() => {
});
useEffect(() => {
if (ticketData.status !== 'OPEN') {
handleFinaly();
}
}, [])
useEffect(() => {
}, [])
const shareStream = () => {
};
useEffect(() => {
if (myId && ticketData.status === "OPEN") {
if (!peer) {
setPeer(new Peer(myId, { secure: true, ...customConfig }));
}
if (user) {
socket?.emit("sendIdAttendant", {
uuidSupportTicket: ticket,
id: myId
});
}, [myId]);
useEffect(() => {
if (theirId && ticketData.status === "OPEN") {
}, [peer, myId, theirId])
useEffect(() => {
if (peer && ticketData.status === "OPEN") {
peer.on('call', call => {
call.answer(stream);
call.on('stream', userVideoStream => {
// When we recieve their stream
setStreamRemoto(userVideoStream)
})
call.on('close', () => {
setStreamRemoto(undefined);
})
})
}
}, [peer]);
useEffect(() => {
if (peer && stream && theirId) {
const call = peer.call(theirId, stream); // Call the user who just joined
}, [theirId, stream])
useEffect(() => {
}, [streamRemoto]);
const handleStreamEnded = () => {
if (ticketData.status === "OPEN") {
closeStream()
setIsSharing((prevVal) => !prevVal); // Atualize o estado, se necessário
}
};
async function closeStream(justVideo?: boolean) {
if (stream) {
if (justVideo) {
const tracksVideo = stream.getVideoTracks();
const tracksAudio = stream.getAudioTracks();
tracksVideo.forEach((trackVideo) => trackVideo.stop());
setStream(new MediaStream([...tracksAudio]))
} else {
const tracks = stream.getTracks();
tracks.forEach((track) => track.stop());
setStream(undefined);
if (localVideo?.current) {
localVideo.current.srcObject = null;
}
}
}
useEffect(() => {
if (stream && stream.active) {
console.log(stream)
stream.getTracks()[0].addEventListener("ended", handleStreamEnded);
const videoTracks = stream?.getVideoTracks();
if (videoTracks && videoTracks.length) {
videoTracks[0].addEventListener('ended', handleStreamEnded)
}
}
return () => {
if (stream && stream?.active) {
stream.getTracks()[0].removeEventListener("ended", handleStreamEnded);
}
const videoTracks = stream?.getVideoTracks();
if (videoTracks && videoTracks.length) {
videoTracks[0].removeEventListener('ended', handleStreamEnded)
}
};
}, [stream]);
useEffect(() => {
}, [isAudioShared]);
/useEffect(() => {
if (ticketData.status === "OPEN") {
stream && setIsSharing(Boolean(stream.active));
}
}, [shareStream]);/
const MyInterface = () => (
<Button variant="gray" size="lg" className="mt-2" onClick={() => { setIsAudioShared(!isAudioShared); }}>
<FontAwesomeIcon icon={isAudioShared ? faMicrophone : faMicrophoneSlash} />
<Button
className="mt-2"
variant="gray"
onClick={() => { setIsChatVisible((prevVal) => !prevVal) }}
>
<FontAwesomeIcon icon={isChatVisible ? faCommentSlash : faComments} />
);
return ticketData.status === "OPEN" ? (
) : null;
}
export default MainChatStream;
`
I hope this helps clarify my issue with PeerJS in my repository. Any guidance or suggestions on how to improve the reliability of the peer-to-peer connections when deployed to a server would be greatly appreciated!
Beta Was this translation helpful? Give feedback.
All reactions