Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 1eff740

Browse files
committed
wip: add pubsub to file exchange
1 parent 82d87bb commit 1eff740

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

examples/exchange-files-in-browser/public/app.js

+48-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const IPFS = require('ipfs')
3+
// const IPFS = require('ipfs')
44

55
// Node
66
const $nodeId = document.querySelector('.node-id')
@@ -22,6 +22,9 @@ const $allDisabledButtons = document.querySelectorAll('button:disabled')
2222
const $allDisabledInputs = document.querySelectorAll('input:disabled')
2323
const $allDisabledElements = document.querySelectorAll('.disabled')
2424

25+
const FILES = []
26+
const workspace = location.hash
27+
2528
let node
2629
let info
2730
let Buffer
@@ -33,6 +36,9 @@ let Buffer
3336
function start () {
3437
if (!node) {
3538
const options = {
39+
EXPERIMENTAL: {
40+
pubsub: true
41+
},
3642
repo: 'ipfs-' + Math.random(),
3743
config: {
3844
Addresses: {
@@ -41,7 +47,8 @@ function start () {
4147
}
4248
}
4349

44-
node = new IPFS(options)
50+
// node = new IPFS(options)
51+
node = new window.Ipfs(options)
4552

4653
Buffer = node.types.Buffer
4754

@@ -54,10 +61,46 @@ function start () {
5461
setInterval(refreshPeerList, 1000)
5562
})
5663
.catch((error) => onError(err))
64+
65+
subscribeToWorkpsace()
5766
})
5867
}
5968
}
6069

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+
61104
/* ===========================================================================
62105
Files handling
63106
=========================================================================== */
@@ -89,6 +132,9 @@ function appendFile (name, hash, size, data) {
89132
row.appendChild(downloadCell)
90133

91134
$fileHistory.insertBefore(row, $fileHistory.firstChild)
135+
136+
FILES.push(hash)
137+
publishHash(hash)
92138
}
93139

94140
function getFile () {

examples/exchange-files-in-browser/public/index.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ <h2>Files</h2>
8484
</div>
8585
</main>
8686

87+
<script src="//unpkg.com/ipfs/dist/index.js"></script>
88+
<script src="app.js"></script>
89+
8790
<!-- The app bundled with IPFS -->
88-
<script src="bundle.js"></script>
91+
<!-- <script src="bundle.js"></script> -->
8992
</body>
9093
</html>

0 commit comments

Comments
 (0)