3

I am trying to const pc = new RTCPeerConnection() while I get ReferenceError: RTCPeerConnection is not defined. How can I overcome this error?

It is not my browsers, I can run webRTC natively on them.

juliomalves
  • 42,130
  • 20
  • 150
  • 146
Hypothesis
  • 1,208
  • 3
  • 17
  • 43

1 Answers1

3

Next.js pre-renders every page on the server. Trying to use Web APIs when the page gets pre-rendered will throw an error like the one you're seeing since those Web APIs are not present in a Node.js environment.

To solve it, make sure you call new RTCPeerConnection() within your component's useEffect so it only gets called on the client-side.

useEffect(() => {
    const pc = new RTCPeerConnection()
    // Rest of your logic here
}, [])
juliomalves
  • 42,130
  • 20
  • 150
  • 146