react-peer-chat 0.9.0 → 0.10.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.
@@ -75,14 +75,29 @@ function useChat({
75
75
  return { completePeerId: addPrefix(peerId), completeRemotePeerIds: remotePeerIds.map(addPrefix) };
76
76
  }, [peerId]);
77
77
  function handleCall(call) {
78
- const id = call.peer;
79
- call.on("stream", (stream) => handleRemoteStream(id, stream));
78
+ const peerId2 = call.peer;
79
+ call.on("stream", () => {
80
+ callsRef.current[peerId2] = call;
81
+ if (!audioContextRef.current) audioContextRef.current = new AudioContext();
82
+ if (audioContextRef.current.state === "suspended") audioContextRef.current.resume();
83
+ if (!mixerRef.current) {
84
+ mixerRef.current = audioContextRef.current.createGain();
85
+ mixerRef.current.connect(audioContextRef.current.destination);
86
+ }
87
+ removePeerAudio(peerId2);
88
+ const audio2 = new Audio();
89
+ audio2.srcObject = call.remoteStream;
90
+ audio2.autoplay = true;
91
+ audio2.muted = false;
92
+ const source = audioContextRef.current.createMediaElementSource(audio2);
93
+ source.connect(mixerRef.current);
94
+ sourceNodesRef.current[peerId2] = source;
95
+ });
80
96
  call.on("close", () => {
81
97
  call.removeAllListeners();
82
- removePeerAudio(id);
83
- delete callsRef.current[id];
98
+ removePeerAudio(peerId2);
99
+ delete callsRef.current[peerId2];
84
100
  });
85
- callsRef.current[id] = call;
86
101
  }
