webpeerjs 0.2.0 → 0.2.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 +13 -19
- package/package.json +2 -2
- package/src/config.js +1 -1
- package/src/webpeer.js +66 -66
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# WebPEER
|
|
1
|
+
# WebPEER.js
|
|
2
2
|
|
|
3
3
|
WebPEER is a P2P Network that Runs in a Standard Browser.
|
|
4
4
|
|
|
@@ -26,25 +26,23 @@ WebPEER Network run over [`libp2p gossipsub`](https://docs.libp2p.io/concepts/se
|
|
|
26
26
|
* Voting / Polling
|
|
27
27
|
* Collaborative activity
|
|
28
28
|
* IoT
|
|
29
|
-
*
|
|
29
|
+
* social media
|
|
30
30
|
* Remote control
|
|
31
31
|
* Multiplayer games
|
|
32
|
-
*
|
|
32
|
+
* Distributed web
|
|
33
33
|
* Signalling protocol
|
|
34
34
|
* Location tracker
|
|
35
|
-
*
|
|
35
|
+
* Activity tracker.
|
|
36
36
|
|
|
37
37
|
## Try it out!
|
|
38
38
|
|
|
39
|
-
* Go to a deployed chat demo at : [
|
|
39
|
+
* Go to a deployed chat demo at : [p2pchat](https://nuzulul.github.io/webpeerjs/demo/chat.html) .
|
|
40
40
|
* Open the app on another device.
|
|
41
41
|
* Both your devices should connected.
|
|
42
42
|
* Now start sending message.
|
|
43
43
|
|
|
44
44
|
## Browser Support
|
|
45
|
-

|
|
46
|
-
--- | --- | --- | --- | --- | --- |
|
|
47
|
-
Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ |
|
|
45
|
+
     
|
|
48
46
|
|
|
49
47
|
## Quickstart
|
|
50
48
|
|
|
@@ -102,31 +100,27 @@ Create a new peer node.
|
|
|
102
100
|
|
|
103
101
|
`config` - Configuration object contains:
|
|
104
102
|
|
|
105
|
-
- `networkName` - Unique identifier of your network.
|
|
103
|
+
- `networkName` - Unique identifier name of your network.
|
|
106
104
|
|
|
107
105
|
- `rtcConfiguration` - **(optional)** Custom [rtcConfiguration](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/RTCPeerConnection) for WebRTC transport.
|
|
108
106
|
|
|
109
107
|
### `peer.id`
|
|
110
108
|
|
|
111
|
-
Get the unique ID of the node
|
|
109
|
+
Get the unique ID of the peer node.
|
|
112
110
|
|
|
113
111
|
### `peer.status`
|
|
114
112
|
|
|
115
|
-
Get the node status, returns `connected` or `
|
|
116
|
-
|
|
117
|
-
### `peer.peers`
|
|
118
|
-
|
|
119
|
-
Get all connected peers.
|
|
113
|
+
Get the peer node status, returns `connected` or `disconnected`.
|
|
120
114
|
|
|
121
115
|
### `room = peer.joinRoom(namespace)`
|
|
122
116
|
|
|
123
|
-
Join to
|
|
117
|
+
Join to a room, returns an object.
|
|
124
118
|
|
|
125
|
-
- `room.sendMessage()` - Function to broadcast message to room
|
|
119
|
+
- `room.sendMessage()` - Function to broadcast message to the room.
|
|
126
120
|
- `romm.onMessage((message,id)=>{})` - Listen on incoming broadcast message.
|
|
127
|
-
- `room.onMembersChange((members)=>{})` - Listen on room members update.
|
|
121
|
+
- `room.onMembersChange((members)=>{})` - Listen on the room members update.
|
|
128
122
|
|
|
129
|
-
##
|
|
123
|
+
## See Also
|
|
130
124
|
|
|
131
125
|
- [p2p.js](https://github.com/nuzulul/p2p.js) - Alternative simple api WebRTC library with auto matchmaking without signaling server.
|
|
132
126
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webpeerjs",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "P2P Network that Runs in a Standard Browser",
|
|
5
|
+
"description": "WebPEER is P2P Network that Runs in a Standard Browser",
|
|
6
6
|
"main": "./src/webpeer.js",
|
|
7
7
|
"module": "./src/webpeer.js",
|
|
8
8
|
"exports": {
|
package/src/config.js
CHANGED
package/src/webpeer.js
CHANGED
|
@@ -113,9 +113,6 @@ class webpeerjs{
|
|
|
113
113
|
//callback to websocket dialable
|
|
114
114
|
#onWebsocketFn
|
|
115
115
|
|
|
116
|
-
//time tracker for sending message
|
|
117
|
-
#sendMessageTimeTracker
|
|
118
|
-
|
|
119
116
|
id
|
|
120
117
|
status
|
|
121
118
|
IPFS
|
|
@@ -150,7 +147,6 @@ class webpeerjs{
|
|
|
150
147
|
this.#lastTimeReceiveData = new Date().getTime()
|
|
151
148
|
this.#onConnectQueue = []
|
|
152
149
|
this.#msgTimeTracker = new Map()
|
|
153
|
-
this.#sendMessageTimeTracker = 0
|
|
154
150
|
|
|
155
151
|
this.peers = (function(f) {
|
|
156
152
|
return f
|
|
@@ -160,7 +156,7 @@ class webpeerjs{
|
|
|
160
156
|
return libp2p.status
|
|
161
157
|
})(this.#libp2p);
|
|
162
158
|
|
|
163
|
-
this.status = '
|
|
159
|
+
this.status = 'disconnected'
|
|
164
160
|
|
|
165
161
|
this.IPFS = (function(libp2p,discoveredPeers) {
|
|
166
162
|
const obj = {libp2p,discoveredPeers}
|
|
@@ -191,7 +187,13 @@ class webpeerjs{
|
|
|
191
187
|
}
|
|
192
188
|
this.#dialMultiaddress(mddrs)
|
|
193
189
|
}
|
|
194
|
-
})
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
const broadcast = {
|
|
193
|
+
id : this.id,
|
|
194
|
+
address : this.address
|
|
195
|
+
}
|
|
196
|
+
signalingserver.send(broadcast);
|
|
195
197
|
|
|
196
198
|
setInterval(()=>{
|
|
197
199
|
const broadcast = {
|
|
@@ -199,7 +201,7 @@ class webpeerjs{
|
|
|
199
201
|
address : this.address
|
|
200
202
|
}
|
|
201
203
|
signalingserver.send(broadcast);
|
|
202
|
-
},
|
|
204
|
+
},15*1000)
|
|
203
205
|
|
|
204
206
|
|
|
205
207
|
|
|
@@ -676,6 +678,16 @@ class webpeerjs{
|
|
|
676
678
|
this.address = addrs
|
|
677
679
|
this.#ping()
|
|
678
680
|
this.#peerDiscoveryHybrid()
|
|
681
|
+
if(addrs.length > 0){
|
|
682
|
+
const broadcast = {
|
|
683
|
+
id : this.id,
|
|
684
|
+
address : this.address
|
|
685
|
+
}
|
|
686
|
+
signalingserver.send(broadcast);
|
|
687
|
+
this.status = 'connected'
|
|
688
|
+
}else{
|
|
689
|
+
this.status = 'disconnected'
|
|
690
|
+
}
|
|
679
691
|
})
|
|
680
692
|
|
|
681
693
|
|
|
@@ -830,17 +842,17 @@ class webpeerjs{
|
|
|
830
842
|
|
|
831
843
|
joinRoom = room => {
|
|
832
844
|
if (this.#rooms[room]) {
|
|
833
|
-
return
|
|
834
|
-
this.#rooms[room].sendMessage,
|
|
835
|
-
this.#rooms[room].listenMessage,
|
|
836
|
-
this.#rooms[room].onMembersChange
|
|
837
|
-
|
|
845
|
+
return {
|
|
846
|
+
sendMessage : this.#rooms[room].sendMessage,
|
|
847
|
+
onMessage : this.#rooms[room].listenMessage,
|
|
848
|
+
onMembersChange : this.#rooms[room].onMembersChange
|
|
849
|
+
}
|
|
838
850
|
|
|
839
851
|
|
|
840
852
|
}
|
|
841
853
|
|
|
842
854
|
if (!room) {
|
|
843
|
-
throw mkErr('
|
|
855
|
+
throw mkErr('Room name is required')
|
|
844
856
|
}
|
|
845
857
|
|
|
846
858
|
//join room version 1 user pupsub via pupsub peer discovery
|
|
@@ -859,17 +871,12 @@ class webpeerjs{
|
|
|
859
871
|
onMessage : () => {},
|
|
860
872
|
listenMessage : f => (this.#rooms[room] = {...this.#rooms[room], onMessage: f}),
|
|
861
873
|
sendMessage : async (message) => {
|
|
862
|
-
const now = (new Date()).getTime()
|
|
863
|
-
//if(now-this.#sendMessageTimeTracker < 1000){
|
|
864
|
-
//throw mkErr('can not send more than 1 message/s')
|
|
865
|
-
//}
|
|
866
|
-
this.#sendMessageTimeTracker = now
|
|
867
874
|
const msgId = crypto.randomUUID();
|
|
868
875
|
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,room,message,id:this.#libp2p.peerId.toString(),msgId})
|
|
869
876
|
const arr = uint8ArrayFromString(data)
|
|
870
877
|
const sizelimit = config.CONFIG_MESSAGE_SIZE_LIMIT
|
|
871
878
|
if(arr.byteLength > sizelimit){
|
|
872
|
-
throw mkErr(`
|
|
879
|
+
throw mkErr(`Only message less than ${sizelimit} byte allowed`);
|
|
873
880
|
}
|
|
874
881
|
const peer = {
|
|
875
882
|
publicKey: this.#libp2p.peerId.publicKey,
|
|
@@ -976,12 +983,6 @@ class webpeerjs{
|
|
|
976
983
|
const item = {id:peer[0],address:peer[1].addrs}
|
|
977
984
|
this.#connectedPeersArr.push(item)
|
|
978
985
|
}
|
|
979
|
-
if(this.#connectedPeers.size > 0){
|
|
980
|
-
this.status = 'connected'
|
|
981
|
-
}
|
|
982
|
-
else{
|
|
983
|
-
this.status = 'unconnected'
|
|
984
|
-
}
|
|
985
986
|
this.#ping()
|
|
986
987
|
}
|
|
987
988
|
|
|
@@ -1193,44 +1194,6 @@ class webpeerjs{
|
|
|
1193
1194
|
}
|
|
1194
1195
|
}
|
|
1195
1196
|
|
|
1196
|
-
async #findHybridPeer(){
|
|
1197
|
-
|
|
1198
|
-
return;
|
|
1199
|
-
|
|
1200
|
-
if(!navigator.onLine)return
|
|
1201
|
-
if(!this.#isDialEnabled)return
|
|
1202
|
-
if(this.#connectedPeers.size > 5)return
|
|
1203
|
-
|
|
1204
|
-
for(const target of config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
|
|
1205
|
-
if(!this.#isConnected(target) && !this.#connections.has(target)){
|
|
1206
|
-
//console.log('findPeer',target)
|
|
1207
|
-
const peerId = peerIdFromString(target)
|
|
1208
|
-
//const peerInfo = await this.#libp2p.services.aminoDHT.findPeer(peerId)
|
|
1209
|
-
|
|
1210
|
-
//console.info(peerInfo)
|
|
1211
|
-
for await (const event of this.#libp2p.services.aminoDHT.findPeer(peerId)){
|
|
1212
|
-
//console.info('findPeer',event)
|
|
1213
|
-
if (event.name === 'FINAL_PEER'){
|
|
1214
|
-
//console.log(event.peer.id.toString(),event.peer.multiaddrs.toString())
|
|
1215
|
-
let mddrs = []
|
|
1216
|
-
let addrs = []
|
|
1217
|
-
const id = event.peer.id.toString()
|
|
1218
|
-
for(const mddr of event.peer.multiaddrs){
|
|
1219
|
-
const peeraddr = mddr.toString()+'/p2p/'+id
|
|
1220
|
-
const peermddr = multiaddr(peeraddr)
|
|
1221
|
-
addrs.push(peeraddr)
|
|
1222
|
-
mddrs.push(peermddr)
|
|
1223
|
-
}
|
|
1224
|
-
this.#dialedKnownBootstrap.set(id,addrs)
|
|
1225
|
-
if(!this.#isConnected(id)){
|
|
1226
|
-
this.#dialMultiaddress(mddrs)
|
|
1227
|
-
}
|
|
1228
|
-
}
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
}
|
|
1232
|
-
}
|
|
1233
|
-
|
|
1234
1197
|
|
|
1235
1198
|
//check the last seen in web peer
|
|
1236
1199
|
#trackLastSeen(){
|
|
@@ -1665,6 +1628,45 @@ class webpeerjs{
|
|
|
1665
1628
|
},5000)
|
|
1666
1629
|
}
|
|
1667
1630
|
|
|
1631
|
+
async #findHybridPeer(){
|
|
1632
|
+
|
|
1633
|
+
this.#dialKnownID();
|
|
1634
|
+
return;
|
|
1635
|
+
|
|
1636
|
+
if(!navigator.onLine)return
|
|
1637
|
+
if(!this.#isDialEnabled)return
|
|
1638
|
+
if(this.#connectedPeers.size > 5)return
|
|
1639
|
+
|
|
1640
|
+
for(const target of config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
|
|
1641
|
+
if(!this.#isConnected(target) && !this.#connections.has(target)){
|
|
1642
|
+
//console.log('findPeer',target)
|
|
1643
|
+
const peerId = peerIdFromString(target)
|
|
1644
|
+
//const peerInfo = await this.#libp2p.services.aminoDHT.findPeer(peerId)
|
|
1645
|
+
|
|
1646
|
+
//console.info(peerInfo)
|
|
1647
|
+
for await (const event of this.#libp2p.services.aminoDHT.findPeer(peerId)){
|
|
1648
|
+
//console.info('findPeer',event)
|
|
1649
|
+
if (event.name === 'FINAL_PEER'){
|
|
1650
|
+
//console.log(event.peer.id.toString(),event.peer.multiaddrs.toString())
|
|
1651
|
+
let mddrs = []
|
|
1652
|
+
let addrs = []
|
|
1653
|
+
const id = event.peer.id.toString()
|
|
1654
|
+
for(const mddr of event.peer.multiaddrs){
|
|
1655
|
+
const peeraddr = mddr.toString()+'/p2p/'+id
|
|
1656
|
+
const peermddr = multiaddr(peeraddr)
|
|
1657
|
+
addrs.push(peeraddr)
|
|
1658
|
+
mddrs.push(peermddr)
|
|
1659
|
+
}
|
|
1660
|
+
this.#dialedKnownBootstrap.set(id,addrs)
|
|
1661
|
+
if(!this.#isConnected(id)){
|
|
1662
|
+
this.#dialMultiaddress(mddrs)
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
}
|
|
1669
|
+
|
|
1668
1670
|
async #dialSavedKnownID(){
|
|
1669
1671
|
|
|
1670
1672
|
if(!navigator.onLine)return
|
|
@@ -2022,9 +2024,7 @@ const createWebPEER = async (configuration) => {
|
|
|
2022
2024
|
webSockets(),
|
|
2023
2025
|
webRTC(webrtcconfig),
|
|
2024
2026
|
circuitRelayTransport({
|
|
2025
|
-
|
|
2026
|
-
reservationConcurrency: 1,
|
|
2027
|
-
maxReservationQueueLength: 3
|
|
2027
|
+
reservationConcurrency: config.CONFIG_DISCOVER_RELAYS
|
|
2028
2028
|
}),
|
|
2029
2029
|
],
|
|
2030
2030
|
connectionManager: {
|