|
import 'babel-polyfill' |
|
import Room from 'ipfs-pubsub-room' |
|
import IPFS from 'ipfs' |
|
import ko from 'knockout' |
|
import queryString from 'query-string' |
|
|
|
// Global references for demo purposes |
|
let ipfs |
|
let viewModel |
|
|
|
const setup = async () => { |
|
// Create view model with properties to control chat |
|
function ViewModel() { |
|
let self = this |
|
// Stores username |
|
self.name = ko.observable('') |
|
// Stores current message |
|
self.message = ko.observable('') |
|
// Stores array of messages |
|
self.messages = ko.observableArray([]) |
|
// Stores local peer id |
|
self.id = ko.observable(null) |
|
// Stores whether we've successfully subscribed to the room |
|
self.subscribed = ko.observable(false) |
|
// Logs latest error (just there in case we want it) |
|
self.error = ko.observable(null) |
|
// We compute the ipns link on the fly from the peer id |
|
self.url = ko.pureComputed(() => { |
|
return `https://ipfs.io/ipns/${self.id()}` |
|
}) |
|
} |
|
// Create default view model used for binding ui elements etc. |
|
viewModel = new ViewModel() |
|
// Apply default bindings |
|
ko.applyBindings(viewModel) |
|
window.viewModel = viewModel // Just for demo purposes later! |
|
|
|
try { |
|
ipfs = new IPFS({ |
|
// We need to enable pubsub... |
|
EXPERIMENTAL: { |
|
pubsub: true |
|
}, |
|
config: { |
|
Addresses: { |
|
// ...And supply swarm address to announce on |
|
Swarm: [ |
|
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star' |
|
] |
|
} |
|
} |
|
}) |
|
} catch(err) { |
|
console.error('Failed to initialize peer', err) |
|
viewModel.error(err) // Log error... |
|
} |
|
} |
|
setup() |