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

Commit 7b6d143

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

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

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

+60-6
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

@@ -52,16 +59,57 @@ function start () {
5259
updateView('ready', node)
5360
onSuccess('Node is ready.')
5461
setInterval(refreshPeerList, 1000)
62+
// setInterval(sendFileList, 10000)
5563
})
5664
.catch((error) => onError(err))
65+
66+
subscribeToWorkpsace()
5767
})
5868
}
5969
}
6070

71+
/* ===========================================================================
72+
Pubsub
73+
=========================================================================== */
74+
75+
const messageHandler = (message) => {
76+
const myNode = info. id
77+
const hash = message.data.toString()
78+
const messageSender = message.from
79+
80+
console.log('Node ID:', myNode)
81+
console.log('Received message from:', messageSender)
82+
console.log('Message:', hash)
83+
84+
// append new files when someone uploads them
85+
if (myNode !== messageSender && !isFileInList(hash)) {
86+
console.log(':: Append new file')
87+
$multihashInput.value = hash
88+
getFile()
89+
}
90+
}
91+
92+
const subscribeToWorkpsace = () => {
93+
node.pubsub.subscribe(workspace, messageHandler)
94+
.then(() => console.log(':: Subscribed to workspace:', workspace))
95+
}
96+
97+
const publishHash = (hash) => {
98+
const data = Buffer.from(hash)
99+
100+
node.pubsub.publish(workspace, data)
101+
.then(() => console.log(':: Successfully published to workspace'))
102+
.catch((error) => console.log(':: Error publishing:', error))
103+
}
104+
61105
/* ===========================================================================
62106
Files handling
63107
=========================================================================== */
64108

109+
const isFileInList = (hash) => FILES.indexOf(hash) !== -1
110+
111+
const sendFileList = () => FILES.forEach((hash) => publishHash(hash))
112+
65113
function appendFile (name, hash, size, data) {
66114
const file = new window.Blob([data], { type: 'application/octet-binary' })
67115
const url = window.URL.createObjectURL(file)
@@ -89,22 +137,27 @@ function appendFile (name, hash, size, data) {
89137
row.appendChild(downloadCell)
90138

91139
$fileHistory.insertBefore(row, $fileHistory.firstChild)
140+
141+
publishHash(hash)
92142
}
93143

94144
function getFile () {
95-
const cid = $multihashInput.value
145+
const hash = $multihashInput.value
96146

97147
$multihashInput.value = ''
98148

99-
if (!cid) {
149+
if (!hash) {
100150
return onError('No multihash was inserted.')
151+
} else if (isFileInList(hash)) {
152+
return onSuccess('The file is already in the current workspace.')
101153
}
102154

103-
node.files.get(cid)
155+
node.files.get(hash)
104156
.then((files) => {
105157
files.forEach((file) => {
106158
if (file.content) {
107-
appendFile(file.name, cid, file.size, file.content)
159+
FILES.push(hash)
160+
appendFile(file.name, hash, file.size, file.content)
108161
onSuccess(`The ${file.name} file was added.`)
109162
$emptyRow.style.display = 'none'
110163
}
@@ -260,6 +313,7 @@ function updateView (state, ipfs) {
260313
const startApplication = () => {
261314
// Setup event listeners
262315
$dragContainer.addEventListener('dragenter', onDragEnter)
316+
$dragContainer.addEventListener('dragover', onDragEnter)
263317
$dragContainer.addEventListener('drop', onDrop)
264318
$dragContainer.addEventListener('dragleave', onDragLeave)
265319
$fetchButton.addEventListener('click', 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)