react-peer-chat 0.4.3 → 0.5.1

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 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,10 +118,11 @@ 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. |
123
- | `onError` | `Function` | No | `() => alert('Browser not supported! Try some other browser.')` | Function to be executed if browser doesn't support [WebRTC](https://webrtc.org/). |
125
+ | `onError` | `Function` | No | `() => alert('Browser not supported! Try some other browser.')` | Function to be executed if browser doesn't support `WebRTC` |
124
126
  | `onMicError` | `Function` | No | `() => alert('Microphone not accessible!')` | Function to be executed when microphone is not accessible. |
125
127
  | `props` | `React.DetailedHTMLProps` | No | - | Props to customize the `<Chat>` component. |
126
128
  | `children` | [`Children`](#Children) | No | - | Props to customize the `<Chat>` component. |
@@ -155,7 +157,5 @@ type ChildrenOptions = {
155
157
  };
156
158
  type Children = (childrenOptions: ChildrenOptions) => ReactNode;
157
159
  ```
158
- ## Used By
159
- - [StarWars](https://starwarsgame.vercel.app/)
160
160
  ## Author
161
161
  [Sahil Aggarwal](https://www.github.com/SahilAggarwal2004)
package/build/index.d.ts CHANGED
@@ -7,7 +7,7 @@ export interface DialogOptions {
7
7
  position?: DialogPosition;
8
8
  style?: CSSProperties;
9
9
  }
10
- type RemotePeers = {
10
+ export type RemotePeers = {
11
11
  [id: string]: string;
12
12
  };
13
13
  export interface Message {
@@ -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,7 +5,7 @@ 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
11
  const [remotePeers, setRemotePeers] = useStorage('rpc-remote-peer', {});
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-peer-chat",
3
- "version": "0.4.3",
3
+ "version": "0.5.1",
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.21"
37
+ "@types/react": "^18.2.28"
38
38
  },
39
39
  "dependencies": {
40
- "peerjs": "^1.5.0"
40
+ "peerjs": "^1.5.1"
41
41
  }
42
42
  }