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

Commit

Permalink
wip: add pubsub to file exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
fsdiogo committed May 23, 2018
1 parent 82d87bb commit 1eff740
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
50 changes: 48 additions & 2 deletions examples/exchange-files-in-browser/public/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const IPFS = require('ipfs')
// const IPFS = require('ipfs')

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

const FILES = []
const workspace = location.hash

let node
let info
let Buffer
Expand All @@ -33,6 +36,9 @@ let Buffer
function start () {
if (!node) {
const options = {
EXPERIMENTAL: {
pubsub: true
},
repo: 'ipfs-' + Math.random(),
config: {
Addresses: {
Expand All @@ -41,7 +47,8 @@ function start () {
}
}

node = new IPFS(options)
// node = new IPFS(options)
node = new window.Ipfs(options)

Buffer = node.types.Buffer

Expand All @@ -54,10 +61,46 @@ function start () {
setInterval(refreshPeerList, 1000)
})
.catch((error) => onError(err))

subscribeToWorkpsace()
})
}
}

/* ===========================================================================
Pubsub
=========================================================================== */

const messageHandler = (message) => {
const myNode = info. id
const hash = message.data.toString()
const messageSender = message.from

console.log('my node id:', myNode)
console.log('received message from:', messageSender)
console.log('message is:', hash)

// append new files when someone uploads them
if (myNode !== messageSender && FILES.indexOf(hash) === -1) {
console.log(':: Append new file')
$multihashInput.value = hash
getFile()
}
}

const subscribeToWorkpsace = () => {
node.pubsub.subscribe(workspace, messageHandler)
.then(() => console.log(':: Subscribed to workspace:', workspace))
}

const publishHash = (hash) => {
const data = Buffer.from(hash)

node.pubsub.publish(workspace, data)
.then(() => console.log(':: Successfully published to workspace'))
.catch((error) => console.log(':: Error publishing:', error))
}

/* ===========================================================================
Files handling
=========================================================================== */
Expand Down Expand Up @@ -89,6 +132,9 @@ function appendFile (name, hash, size, data) {
row.appendChild(downloadCell)

$fileHistory.insertBefore(row, $fileHistory.firstChild)

FILES.push(hash)
publishHash(hash)
}

function getFile () {
Expand Down
5 changes: 4 additions & 1 deletion examples/exchange-files-in-browser/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ <h2>Files</h2>
</div>
</main>

<script src="//unpkg.com/ipfs/dist/index.js"></script>
<script src="app.js"></script>

<!-- The app bundled with IPFS -->
<script src="bundle.js"></script>
<!-- <script src="bundle.js"></script> -->
</body>
</html>

0 comments on commit 1eff740

Please sign in to comment.