videocall-client-socket 0.1.6 → 0.1.7

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.
Files changed (3) hide show
  1. package/README.md +15 -9
  2. package/package.json +1 -1
  3. package/src/index.js +42 -10
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # 📞 videocall-client-socket
1
+ # 📞 videocall-socket-client
2
2
 
3
3
  A simple WebRTC client library for peer-to-peer video calls using [simple-peer](https://www.npmjs.com/package/simple-peer) and [socket.io-client](https://www.npmjs.com/package/socket.io-client).
4
4
 
@@ -56,14 +56,20 @@ VideoClient.on("user-media-toggled", (data) => {
56
56
  });
57
57
 
58
58
  // Step 2: Create media stream
59
- await VideoClient.createMediaStream();
60
-
61
- // Step 3: Play local stream in <video> tag
62
- VideoClient.playVideoTrack("localVideo");
63
-
64
- // Step 4: Join the signaling channel
65
- VideoClient.setServerURL("http://localhost:3000");
66
- VideoClient.joinChannel(userId, channelName);
59
+ SocketClient.createMediaStream()
60
+ .then(() => {
61
+ // Step 3: Play local stream in <video> tag
62
+ VideoClient.playVideoTrack("localVideo");
63
+ SocketClient.joinChannel(userId, channelName);
64
+
65
+ // Step 4: Join the signaling channel
66
+ //IMPORTANT: The service repository is located at the bottom.
67
+ VideoClient.setServerURL("http://localhost:3000");
68
+ VideoClient.joinChannel(userId, channelName);
69
+ })
70
+ .catch((err) => {
71
+ console.error(err);
72
+ });
67
73
  ```
68
74
 
69
75
  ---
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "videocall-client-socket",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "main": "src/index.js",
5
5
  "types": "src/index.d.ts",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -5,7 +5,7 @@ var socket = null;
5
5
  var mediastream = null;
6
6
  var localVideoId = "";
7
7
  var userId = "";
8
- var url = 'http://localhost:3000';
8
+ var serverURL = 'http://localhost:3000';
9
9
  let events = {};
10
10
 
11
11
  /**
@@ -42,20 +42,39 @@ let emit = (_event, data) => {
42
42
  * @param {string} _url - Server URL (e.g. "http://localhost:3000").
43
43
  */
44
44
  export function setServerURL(_url) {
45
- url = _url;
45
+ serverURL = _url;
46
46
  }
47
47
 
48
48
  /**
49
- * Requests access to the user's camera and microphone.
50
- * @returns {Promise<MediaStream>} - Resolves with a MediaStream containing audio and video.
49
+ * Requests access to both camera and microphone.
50
+ * Throws an error if either device is missing or denied.
51
+ * @returns {Promise<MediaStream>}
51
52
  */
52
53
  export function createMediaStream() {
53
- return navigator.mediaDevices.getUserMedia({ video: true, audio: true }).then(stream => {
54
- mediastream = stream;
55
- return stream;
54
+ return new Promise((resolve, reject) => {
55
+ navigator.mediaDevices.getUserMedia({ video: true, audio: true }).then((stream) => {
56
+ const hasVideo = stream.getVideoTracks().length > 0;
57
+ const hasAudio = stream.getAudioTracks().length > 0;
58
+
59
+ if (!hasVideo || !hasAudio) {
60
+ stream.getTracks().forEach(track => track.stop()); // cleanup
61
+ throw new Error(
62
+ !hasVideo && !hasAudio
63
+ ? "Camera and microphone are not available."
64
+ : !hasVideo
65
+ ? "Camera is not available."
66
+ : "Microphone is not available."
67
+ );
68
+ }
69
+
70
+ mediastream = stream;
71
+ resolve(mediastream);
72
+ }).catch((err) => {
73
+ reject("Camera and microphone are not available.");
74
+ });
56
75
  });
57
- }
58
76
 
77
+ }
59
78
  /**
60
79
  * Plays the local video stream into a specified <video> element.
61
80
  * @param {string} _localVideoId - The ID of the <video> element in the DOM.
@@ -76,18 +95,20 @@ export function playVideoTrack(_localVideoId) {
76
95
  export function joinChannel(_userId, channelName) {
77
96
  userId = _userId;
78
97
 
79
- socket = io(url, {
98
+ socket = io(serverURL, {
80
99
  query: { room: channelName, userId }
81
100
  });
82
101
 
83
102
  socket.on('all-users', users => {
84
- users.forEach(({ userId: remoteUserId, socketId }) => {
103
+ users.forEach(({ userId: remoteUserId, socketId, }) => {
85
104
  const peer = createPeer(socket, mediastream, socketId, remoteUserId, true);
86
105
  peers[remoteUserId] = peer;
87
106
  });
88
107
  });
89
108
 
90
109
  socket.on('user-joined', ({ userId: newUserId, socketId: newSocketId }) => {
110
+ console.log("User joined", newUserId, newSocketId);
111
+
91
112
  const peer = createPeer(socket, mediastream, newSocketId, newUserId, false);
92
113
  peers[newUserId] = peer;
93
114
  });
@@ -118,6 +139,7 @@ export function joinChannel(_userId, channelName) {
118
139
  enabled
119
140
  });
120
141
  });
142
+
121
143
  }
122
144
 
123
145
  /**
@@ -173,6 +195,7 @@ function createPeer(socket, localStream, targetSocketId, remoteUserId, initiator
173
195
  });
174
196
 
175
197
  peer.on('stream', stream => {
198
+
176
199
  emit("user-published", {
177
200
  user: {
178
201
  uuid: remoteUserId,
@@ -180,6 +203,15 @@ function createPeer(socket, localStream, targetSocketId, remoteUserId, initiator
180
203
  },
181
204
  mediaType: "video"
182
205
  });
206
+
207
+ setTimeout(() => {
208
+ socket.emit('initial-media-status', {
209
+ targetId: targetSocketId,
210
+ userId: userId,
211
+ audio: isAudioOn(),
212
+ video: isCameraOn()
213
+ });
214
+ }, 100);
183
215
  });
184
216
 
185
217
  peer.on('error', err => {