signalingserver.js 0.0.1 → 0.0.2

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
@@ -20,6 +20,7 @@ It use free public WebTorrent trackers as transport.
20
20
  * Mesh network
21
21
  * Chat
22
22
  * Multiplayer
23
+ * P2P matchmaking
23
24
 
24
25
  ## Install
25
26
 
@@ -43,18 +44,24 @@ const config = {
43
44
  appid : 'myApp'
44
45
  }
45
46
 
46
- //create new node
47
47
  const node = createSignalingServer(config);
48
48
 
49
- //signal handler
50
- node.data((signal)=>{
49
+ node.data((signal,id)=>{
50
+
51
+ //handle signal
51
52
  console.log(`receive signal : ${signal}`);
53
+
54
+ if(id){
55
+ //send answer signal to id
56
+ const msg = 'test answer';
57
+ node.send(msg,id);
58
+ }
59
+
52
60
  });
53
61
 
54
- //broadcast signal
55
- const signal = 'test'; //example
56
- node.send(signal);
57
- console.log(`send signal : ${signal}`);
62
+ //broadcast offer signal
63
+ const msg = 'test offer';
64
+ node.send(msg);
58
65
  ```
59
66
 
60
67
  ## API
@@ -63,22 +70,26 @@ console.log(`send signal : ${signal}`);
63
70
 
64
71
  Create new signaling server node
65
72
 
66
- Config : Parameter object
73
+ config : Parameter object
74
+
75
+ * appid = (string) Custom unique application ID
76
+ * tracker = (Array) Custom WebTorrent tracker list
67
77
 
68
- * appid = (string) Custom application name as identifier
69
- * tracker = (Array) Custom WebTorrent trackers list
78
+ ### send(signal,id)
70
79
 
71
- ### send
80
+ Send signal to other node, leave out id paramater to broadcast.
72
81
 
73
- Broadcast signal to all node
82
+ ### data(callback)
74
83
 
75
- ### data
84
+ On receive data handler.
76
85
 
77
- On receive signal handler
86
+ callback(signal,id)
87
+ * type offer emit signal and sender id
88
+ * type answer emit signal only
78
89
 
79
90
  ## Recomendation
80
91
 
81
- Encrypt the signal before broadcast it to prevent mitm.
92
+ Encrypt the signal before send it to prevent appearing plaintext data in the network.
82
93
 
83
94
  ## See Also
84
95
 
@@ -86,4 +97,4 @@ Encrypt the signal before broadcast it to prevent mitm.
86
97
 
87
98
  ## License
88
99
 
89
- * [MIT](https://github.com/nuzulul/signaling.js/blob/main/LICENSE) - [Nuzulul Zulkarnain](https://github.com/nuzulul)
100
+ * [MIT](https://github.com/nuzulul/signalingserver.js/blob/main/LICENSE) - [Nuzulul Zulkarnain](https://github.com/nuzulul)
package/demo.html CHANGED
@@ -22,16 +22,30 @@ const config = {
22
22
  }
23
23
 
24
24
  const node = createSignalingServer(config);
25
- node.data((signal)=>{
26
- const status = `receive signal : ${signal}`;
27
- write(status);
25
+ node.data((signal,id)=>{
26
+
27
+ if(id){
28
+ //get offer
29
+ const getstatus = `receive offer : ${signal}`;
30
+ write(getstatus);
31
+ //send answer
32
+ const msg = new Date().getTime().toString();
33
+ node.send(msg,id);
34
+ const sendstatus = `send answer : ${msg}`;
35
+ write(sendstatus);
36
+ }else{
37
+ //get answer
38
+ const status = `receive answer : ${signal}`;
39
+ write(status);
40
+ }
41
+
28
42
  })
29
43
 
30
44
  setInterval(()=>{
31
- const signal = new Date().getTime(); //example signal
32
- node.send(signal);
33
- const status = `send signal : ${signal}`;
34
- write(status);
45
+ const msg = new Date().getTime().toString();
46
+ node.send(msg);
47
+ const sendstatus = `send offer : ${msg}`;
48
+ write(sendstatus);
35
49
  },5000)
36
50
  </script>
37
51
  </body>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "signalingserver.js",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Alternative signaling server that works in browser, no server required.",
5
5
  "main": "signalingserver.js",
6
6
  "module": "signalingserver.js",
@@ -18,7 +18,9 @@
18
18
  },
19
19
  "keywords": [
20
20
  "signaling",
21
- "signaling server"
21
+ "signaling server",
22
+ "peer discovery",
23
+ "webrtc signaling"
22
24
  ],
23
25
  "author": "Nuzulul Zulkarnain",
24
26
  "license": "MIT",
@@ -1,13 +1,13 @@
1
1
  /**
2
- * signalingserver.js by Nuzulul Zulkarnain
3
- * https://github.com/nuzulul/signalingserver.js
4
- **/
2
+ * signalingserver.js - Alternative signaling server that works in browser, no server required.
3
+ * https://github.com/nuzulul/signalingserver.js
4
+ * License MIT 2026 - Nuzulul Zulkarnain
5
+ */
5
6
 
6
7
  const defaultTrackers = [
7
8
  'wss://tracker.webtorrent.dev',
8
9
  'wss://tracker.openwebtorrent.com',
9
- 'wss://tracker.btorrent.xyz',
10
- 'wss://tracker.files.fm:7073/announce'
10
+ 'wss://tracker.btorrent.xyz'
11
11
  ];
12
12
  const defaultAppId = 'global';
13
13
  const appName = 'signaling.js';
@@ -23,6 +23,7 @@ const createId = () => new Array(20).fill().map(()=>charSet[Math.floor(Math.rand
23
23
  const myid = createId();
24
24
  const encodeBytes = txt => new TextEncoder().encode(txt);
25
25
  let handledOffers = {}
26
+ let handledAnswer = {}
26
27
 
27
28
  const createSignalingServer = (config={}) => {
28
29
 
@@ -50,15 +51,15 @@ const createSignalingServer = (config={}) => {
50
51
  .slice(0,hashLimit)
51
52
  )
52
53
 
53
- const send = async (content) => {
54
+ const send = async (content,to_offer_id) => {
54
55
  const infoHash = await createInfoHash;
55
56
  const offer_id = createId()
56
57
  config.tracker.forEach(async url => {
57
58
  const socket = makeSocket(url,infoHash);
58
59
  if(socket.readyState === WebSocket.OPEN){
59
- announce(socket,infoHash,content,offer_id);
60
+ announce(socket,infoHash,content,offer_id,to_offer_id);
60
61
  }else if(socket.readyState !== WebSocket.CONNECTING){
61
- announce(await makeSocket(url,infoHash),infoHash,content,offer_id);
62
+ announce(await makeSocket(url,infoHash),infoHash,content,offer_id,to_offer_id);
62
63
  }
63
64
  })
64
65
  }
@@ -78,24 +79,52 @@ const createSignalingServer = (config={}) => {
78
79
  return sockets[url];
79
80
  }
80
81
 
81
- const announce = async (socket,infoHash,content,offer_id) =>
82
- socket.send(
83
- JSON.stringify({
84
- action : trackerAction,
85
- info_hash: infoHash,
86
- peer_id: myid,
87
- numwant:offerPoolSize,
88
- offers:[
89
- {
90
- offer:{
91
- type: 'offer',
92
- sdp:content
93
- },
94
- offer_id
82
+ const announce = async (socket,infoHash,content,offer_id,to_id) => {
83
+
84
+ if(to_id){
85
+
86
+ let id;
87
+ try{
88
+ id = JSON.parse(atob(to_id));
89
+ }catch(e){
90
+ console.warn(e);
91
+ return;
92
+ }
93
+
94
+ socket.send(
95
+ JSON.stringify({
96
+ action : trackerAction,
97
+ info_hash : infoHash,
98
+ peer_id : myid,
99
+ to_peer_id : id.peer_id,
100
+ offer_id : id.offer_id,
101
+ answer : {
102
+ type : 'answer',
103
+ sdp :content
95
104
  }
96
- ]
97
- })
98
- )
105
+ })
106
+ )
107
+
108
+ } else {
109
+ socket.send(
110
+ JSON.stringify({
111
+ action : trackerAction,
112
+ info_hash : infoHash,
113
+ peer_id : myid,
114
+ numwant : offerPoolSize,
115
+ offers : [
116
+ {
117
+ offer : {
118
+ type : 'offer',
119
+ sdp : content
120
+ },
121
+ offer_id
122
+ }
123
+ ]
124
+ })
125
+ )
126
+ }
127
+ }
99
128
 
100
129
  const onSocketMessage = async (socket,e) => {
101
130
  const infoHash = await createInfoHash;
@@ -123,14 +152,32 @@ const createSignalingServer = (config={}) => {
123
152
  return;
124
153
  }
125
154
 
126
- if(handledOffers[val.offer_id]){
127
- return;
128
- }
129
-
130
- handledOffers[val.offer_id] = true;
155
+
131
156
 
132
157
  if(val.offer){
133
- contentHandler(val.offer.sdp);
158
+
159
+ if(handledOffers[val.offer_id]){
160
+ return;
161
+ }
162
+
163
+ handledOffers[val.offer_id] = true;
164
+
165
+ const id = btoa(JSON.stringify({
166
+ offer_id : val.offer_id,
167
+ peer_id : val.peer_id
168
+ }))
169
+ contentHandler(val.offer.sdp, id);
170
+ }
171
+
172
+ if(val.answer){
173
+
174
+ if(handledAnswer[val.offer_id]){
175
+ return;
176
+ }
177
+
178
+ handledAnswer[val.offer_id] = true;
179
+
180
+ contentHandler(val.answer.sdp);
134
181
  }
135
182
 
136
183
  return;