1
1
'use strict'
2
2
3
- const IPFS = require ( 'ipfs' )
3
+ // const IPFS = require('ipfs')
4
4
5
5
// Node
6
6
const $nodeId = document . querySelector ( '.node-id' )
@@ -22,6 +22,9 @@ const $allDisabledButtons = document.querySelectorAll('button:disabled')
22
22
const $allDisabledInputs = document . querySelectorAll ( 'input:disabled' )
23
23
const $allDisabledElements = document . querySelectorAll ( '.disabled' )
24
24
25
+ const FILES = [ ]
26
+ const workspace = location . hash
27
+
25
28
let node
26
29
let info
27
30
let Buffer
@@ -33,6 +36,9 @@ let Buffer
33
36
function start ( ) {
34
37
if ( ! node ) {
35
38
const options = {
39
+ EXPERIMENTAL : {
40
+ pubsub : true
41
+ } ,
36
42
repo : 'ipfs-' + Math . random ( ) ,
37
43
config : {
38
44
Addresses : {
@@ -41,7 +47,8 @@ function start () {
41
47
}
42
48
}
43
49
44
- node = new IPFS ( options )
50
+ // node = new IPFS(options)
51
+ node = new window . Ipfs ( options )
45
52
46
53
Buffer = node . types . Buffer
47
54
@@ -54,10 +61,46 @@ function start () {
54
61
setInterval ( refreshPeerList , 1000 )
55
62
} )
56
63
. catch ( ( error ) => onError ( err ) )
64
+
65
+ subscribeToWorkpsace ( )
57
66
} )
58
67
}
59
68
}
60
69
70
+ /* ===========================================================================
71
+ Pubsub
72
+ =========================================================================== */
73
+
74
+ const messageHandler = ( message ) => {
75
+ const myNode = info . id
76
+ const hash = message . data . toString ( )
77
+ const messageSender = message . from
78
+
79
+ console . log ( 'my node id:' , myNode )
80
+ console . log ( 'received message from:' , messageSender )
81
+ console . log ( 'message is:' , hash )
82
+
83
+ // append new files when someone uploads them
84
+ if ( myNode !== messageSender && FILES . indexOf ( hash ) === - 1 ) {
85
+ console . log ( ':: Append new file' )
86
+ $multihashInput . value = hash
87
+ getFile ( )
88
+ }
89
+ }
90
+
91
+ const subscribeToWorkpsace = ( ) => {
92
+ node . pubsub . subscribe ( workspace , messageHandler )
93
+ . then ( ( ) => console . log ( ':: Subscribed to workspace:' , workspace ) )
94
+ }
95
+
96
+ const publishHash = ( hash ) => {
97
+ const data = Buffer . from ( hash )
98
+
99
+ node . pubsub . publish ( workspace , data )
100
+ . then ( ( ) => console . log ( ':: Successfully published to workspace' ) )
101
+ . catch ( ( error ) => console . log ( ':: Error publishing:' , error ) )
102
+ }
103
+
61
104
/* ===========================================================================
62
105
Files handling
63
106
=========================================================================== */
@@ -89,6 +132,9 @@ function appendFile (name, hash, size, data) {
89
132
row . appendChild ( downloadCell )
90
133
91
134
$fileHistory . insertBefore ( row , $fileHistory . firstChild )
135
+
136
+ FILES . push ( hash )
137
+ publishHash ( hash )
92
138
}
93
139
94
140
function getFile ( ) {
0 commit comments