Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/ NodeJS support #928

Merged
merged 2 commits into from
Feb 20, 2022

Conversation

afrokick
Copy link
Member

Test on: NodeJS 14

NOTE1: NodeJS works on this branch only! After release v2, it will be a part of peerjs.

NOTE2: node-fetch must be v2! because in v3 it is es module and it doesn't work for cjs.


Install required npm packages:

npm install peerjs node-fetch@2 ws wrtc filereader

Then, you should provide it via polyfills field in options.

index.js:

const { Peer } = require('peerjs');
const fetch = require('node-fetch');
const WebSocket = require('ws');
const WebRTC = require('wrtc');
const FileReader = require('filereader');

const polyfills = { fetch, WebSocket, WebRTC, FileReader };

const peer = new Peer({ polyfills, debug: 3 });

peer.on('open', id => {
  console.log('Server: peer id ', id);
  const peer2 = new Peer({ polyfills, debug: 3 });

  peer2.on('open', id2 => {
    console.log('Client: peer id ', id2);
    const conn = peer2.connect(id, { serialization: 'json' });

    conn.on('open', () => {
      console.log('Client: connection open');
      setInterval(() => {
        conn.send('hi!');
      }, 1000);

      conn.on('data', data => {
        console.log('Client: ', data);
      });
    });
  });
});

peer.on('connection', conn => {
  conn.on('data', data => {
    console.log('Server: ', data);

    conn.send(`now time is ${Date.now()}`);
  });

  conn.on('open', () => {
    conn.send('hello!');
  });
});

@afrokick afrokick added this to the 2.0.0 milestone Feb 20, 2022
This was referenced Feb 20, 2022
@matallui
Copy link
Contributor

@afrokick can we somehow make this version work with react-native-webrtc as well? There was a react-native-peerjs package but hasn't been maintained for like 3 years, so I'm trying to figure out how I could create a fork that works.

Any guidance on how to achieve this would be much appreciated.

@jonasgloning
Copy link
Member

Have a look at the diff @matallui.

react-native-webrtc should support UnifiedPlan and the track API in the meantime. Binarypack also should work. Maybe webrtc-adapter still has to be removed, but PeerJS will probably work with only react-native-webrtc shim imported and registerGlobals() called.

@matallui
Copy link
Contributor

@jonasgloning thanks for the response. What I ended up doing was to copy peerjs's .ts source files into my RN app (which contains react-native-webrtc) and then made some small adjustments to the code to get it working and that worked for me.

I just needed a quick win cause I have a demo in a few days, but once I find the time I'd love to figure out a better way of maintaining a React Native PeerJS package.

Or better yet, I wonder if changes could be made to this repo to make it work in both the browser and RN. I feel like it wouldn't require a lot of changes to achieve that, but not sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants