react-peer-chat 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Sahil Aggawal, <aggarwalsahil2004@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # react-peer-chat
2
+ An easy to use react component for impleting peer-to-peer chatting.
@@ -0,0 +1,3 @@
1
+ import React from "react";
2
+ export declare function BsFillMicFill(props: React.DetailedHTMLProps<React.HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>): React.JSX.Element;
3
+ export declare function BsFillMicMuteFill(props: React.DetailedHTMLProps<React.HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>): React.JSX.Element;
package/build/icons.js ADDED
@@ -0,0 +1,13 @@
1
+ import React from "react";
2
+ export function BsFillMicFill(props) {
3
+ return React.createElement("span", Object.assign({}, props),
4
+ React.createElement("svg", { fill: "currentColor", viewBox: "0 0 16 16" },
5
+ React.createElement("path", { d: "M5 3a3 3 0 0 1 6 0v5a3 3 0 0 1-6 0V3z" }),
6
+ React.createElement("path", { d: "M3.5 6.5A.5.5 0 0 1 4 7v1a4 4 0 0 0 8 0V7a.5.5 0 0 1 1 0v1a5 5 0 0 1-4.5 4.975V15h3a.5.5 0 0 1 0 1h-7a.5.5 0 0 1 0-1h3v-2.025A5 5 0 0 1 3 8V7a.5.5 0 0 1 .5-.5z" })));
7
+ }
8
+ export function BsFillMicMuteFill(props) {
9
+ return React.createElement("span", Object.assign({}, props),
10
+ React.createElement("svg", { fill: "currentColor", viewBox: "0 0 16 16" },
11
+ React.createElement("path", { d: "M13 8c0 .564-.094 1.107-.266 1.613l-.814-.814A4.02 4.02 0 0 0 12 8V7a.5.5 0 0 1 1 0v1zm-5 4c.818 0 1.578-.245 2.212-.667l.718.719a4.973 4.973 0 0 1-2.43.923V15h3a.5.5 0 0 1 0 1h-7a.5.5 0 0 1 0-1h3v-2.025A5 5 0 0 1 3 8V7a.5.5 0 0 1 1 0v1a4 4 0 0 0 4 4zm3-9v4.879L5.158 2.037A3.001 3.001 0 0 1 11 3z" }),
12
+ React.createElement("path", { d: "M9.486 10.607 5 6.12V8a3 3 0 0 0 4.486 2.607zm-7.84-9.253 12 12 .708-.708-12-12-.708.708z" })));
13
+ }
@@ -0,0 +1,11 @@
1
+ import React from 'react';
2
+ import { PeerOptions } from '../node_modules/peerjs/dist/types';
3
+ type Id = string | number;
4
+ type Props = {
5
+ peerId: Id;
6
+ remotePeerId: Id;
7
+ peerOptions?: PeerOptions;
8
+ onError?: () => void;
9
+ };
10
+ export default function Chat({ peerId, remotePeerId, peerOptions, onError }: Props): React.JSX.Element;
11
+ export {};
package/build/index.js ADDED
@@ -0,0 +1,49 @@
1
+ import React, { useEffect, useRef } from 'react';
2
+ import useStorage from './useStorage';
3
+ import { BsFillMicFill, BsFillMicMuteFill } from './icons';
4
+ export default function Chat({ peerId, remotePeerId, peerOptions = {}, onError = () => console.error("Can not access microphone!") }) {
5
+ const [audio, setAudio] = useStorage('rpc-audio', false, { local: true, save: true });
6
+ const streamRef = useRef(null);
7
+ const localStream = useRef();
8
+ const handleRemoteStream = (remoteStream) => streamRef.current.srcObject = remoteStream;
9
+ useEffect(() => {
10
+ if (!audio)
11
+ return;
12
+ const Peer = require('peerjs').default;
13
+ const peer = new Peer(`rpc-${peerId}`, peerOptions);
14
+ let call;
15
+ peer.on('open', () => {
16
+ const getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
17
+ getUserMedia({
18
+ video: false,
19
+ audio: {
20
+ autoGainControl: false,
21
+ noiseSuppression: true,
22
+ echoCancellation: true
23
+ }
24
+ }, (stream) => {
25
+ localStream.current = stream;
26
+ call = peer.call(`rpc-${remotePeerId}`, stream);
27
+ call.on('stream', handleRemoteStream);
28
+ call.on('close', call.removeAllListeners);
29
+ peer.on('call', (e) => {
30
+ call = e;
31
+ call.answer(stream);
32
+ call.on('stream', handleRemoteStream);
33
+ call.on('close', call.removeAllListeners);
34
+ });
35
+ }, onError);
36
+ });
37
+ return () => {
38
+ var _a;
39
+ (_a = localStream.current) === null || _a === void 0 ? void 0 : _a.getTracks().forEach(track => track.stop());
40
+ call === null || call === void 0 ? void 0 : call.removeAllListeners();
41
+ call === null || call === void 0 ? void 0 : call.close();
42
+ peer.removeAllListeners();
43
+ peer.destroy();
44
+ };
45
+ }, [audio]);
46
+ return React.createElement("button", { onClick: () => setAudio(audio => !audio) }, audio ? React.createElement(React.Fragment, null,
47
+ React.createElement("audio", { ref: streamRef, autoPlay: true, className: 'hidden' }),
48
+ React.createElement(BsFillMicFill, { title: "Turn mic off" })) : React.createElement(BsFillMicMuteFill, { title: "Turn mic on" }));
49
+ }
@@ -0,0 +1,4 @@
1
+ export default function useStorage<Value>(key: string, initialValue: Value, { local, save }?: {
2
+ local?: boolean | undefined;
3
+ save?: boolean | undefined;
4
+ }): [Value, (value: Value | ((old: Value) => Value)) => void];
@@ -0,0 +1,37 @@
1
+ import { useState } from "react";
2
+ const setStorage = (key, value, local = false) => (local ? localStorage : sessionStorage).setItem(key, JSON.stringify(value));
3
+ const removeStorage = (key, local = false) => (local ? localStorage : sessionStorage).removeItem(key);
4
+ const getStorage = (key, fallbackValue, local = false) => {
5
+ if (typeof window === "undefined")
6
+ return fallbackValue;
7
+ let value = (local ? localStorage : sessionStorage).getItem(key);
8
+ try {
9
+ if (!value)
10
+ throw new Error("Value doesn't exist");
11
+ value = JSON.parse(value);
12
+ }
13
+ catch (_a) {
14
+ if (fallbackValue !== undefined) {
15
+ value = fallbackValue;
16
+ setStorage(key, value, local);
17
+ }
18
+ else {
19
+ value = null;
20
+ removeStorage(key, local);
21
+ }
22
+ }
23
+ return value;
24
+ };
25
+ export default function useStorage(key, initialValue, { local = false, save = false } = {}) {
26
+ save || (save = getStorage('mode') !== 'online');
27
+ const [storedValue, setStoredValue] = useState(save ? getStorage(key, initialValue, local) : initialValue);
28
+ const setValue = (value) => {
29
+ setStoredValue((old) => {
30
+ const updatedValue = typeof value === 'function' ? value(old) : value;
31
+ if (save)
32
+ setStorage(key, updatedValue, local);
33
+ return updatedValue;
34
+ });
35
+ };
36
+ return [storedValue, setValue];
37
+ }
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "react-peer-chat",
3
+ "version": "0.1.0",
4
+ "description": "An easy to use react component for impleting peer-to-peer chatting.",
5
+ "main": "./build/index.js",
6
+ "scripts": {
7
+ "build": "npm i && tsc"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/SahilAggarwal2004/react-peer-chat.git"
12
+ },
13
+ "keywords": [
14
+ "react",
15
+ "chat",
16
+ "component",
17
+ "peer",
18
+ "peerjs",
19
+ "p2p",
20
+ "peer-to-peer",
21
+ "webrtc",
22
+ "react-peer-chat"
23
+ ],
24
+ "author": "Sahil Aggarwal",
25
+ "license": "MIT",
26
+ "bugs": {
27
+ "url": "https://github.com/SahilAggarwal2004/react-peer-chat/issues"
28
+ },
29
+ "homepage": "https://github.com/SahilAggarwal2004/react-peer-chat#readme",
30
+ "peerDependencies": {
31
+ "react": ">=16.0.0",
32
+ "react-dom": ">=16.0.0"
33
+ },
34
+ "devDependencies": {
35
+ "@types/node": "^20.5.9",
36
+ "@types/react": "^18.2.21"
37
+ },
38
+ "dependencies": {
39
+ "peerjs": "^1.5.0"
40
+ }
41
+ }