87
102
  function handleConnection(conn) {
88
103
  connRef.current[conn.peer] = conn;
@@ -102,17 +117,6 @@ function useChat({
102
117
  setAudio(false);
103
118
  onMicError();
104
119
  }
105
- function handleRemoteStream(peerId2, remoteStream) {
106
- if (!audioContextRef.current) audioContextRef.current = new AudioContext();
107
- if (!mixerRef.current) {
108
- mixerRef.current = audioContextRef.current.createGain();
109
- mixerRef.current.connect(audioContextRef.current.destination);
110
- }
111
- removePeerAudio(peerId2);
112
- const source = audioContextRef.current.createMediaStreamSource(remoteStream);
113
- source.connect(mixerRef.current);
114
- sourceNodesRef.current[peerId2] = source;
115
- }
116
120
  function receiveMessage(message) {
117
121
  addMessage(message);
118
122
  onMessageReceived == null ? void 0 : onMessageReceived(message);
@@ -123,9 +127,10 @@ function useChat({
123
127
  delete sourceNodesRef.current[peerId2];
124
128
  }
125
129
  function sendMessage(message) {
126
- addMessage(message);
127
- Object.values(connRef.current).forEach((conn) => conn.send({ type: "message", message }));
128
- onMessageSent == null ? void 0 : onMessageSent(message);
130
+ const event = { type: "message", message: __spreadProps(__spreadValues({}, message), { name }) };
131
+ addMessage(event.message);
132
+ Object.values(connRef.current).forEach((conn) => conn.send(event));
133
+ onMessageSent == null ? void 0 : onMessageSent(event.message);
129
134
  }
130
135
  useEffect(() => {
131
136
  if (!text && !audio) return;
@@ -166,12 +171,9 @@ function useChat({
166
171
  const setupAudio = () => navigator.mediaDevices.getUserMedia({
167
172
  video: false,
168
173
  audio: {
169
- autoGainControl: false,
170
- // Disable automatic gain control
174
+ autoGainControl: true,
171
175
  noiseSuppression: true,
172
- // Enable noise suppression
173
176
  echoCancellation: true
174
- // Enable echo cancellation
175
177
  }
176
178
  }).then((stream) => {
177
179
  localStream = stream;
@@ -1,4 +1,4 @@
1
- import { useChat } from './chunk-MBDO7IOK.js';
1
+ import { useChat } from './chunk-L3CFU5IB.js';
2
2
  import { BiSolidMessageX, BiSolidMessageDetail, GrSend, BsFillMicFill, BsFillMicMuteFill } from './chunk-JJPIWKLG.js';
3
3
  import { __objRest, __spreadValues } from './chunk-LNEKYYG7.js';
4
4
  import React, { useRef, useState, useEffect } from 'react';
@@ -28,7 +28,7 @@ function Chat(_a) {
28
28
  const _a2 = useChat(__spreadValues({
29
29
  text,
30
30
  audio,
31
- onMessageReceived: modifiedOnMessageReceived
31
+ onMessageReceived: receiveMessageHandler
32
32
  }, hookProps)), { peerId } = _a2, childrenOptions = __objRest(_a2, ["peerId"]);
33
33
  const { remotePeers, messages, sendMessage, audio: audioEnabled, setAudio } = childrenOptions;
34
34
  const containerRef = useRef(null);
@@ -36,7 +36,7 @@ function Chat(_a) {
36
36
  const dialogRef = useRef(null);
37
37
  const inputRef = useRef(null);
38
38
  const [notification, setNotification] = useState(false);
39
- function modifiedOnMessageReceived(message) {
39
+ function receiveMessageHandler(message) {
40
40
  var _a3;
41
41
  if (!((_a3 = dialogRef.current) == null ? void 0 : _a3.open)) setNotification(true);
42
42
  onMessageReceived == null ? void 0 : onMessageReceived(message);
@@ -59,7 +59,7 @@ function Chat(_a) {
59
59
  setDialog(true);
60
60
  }
61
61
  }
62
- ), notification && /* @__PURE__ */ React.createElement("span", { className: "rpc-badge" })), /* @__PURE__ */ React.createElement("dialog", { ref: dialogRef, className: `${dialog ? "rpc-dialog" : ""} rpc-position-${(dialogOptions == null ? void 0 : dialogOptions.position) || "center"}`, style: dialogOptions == null ? void 0 : dialogOptions.style }, /* @__PURE__ */ React.createElement("div", { className: "rpc-heading" }, "Chat"), /* @__PURE__ */ React.createElement("hr", { className: "rpc-hr" }), /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement("div", { ref: containerRef, className: "rpc-message-container" }, messages.map(({ id, text: text2 }, i) => /* @__PURE__ */ React.createElement("div", { key: i }, /* @__PURE__ */ React.createElement("strong", null, id === peerId ? "You" : remotePeers[id], ": "), /* @__PURE__ */ React.createElement("span", null, text2)))), /* @__PURE__ */ React.createElement("hr", { className: "rpc-hr" }), /* @__PURE__ */ React.createElement(
62
+ ), notification && /* @__PURE__ */ React.createElement("span", { className: "rpc-badge" })), /* @__PURE__ */ React.createElement("dialog", { ref: dialogRef, className: `${dialog ? "rpc-dialog" : ""} rpc-position-${(dialogOptions == null ? void 0 : dialogOptions.position) || "center"}`, style: dialogOptions == null ? void 0 : dialogOptions.style }, /* @__PURE__ */ React.createElement("div", { className: "rpc-heading" }, "Chat"), /* @__PURE__ */ React.createElement("hr", { className: "rpc-hr" }), /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement("div", { ref: containerRef, className: "rpc-message-container" }, messages.map(({ id, name, text: text2 }, i) => /* @__PURE__ */ React.createElement("div", { key: i }, /* @__PURE__ */ React.createElement("strong", null, id === peerId ? "You" : name, ": "), /* @__PURE__ */ React.createElement("span", null, text2)))), /* @__PURE__ */ React.createElement("hr", { className: "rpc-hr" }), /* @__PURE__ */ React.createElement(
63
63
  "form",
64
64
  {
65
65
  className: "rpc-input-container",
@@ -1,5 +1,5 @@
1
- export { Chat as default } from './chunks/chunk-N4TBKQIR.js';
2
- import './chunks/chunk-MBDO7IOK.js';
1
+ export { Chat as default } from './chunks/chunk-OB5LLPQI.js';
2
+ import './chunks/chunk-L3CFU5IB.js';
3
3
  import './chunks/chunk-JJPIWKLG.js';
4
4
  import './chunks/chunk-ZYFPSCFE.js';
5
5
  import './chunks/chunk-LNEKYYG7.js';
package/dist/hooks.js CHANGED
@@ -1,3 +1,3 @@
1
- export { useAudio, useChat, useMessages, useStorage } from './chunks/chunk-MBDO7IOK.js';
1
+ export { useAudio, useChat, useMessages, useStorage } from './chunks/chunk-L3CFU5IB.js';
2
2
  import './chunks/chunk-ZYFPSCFE.js';
3
3
  import './chunks/chunk-LNEKYYG7.js';
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- export { Chat as default } from './chunks/chunk-N4TBKQIR.js';
2
- export { useChat } from './chunks/chunk-MBDO7IOK.js';
1
+ export { Chat as default } from './chunks/chunk-OB5LLPQI.js';
2
+ export { useChat } from './chunks/chunk-L3CFU5IB.js';
3
3
  import './chunks/chunk-JJPIWKLG.js';
4
4
  export { clearChat } from './chunks/chunk-ZYFPSCFE.js';
5
5
  import './chunks/chunk-LNEKYYG7.js';
package/dist/types.d.ts CHANGED
@@ -4,10 +4,13 @@ import { CSSProperties, DetailedHTMLProps, HTMLAttributes, SetStateAction, React
4
4
 
5
5
  type Connection = DataConnection | MediaConnection;
6
6
  type ErrorHandler = () => void;
7
- type Message = {
7
+ type InputMessage = {
8
8
  id: string;
9
9
  text: string;
10
10
  };
11
+ type Message = InputMessage & {
12
+ name: string;
13
+ };
11
14
  type MessageEventHandler = (message: Message) => void;
12
15
 
13
16
  type RemotePeerId = string | string[];
@@ -27,7 +30,7 @@ type UseChatProps = {
27
30
  type ChildrenOptions = {
28
31
  remotePeers: RemotePeers;
29
32
  messages: Message[];
30
- sendMessage: (message: Message) => void;
33
+ sendMessage: (message: InputMessage) => void;
31
34
  audio: boolean;
32
35
  setAudio: (value: SetStateAction<boolean>) => void;
33
36
  };
@@ -49,4 +52,4 @@ type DialogPosition = "left" | "center" | "right";
49
52
  type DivProps = DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
50
53
  type RemotePeers = Record<string, string>;
51
54
 
52
- export type { ChatProps, Children, ChildrenOptions, Connection, DialogOptions, DialogPosition, DivProps, ErrorHandler, IconProps, Message, MessageEventHandler, RemotePeerId, RemotePeers, UseChatProps, UseChatReturn };
55
+ export type { ChatProps, Children, ChildrenOptions, Connection, DialogOptions, DialogPosition, DivProps, ErrorHandler, IconProps, InputMessage, Message, MessageEventHandler, RemotePeerId, RemotePeers, UseChatProps, UseChatReturn };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-peer-chat",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "An easy to use react component for impleting peer-to-peer chatting.",
5
5
  "license": "MIT",
6
6
  "author": "Sahil Aggarwal <aggarwalsahil2004@gmail.com>",