<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Luca Girotti </title>
    <description>The latest articles on DEV Community by Luca Girotti  (@iluca).</description>
    <link>https://dev.to/iluca</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1027307%2F52a8967c-6511-49af-a28f-4c33b9eb4254.jpg</url>
      <title>DEV Community: Luca Girotti </title>
      <link>https://dev.to/iluca</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iluca"/>
    <language>en</language>
    <item>
      <title>Unhandled Rejection (TypeError): "function" is not a function</title>
      <dc:creator>Luca Girotti </dc:creator>
      <pubDate>Fri, 17 Feb 2023 12:13:40 +0000</pubDate>
      <link>https://dev.to/iluca/unhandled-rejection-typeerror-function-is-not-a-function-3c24</link>
      <guid>https://dev.to/iluca/unhandled-rejection-typeerror-function-is-not-a-function-3c24</guid>
      <description>&lt;p&gt;I am pretty new to react and I just started using it. I am currently making this video calling web app with both webRTC an react and I am currently facing this issue:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjit228qxj6vhnhw4yrkj.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjit228qxj6vhnhw4yrkj.PNG" alt="Unhandled Rejection (TypeError): sendStream is not a function" width="800" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is where I am using/creating the specific "sendStream" function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useMemo, useEffect, useState, useCallback } from "react";

const peerContext = React.createContext(null);

export const usePeer = () =&amp;gt; React.createContext(null);

    export const PeerProvider = (props) =&amp;gt; {
            const [remoteStream, setRemoteStream] = useState(null);
            const peer = useMemo(() =&amp;gt; 
            new RTCPeerConnection({
                iceServers: [
                    {
                        urls: [
                            "stun:stun.l.google.com:19302",
                            "stun:global.stun.twilio.com:3478",
                        ],
                    },
                ],
            }),
        []
    );

    const createOffer = async() =&amp;gt; {
        const offer = await peer.createOffer();
        await peer.setLocalDescription(offer);
        return offer;
    };      

    const createAnswer = async (offer) =&amp;gt; {
        await peer.setRemoteDescription(offer);
        const answer = await peer.createAnswer();
        await peer.setLocalDescription(answer);
        return answer;
    };

    const setRemoteAns = async(ans) =&amp;gt;{
        await peer.setRemoteDescription(ans);
    };

    const sendStream = async(stream) =&amp;gt; {
        const tracks = stream.getTracks();
        for(const track of tracks){
            peer.addTrack(track,stream);
        }
    };
**--&amp;gt;here !**

    const handleTrackEvent = useCallback((ev) =&amp;gt;{
        const streams = ev.streams;
        setRemoteStream(streams[0]);
    }, [])

    useEffect(() =&amp;gt; {
        peer.addEventListener("track",handleTrackEvent);
        return () =&amp;gt;{
            peer.removeEventListener("track",handleTrackEvent)
        }
    },[handleTrackEvent,peer])

    return(
    &amp;lt;peerContext.Provider value={{ peer, createOffer, createAnswer, setRemoteAns, sendStream,remoteStream}}&amp;gt;{props.children}&amp;lt;/peerContext.Provider&amp;gt;
    );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;--&amp;gt; Where it is created&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, {useEffect, useCallback, useState} from 'react';
import  ReactPlayer from "react-player";
import { useSocket} from "../providers/Socket";
import { usePeer } from "../providers/Peer";


const SessionPage = () =&amp;gt; {
    const { socket } = useSocket();
    **const { peer, createOffer, createAnswer,setRemoteAns, sendStream,remoteStream } = usePeer();**

    const[myStream,setMyStream] = useState(null);

    const handleNewUserJoined = useCallback(
        async(data) =&amp;gt;{
        const {userID} = data
        console.log("New user joined the session",userID);
        const offer =  await createOffer();
        socket.emit('call-user',{ userID, offer })
        },
        [createOffer,socket]
    );

    const handleIncommingCall = useCallback( async(data) =&amp;gt; {
        const {from, offer} = data;
        console.log("Incomming Call from", from, offer);
        const ans = await createAnswer(offer);
        socket.emit("call-accepted",{userID: from, ans})
    }, 
    [createAnswer, socket] );


    const handleCallAccepted = useCallback(async(data) =&amp;gt; {
        const {ans} = data;
        console.log("Call Got Accepted",ans);
        await setRemoteAns(ans);

    }, [setRemoteAns]);

 **   const getUserMediaStream = useCallback(async() =&amp;gt; {
        const stream = await navigator.mediaDevices.getUserMedia({audio: true, video: true});
        sendStream(stream);
        setMyStream(stream);
    }, [sendStream]);**


    useEffect(() =&amp;gt; {
        socket.on("user-joined",handleNewUserJoined);
        socket.on("incomming-call",handleIncommingCall);
        socket.on("call-accepted",handleCallAccepted);


        return () =&amp;gt;{
            socket.off("user-joined",handleNewUserJoined);
            socket.off("incomming-call", handleIncommingCall);
            socket.off("call-accepted",handleCallAccepted);
        };
    }, [handleCallAccepted,handleIncommingCall, handleNewUserJoined, socket]);

    useEffect(() =&amp;gt; {
        getUserMediaStream();
    },[])



    return(
        &amp;lt;div className='session-page-container'&amp;gt;
            &amp;lt;h1&amp;gt;Hi mom, Im on TV :D&amp;lt;/h1&amp;gt;
            &amp;lt;ReactPlayer url={myStream} playing muted/&amp;gt;
            &amp;lt;ReactPlayer url={remoteStream} playing/&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}

export default SessionPage;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;--&amp;gt;Where it is used&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I don't really know how to handle and fix this, any help?&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
