Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pushpender-singh-ap committed Jan 16, 2024
0 parents commit eab2134
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
peerjs/
dist/
node_modules/
.idea
package-lock.json
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# react-native-peer

A React Native wrapper for [PeerJS](https://peerjs.com/). PeerJS simplifies WebRTC peer-to-peer data, video, and audio calls.

## Getting started

### To install and start using react-native-peer

```sh
npm install react-native-peer
```

## Usage

To use react-native-peer, `import` the `react-native-peer` module and use the `Peer`.

Here is an example of basic usage:

```js
import Peer from 'react-native-peer';

const globalPeer = new Peer();
globalPeer.on('error', console.log);

globalPeer.on('open', globalPeerID => {
console.log('Local peer open with ID', globalPeerID);

const remotePeer = new Peer();
remotePeer.on('error', console.log);
remotePeer.on('open', remotePeerId => {
console.log('Remote peer open with ID', remotePeerId);

const conn = remotePeer.connect(globalPeerID);
conn.on('error', console.log);
conn.on('open', () => {
console.log('Remote peer has opened connection.');
console.log('conn', conn);
conn.on('data', data => console.log('Received from local peer', data));
console.log('Remote peer sending data.');
conn.send('Hello, this is the REMOTE peer!');
});
});
});

globalPeer.on('connection', conn => {
console.log('Local peer has received connection.');
conn.on('error', console.log);
conn.on('open', () => {
console.log('Local peer has opened connection.');
console.log('conn', conn);
conn.on('data', data => console.log('Received from remote peer', data));
console.log('Local peer sending data.');
conn.send('Hello, this is the LOCAL peer!');
});
});

```

## License

MIT
18 changes: 18 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

exec 2>&1

rm -rf dist/ peerjs/
git clone https://github.com/peers/peerjs peerjs
cd peerjs
git checkout tags/v1.5.2
git apply ../decoupling.diff
../node_modules/.bin/parcel build --no-source-maps lib/exports.ts -d ../dist --out-file peerjs.min.js
cd ../
cat imports.js dist/peerjs.min.js > dist/react-native-peer.js
cp index.d.ts dist/
rm dist/peerjs.min.js
rm -rf peerjs/

echo "Done. dist/react-native-peer.js"

122 changes: 122 additions & 0 deletions decoupling.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
diff --git a/lib/dataconnection/StreamConnection/Cbor.ts b/lib/dataconnection/StreamConnection/Cbor.ts
index 523fdf0..8c8b3a8 100644
--- a/lib/dataconnection/StreamConnection/Cbor.ts
+++ b/lib/dataconnection/StreamConnection/Cbor.ts
@@ -1,6 +1,6 @@
-import type { Peer } from "../../peer.js";
+import type { Peer } from "../../peer";
import { Decoder, Encoder } from "cbor-x";
-import { StreamConnection } from "./StreamConnection.js";
+import { StreamConnection } from "./StreamConnection";

const NullValue = Symbol.for(null);

diff --git a/lib/dataconnection/StreamConnection/MsgPack.ts b/lib/dataconnection/StreamConnection/MsgPack.ts
index c2d9881..e4d362b 100644
--- a/lib/dataconnection/StreamConnection/MsgPack.ts
+++ b/lib/dataconnection/StreamConnection/MsgPack.ts
@@ -1,6 +1,6 @@
import { decodeMultiStream, Encoder } from "@msgpack/msgpack";
-import { StreamConnection } from "./StreamConnection.js";
-import type { Peer } from "../../peer.js";
+import { StreamConnection } from "./StreamConnection";
+import type { Peer } from "../../peer";

export class MsgPack extends StreamConnection {
readonly serialization = "MsgPack";
diff --git a/lib/dataconnection/StreamConnection/StreamConnection.ts b/lib/dataconnection/StreamConnection/StreamConnection.ts
index 6a8203c..4ff0a85 100644
--- a/lib/dataconnection/StreamConnection/StreamConnection.ts
+++ b/lib/dataconnection/StreamConnection/StreamConnection.ts
@@ -1,6 +1,6 @@
-import logger from "../../logger.js";
-import type { Peer } from "../../peer.js";
-import { DataConnection } from "../DataConnection.js";
+import logger from "../../logger";
+import type { Peer } from "../../peer";
+import { DataConnection } from "../DataConnection";

export abstract class StreamConnection extends DataConnection {
private _CHUNK_SIZE = 1024 * 8 * 4;
diff --git a/lib/negotiator.ts b/lib/negotiator.ts
index 6f5f462..30efc98 100644
--- a/lib/negotiator.ts
+++ b/lib/negotiator.ts
@@ -37,7 +37,7 @@ export class Negotiator<
const config: RTCDataChannelInit = { ordered: !!options.reliable };

const dataChannel = peerConnection.createDataChannel(
- dataConnection.label,
+ dataConnection.label || "",
config,
);
dataConnection._initializeDataChannel(dataChannel);
@@ -134,9 +134,7 @@ export class Negotiator<
logger.log("Received data channel");

const dataChannel = evt.channel;
- const connection = <DataConnection>(
- provider.getConnection(peerId, connectionId)
- );
+ const connection = provider.getConnection(peerId, connectionId);

connection._initializeDataChannel(dataChannel);
};
diff --git a/lib/supports.ts b/lib/supports.ts
index 902b61a..1b383ca 100644
--- a/lib/supports.ts
+++ b/lib/supports.ts
@@ -1,9 +1,3 @@
-import webRTCAdapter_import from "webrtc-adapter";
-
-const webRTCAdapter: typeof webRTCAdapter_import =
- //@ts-ignore
- webRTCAdapter_import.default || webRTCAdapter_import;
-
export const Supports = new (class {
readonly isIOS = ["iPad", "iPhone", "iPod"].includes(navigator.platform);
readonly supportedBrowsers = ["firefox", "chrome", "safari"];
@@ -33,40 +27,15 @@ export const Supports = new (class {
}

getBrowser(): string {
- return webRTCAdapter.browserDetails.browser;
+ return "chrome";
}

getVersion(): number {
- return webRTCAdapter.browserDetails.version || 0;
+ return this.minChromeVersion;
}

isUnifiedPlanSupported(): boolean {
- const browser = this.getBrowser();
- const version = webRTCAdapter.browserDetails.version || 0;
-
- if (browser === "chrome" && version < this.minChromeVersion) return false;
- if (browser === "firefox" && version >= this.minFirefoxVersion) return true;
- if (
- !window.RTCRtpTransceiver ||
- !("currentDirection" in RTCRtpTransceiver.prototype)
- )
- return false;
-
- let tempPc: RTCPeerConnection;
- let supported = false;
-
- try {
- tempPc = new RTCPeerConnection();
- tempPc.addTransceiver("audio");
- supported = true;
- } catch (e) {
- } finally {
- if (tempPc) {
- tempPc.close();
- }
- }
-
- return supported;
+ return true;
}

toString(): string {
6 changes: 6 additions & 0 deletions imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {
RTCPeerConnection,
RTCIceCandidate,
RTCSessionDescription,
} from 'react-native-webrtc';

3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module "react-native-peer" {
export * from "peerjs";
}
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "react-native-peer",
"version": "1.0.0",
"description": "A React Native wrapper for [PeerJS](https://peerjs.com/). PeerJS simplifies WebRTC peer-to-peer data, video, and audio calls.",
"keywords": [
"webrtc",
"react native",
"peerjs",
"peer",
"react-native-webrtc",
"react-native-peer",
"react-native-peerjs"
],
"repository": "https://github.com/pushpender-singh-ap/react-native-peer",
"author": "Pushpender Singh <[email protected]> (https://github.com/pushpender-singh-ap)",
"license": "MIT",
"bugs": {
"url": "https://github.com/pushpender-singh-ap/react-native-peer/issues"
},
"homepage": "https://github.com/pushpender-singh-ap/react-native-peer#readme",
"scripts": {
"build": "./build.sh",
"prepare": "./build.sh"
},
"main": "dist/react-native-peer.js",
"types": "dist/index.d.ts",
"devDependencies": {
"parcel": "1.12.3",
"peerjs": "1.5.2"
}
}

0 comments on commit eab2134

Please sign in to comment.