react-peer-chat 0.4.2 → 0.5.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/README.md +3 -1
- package/build/index.d.ts +2 -1
- package/build/index.js +10 -7
- package/build/storage.d.ts +1 -4
- package/build/storage.js +7 -7
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
An easy to use react component for impleting peer-to-peer chatting using [peerjs](https://peerjs.com/) under the hood.
|
|
3
3
|
|
|
4
4
|
## Features
|
|
5
|
-
- Peer-to-peer chat without need to have any knowledge about WebRTC
|
|
5
|
+
- Peer-to-peer chat without need to have any knowledge about [WebRTC](https://webrtc.org/)
|
|
6
6
|
- Easy to use
|
|
7
7
|
- Supports text chat that persists on page reload
|
|
8
|
+
- Recovers old chat upon reconnection
|
|
8
9
|
- Clear text chat on command
|
|
9
10
|
- Supports voice chat
|
|
10
11
|
- Multiple peer connections. See [multi peer usage](#Multi-Peer-Usage)
|
|
@@ -117,6 +118,7 @@ Here is the full API for the `<Chat>` component, these properties can be set on
|
|
|
117
118
|
| `peerId` | `string` | Yes | - | It is the unique id that is alloted to a peer. It uniquely identifies a peer from other peers. |
|
|
118
119
|
| `remotePeerId` | `string \| string[]` | No | - | It is the unique id (or array of unique ids) of the remote peer(s). If provided, the peer will try to connect to the remote peer(s). |
|
|
119
120
|
| `text` | `boolean` | No | `true` | Text chat will be enabled if this property is set to true. |
|
|
121
|
+
| `recoverChat` | `boolean` | No | `false` | Old chats will be recovered upon reconnecting with the same peer(s). |
|
|
120
122
|
| `voice` | `boolean` | No | `true` | Voice chat will be enabled if this property is set to true. |
|
|
121
123
|
| `peerOptions` | [`PeerOptions`](#PeerOptions) | No | - | Options to customize peerjs Peer instance. |
|
|
122
124
|
| `dialogOptions` | [`DialogOptions`](#DialogOptions) | No | { position: 'center' } | Options to customize text dialog box styling. |
|
package/build/index.d.ts
CHANGED
|
@@ -28,6 +28,7 @@ export interface ChatProps {
|
|
|
28
28
|
peerId: string;
|
|
29
29
|
remotePeerId?: RemotePeerId;
|
|
30
30
|
text?: boolean;
|
|
31
|
+
recoverChat?: boolean;
|
|
31
32
|
voice?: boolean;
|
|
32
33
|
peerOptions?: PeerOptions;
|
|
33
34
|
dialogOptions?: DialogOptions;
|
|
@@ -37,5 +38,5 @@ export interface ChatProps {
|
|
|
37
38
|
props?: Props;
|
|
38
39
|
}
|
|
39
40
|
export type { IconProps } from './icons.js';
|
|
40
|
-
export default function Chat({ name, peerId, remotePeerId, peerOptions, text, voice, dialogOptions, onError, onMicError, children, props }: ChatProps): React.JSX.Element;
|
|
41
|
+
export default function Chat({ name, peerId, remotePeerId, peerOptions, text, recoverChat, voice, dialogOptions, onError, onMicError, children, props }: ChatProps): React.JSX.Element;
|
|
41
42
|
export declare const clearChat: () => void;
|
package/build/index.js
CHANGED
|
@@ -5,17 +5,17 @@ function closeConnection(conn) {
|
|
|
5
5
|
conn.removeAllListeners();
|
|
6
6
|
conn.close();
|
|
7
7
|
}
|
|
8
|
-
export default function Chat({ name, peerId, remotePeerId = [], peerOptions, text = true, voice = true, dialogOptions, onError = () => alert('Browser not supported! Try some other browser.'), onMicError = () => alert('Microphone not accessible!'), children, props = {} }) {
|
|
8
|
+
export default function Chat({ name, peerId, remotePeerId = [], peerOptions, text = true, recoverChat = false, voice = true, dialogOptions, onError = () => alert('Browser not supported! Try some other browser.'), onMicError = () => alert('Microphone not accessible!'), children, props = {} }) {
|
|
9
9
|
const [peer, setPeer] = useState();
|
|
10
10
|
const [notification, setNotification] = useState(false);
|
|
11
|
-
const [remotePeers, setRemotePeers] = useStorage('rpc-remote-peer', {}
|
|
12
|
-
const [messages, setMessages] = useStorage('rpc-messages', []
|
|
11
|
+
const [remotePeers, setRemotePeers] = useStorage('rpc-remote-peer', {});
|
|
12
|
+
const [messages, setMessages] = useStorage('rpc-messages', []);
|
|
13
13
|
const connRef = useRef({});
|
|
14
14
|
const [dialog, setDialog] = useState(false);
|
|
15
15
|
const dialogRef = useRef(null);
|
|
16
16
|
const containerRef = useRef(null);
|
|
17
17
|
const inputRef = useRef(null);
|
|
18
|
-
const [audio, setAudio] = useStorage('rpc-audio', false,
|
|
18
|
+
const [audio, setAudio] = useStorage('rpc-audio', false, true);
|
|
19
19
|
const streamRef = useRef(null);
|
|
20
20
|
const localStream = useRef();
|
|
21
21
|
peerId = `rpc-${peerId}`;
|
|
@@ -34,16 +34,19 @@ export default function Chat({ name, peerId, remotePeerId = [], peerOptions, tex
|
|
|
34
34
|
function handleConnection(conn) {
|
|
35
35
|
connRef.current[conn.peer] = conn;
|
|
36
36
|
conn.on('open', () => {
|
|
37
|
-
conn.on('data', ({ type, message, remotePeerName }) => {
|
|
37
|
+
conn.on('data', ({ type, message, remotePeerName, messages }) => {
|
|
38
38
|
if (type === 'message')
|
|
39
39
|
addMessage(message);
|
|
40
|
-
else if (type === 'init')
|
|
40
|
+
else if (type === 'init') {
|
|
41
41
|
setRemotePeers(prev => {
|
|
42
42
|
prev[conn.peer] = remotePeerName || 'Anonymous User';
|
|
43
43
|
return prev;
|
|
44
44
|
});
|
|
45
|
+
if (recoverChat)
|
|
46
|
+
setMessages(old => messages.length > old.length ? messages : old);
|
|
47
|
+
}
|
|
45
48
|
});
|
|
46
|
-
conn.send({ type: 'init', remotePeerName: name });
|
|
49
|
+
conn.send({ type: 'init', remotePeerName: name, messages });
|
|
47
50
|
});
|
|
48
51
|
conn.on('close', conn.removeAllListeners);
|
|
49
52
|
}
|
package/build/storage.d.ts
CHANGED
|
@@ -1,5 +1,2 @@
|
|
|
1
1
|
export declare const removeStorage: (key: string, local?: boolean) => void;
|
|
2
|
-
export default function useStorage<Value>(key: string, initialValue: Value,
|
|
3
|
-
local?: boolean | undefined;
|
|
4
|
-
save?: boolean | undefined;
|
|
5
|
-
}): [Value, (value: Value | ((old: Value) => Value)) => void];
|
|
2
|
+
export default function useStorage<Value>(key: string, initialValue: Value, local?: boolean): [Value, (value: Value | ((old: Value) => Value)) => void];
|
package/build/storage.js
CHANGED
|
@@ -2,8 +2,6 @@ import { useState } from "react";
|
|
|
2
2
|
const setStorage = (key, value, local = false) => (local ? localStorage : sessionStorage).setItem(key, JSON.stringify(value));
|
|
3
3
|
export const removeStorage = (key, local = false) => (local ? localStorage : sessionStorage).removeItem(key);
|
|
4
4
|
const getStorage = (key, fallbackValue, local = false) => {
|
|
5
|
-
if (typeof window === "undefined")
|
|
6
|
-
return fallbackValue;
|
|
7
5
|
let value = (local ? localStorage : sessionStorage).getItem(key);
|
|
8
6
|
try {
|
|
9
7
|
if (!value)
|
|
@@ -22,14 +20,16 @@ const getStorage = (key, fallbackValue, local = false) => {
|
|
|
22
20
|
}
|
|
23
21
|
return value;
|
|
24
22
|
};
|
|
25
|
-
export default function useStorage(key, initialValue,
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
export default function useStorage(key, initialValue, local = false) {
|
|
24
|
+
const [storedValue, setStoredValue] = useState(() => {
|
|
25
|
+
if (typeof window === "undefined")
|
|
26
|
+
return initialValue;
|
|
27
|
+
return getStorage(key, initialValue, local);
|
|
28
|
+
});
|
|
28
29
|
const setValue = (value) => {
|
|
29
30
|
setStoredValue((old) => {
|
|
30
31
|
const updatedValue = typeof value === 'function' ? value(old) : value;
|
|
31
|
-
|
|
32
|
-
setStorage(key, updatedValue, local);
|
|
32
|
+
setStorage(key, updatedValue, local);
|
|
33
33
|
return updatedValue;
|
|
34
34
|
});
|
|
35
35
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-peer-chat",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "An easy to use react component for impleting peer-to-peer chatting.",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -34,9 +34,9 @@
|
|
|
34
34
|
"react-dom": ">=16.0.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@types/react": "^18.2.
|
|
37
|
+
"@types/react": "^18.2.23"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"peerjs": "^1.5.
|
|
40
|
+
"peerjs": "^1.5.1"
|
|
41
41
|
}
|
|
42
42
|
}
|