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

Commit bd55179

Browse files
committed
chore: add pubsub to file exchange
1 parent 82d87bb commit bd55179

File tree

1 file changed

+53
-5
lines changed
  • examples/exchange-files-in-browser/public

1 file changed

+53
-5
lines changed

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

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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: {
@@ -52,16 +58,51 @@ function start () {
5258
updateView('ready', node)
5359
onSuccess('Node is ready.')
5460
setInterval(refreshPeerList, 1000)
61+
setInterval(sendFileList, 10000)
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+
// append new files when someone uploads them
80+
if (myNode !== messageSender && !isFileInList(hash)) {
81+
$multihashInput.value = hash
82+
getFile()
83+
}
84+
}
85+
86+
const subscribeToWorkpsace = () => {
87+
node.pubsub.subscribe(workspace, messageHandler)
88+
.catch(() => onError('An error occurred when subscribing to the workspace.'))
89+
}
90+
91+
const publishHash = (hash) => {
92+
const data = Buffer.from(hash)
93+
94+
node.pubsub.publish(workspace, data)
95+
.catch((error) => onError('An error occurred when publishing the message.'))
96+
}
97+
6198
/* ===========================================================================
6299
Files handling
63100
=========================================================================== */
64101

102+
const isFileInList = (hash) => FILES.indexOf(hash) !== -1
103+
104+
const sendFileList = () => FILES.forEach((hash) => publishHash(hash))
105+
65106
function appendFile (name, hash, size, data) {
66107
const file = new window.Blob([data], { type: 'application/octet-binary' })
67108
const url = window.URL.createObjectURL(file)
@@ -89,22 +130,28 @@ function appendFile (name, hash, size, data) {
89130
row.appendChild(downloadCell)
90131

91132
$fileHistory.insertBefore(row, $fileHistory.firstChild)
133+
134+
publishHash(hash)
92135
}
93136

94137
function getFile () {
95-
const cid = $multihashInput.value
138+
const hash = $multihashInput.value
96139

97140
$multihashInput.value = ''
98141

99-
if (!cid) {
142+
if (!hash) {
100143
return onError('No multihash was inserted.')
144+
} else if (isFileInList(hash)) {
145+
return onSuccess('The file is already in the current workspace.')
101146
}
102147

103-
node.files.get(cid)
148+
FILES.push(hash)
149+
150+
node.files.get(hash)
104151
.then((files) => {
105152
files.forEach((file) => {
106153
if (file.content) {
107-
appendFile(file.name, cid, file.size, file.content)
154+
appendFile(file.name, hash, file.size, file.content)
108155
onSuccess(`The ${file.name} file was added.`)
109156
$emptyRow.style.display = 'none'
110157
}
@@ -260,12 +307,13 @@ function updateView (state, ipfs) {
260307
const startApplication = () => {
261308
// Setup event listeners
262309
$dragContainer.addEventListener('dragenter', onDragEnter)
310+
$dragContainer.addEventListener('dragover', onDragEnter)
263311
$dragContainer.addEventListener('drop', onDrop)
264312
$dragContainer.addEventListener('dragleave', onDragLeave)
265313
$fetchButton.addEventListener('click', getFile)
266314
$connectButton.addEventListener('click', connectToPeer)
267315

268-
start();
316+
start()
269317
}
270318

271319
startApplication()

0 commit comments

Comments
 (0)