peers / peerjs

Simple peer-to-peer with WebRTC.
https://peerjs.com
MIT License
12.48k stars 1.43k forks source link

As of versio 1.4.6 peerjs can no longer be used in node backend apps #979

Open maxpavlov opened 2 years ago

maxpavlov commented 2 years ago

Hello.

Some change that has happened between versions 1.3.2 and 1.4.6 has cast peerjs unusable in the node scenario.

I understand that this is not an intended use of peerjs but so far it has been working flawlessly with a few tweeks.

But after I've upgraded my node app to use peerjs 1.4.6, I get the following error while trying to establish a data connection:

`peerjs/dist/peerjs.cjs:4215 if (e.candidate) Object.defineProperty(e, 'candidate', { ^

TypeError: Cannot redefine property: candidate at Function.defineProperty () at /snapshot/WarpCLI/node_modules/peerjs/dist/peerjs.cjs:4215:33 at RTCPeerConnection.wrappedCallback (/snapshot/WarpCLI/node_modules/peerjs/dist/peerjs.cjs:536:33) at /snapshot/WarpCLI/node_modules/wrtc/lib/eventtarget.js:37:18 at Set.forEach () at /snapshot/WarpCLI/node_modules/wrtc/lib/eventtarget.js:33:15`

So something has changed that in node scenario, the candidate property on a window object used to be defined so that it could have been redefined later by this code, and now it can't be.

Obviously for node case window object is just a variable in code, an empty javascript object with a few properties that are required for operation of peersjs in that context.

So in 1.3.2 - all works well, in 1.4.6 - broken, given the following error.

I'd appreciate any direction or a hint on how to troubleshoot this.

jonasgloning commented 2 years ago

Hey @maxpavlov,

Our npm package contains different files for different environments. The peerjs.cjs file is for the browser (included via script tag) and the bundler.(m|c)js for use with bundlers under Node. require() should autmatically use the latter.

I'm surprised to see the former show up in the stack trace. The browser version includes a bundled and re-transpiled version of webrtc-adapter, which seems to clash with wrtc.

How do you include PeerJS, and what are your few tweaks? I would like to reproduce this locally. (And I'm sure others are interested in how to use PeerJS on Node).

maxpavlov commented 2 years ago

Hi @jonasgloning.

80% of the story, not the full one as I am using a customized peerj js from an own repo, but I constantly pull in updates from the main peerjs (1.x.x) as an upstream.

But back to your comments:

  1. I tried "requiring" both peer.js directly (as I did previously with version <= 1.3.2, from a dist dir), as well as peerjs.cjs. I belive .cjs one is with "common js" which is basically using require instead of import.

  2. Requiring bundler.cjs broke something in my code and cast me even unable to connect to a peering server thus I abandoned the idea, now I think that is very much connected to my case.

The tweeks that allow me to use peerjs in node app basically are:

One also needs to bring in the web sockets support via "ws" npm package.

Thanks for your comment, I will actually try to play around with requiring the bundler instead of "peerjs.js" directly. Bundler seems to have the stuff I am missing.

maxpavlov commented 2 years ago

So I have tried to require both "bundler.cjs" and "peerjs.js" and the error appears to be the same with the slightly more informative stack trace:

Data connection state changed to new
/snapshot/WarpCLI/node_modules/webrtc-adapter/dist/common_shim.js:76
      Object.defineProperty(e, 'candidate', {
             ^

TypeError: Cannot redefine property: candidate
    at Function.defineProperty (<anonymous>)
    at /snapshot/WarpCLI/node_modules/webrtc-adapter/dist/common_shim.js:76:14
    at RTCPeerConnection.wrappedCallback (/snapshot/WarpCLI/node_modules/webrtc-adapter/dist/utils.js:60:27)
    at /snapshot/WarpCLI/node_modules/wrtc/lib/eventtarget.js:37:18
    at Set.forEach (<anonymous>)
    at /snapshot/WarpCLI/node_modules/wrtc/lib/eventtarget.js:33:15

It seems like the only correct way is to simply require peerjs.js and it includes everything including the stuff from bundler.cjs.

Digging deeper it seems what has changed is the version of the evenemitter3. On Peerjs 1.3.2 it was eventemitter3 v3, and now it's v4. Will continue to investigate.