peerjs icon indicating copy to clipboard operation
peerjs copied to clipboard

PeerJS not working with React

Open anim8rDev opened this issue 5 years ago • 4 comments

I was excited to get PeerJS working my local developer environment using Gatsby/React. However, when I went to build the website, it didn't work because, come to find out, PeerJs must be trying to access the window.

Does anyone know how to get around this? An alternative that I can try?

Thanks

anim8rDev avatar Mar 31 '20 17:03 anim8rDev

Yes, it's a very annoying dependency which kills server-side rendering (SSR) like on NextJS. I ended up by moving the PeerJS to the 'store' layers, using redux-saga for my experiment, using eventChannels (https://redux-saga.js.org/docs/advanced/Channels.html). I only start the saga when I'm in client-side scope and I have 'window' available.

export default function* rootSaga() {
  // NextJS' webpack config injects process.browser if this snippet is running in the browser
  if (process.browser) {
    yield all([...gameSagas])
  }
}

Good luck!

poison avatar Apr 01 '20 09:04 poison

I managed to get around this by importing react component that uses peerjs with @loadable/component.

gax97 avatar May 06 '20 21:05 gax97

I have created this POC for video calling web-app with react. https://github.com/B-Pratik/call-it hosted on Heroku

B-Pratik avatar Aug 04 '20 18:08 B-Pratik

Yes, it's a very annoying dependency which kills server-side rendering (SSR) like on NextJS. I ended up by moving the PeerJS to the 'store' layers, using redux-saga for my experiment, using eventChannels (https://redux-saga.js.org/docs/advanced/Channels.html). I only start the saga when I'm in client-side scope and I have 'window' available.

export default function* rootSaga() {
  // NextJS' webpack config injects process.browser if this snippet is running in the browser
  if (process.browser) {
    yield all([...gameSagas])
  }
}

Good luck!

Hey :-) long shot I know since it was a long time ago you answered, but I could use some help with running PeerJS in NextJS with Redux Saga. I'm using next-redux-wrapper and all my Sagas run fine, but no matter how I try and use PeerJS it always responds with navigator not found, which I know is a SSR problem, but like I said, I can't stop the PeerJS code running before the browser is present

j-mcgregor avatar Jan 26 '21 03:01 j-mcgregor