diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61cdc48 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +peerjs/ +dist/ +node_modules/ +.idea +package-lock.json \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e283fe4 --- /dev/null +++ b/README.md @@ -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 diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..4a6d86b --- /dev/null +++ b/build.sh @@ -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" + diff --git a/decoupling.diff b/decoupling.diff new file mode 100644 index 0000000..1b9153b --- /dev/null +++ b/decoupling.diff @@ -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 = ( +- 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 { diff --git a/imports.js b/imports.js new file mode 100644 index 0000000..42824bf --- /dev/null +++ b/imports.js @@ -0,0 +1,6 @@ +import { + RTCPeerConnection, + RTCIceCandidate, + RTCSessionDescription, +} from 'react-native-webrtc'; + diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..17ef82f --- /dev/null +++ b/index.d.ts @@ -0,0 +1,3 @@ +declare module "react-native-peer" { + export * from "peerjs"; +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..7da06cb --- /dev/null +++ b/package.json @@ -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 (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" + } +}