protocol0 0.1 e6aeb8 GitHub Get started
DocumentationSend entries through a relay

Documentation  /  Build  /  Send entries through a relay

Send entries through a relay

A relay passes entries between computers that are not online at the same time. This page sends the entries of 1 copy through a relay and reads them on another copy.

1Start 2 copies

Ann starts Flood Relief Network and Bob loads his own copy from her first entry and each copy holds its own key.

import { ready, newKeyPair, publicOf, HttpRelay, sync, Copy } from 'protocol0'

await ready()
const bob = newKeyPair()
const cat = newKeyPair()

const relief = Copy.start({
  key: newKeyPair(),
  me: { name: 'Ann' },
  name: 'Flood Relief Network',
  founders: [
    { name: 'Bob', key: publicOf(bob) },
    { name: 'Cat', key: publicOf(cat) },
  ],
  pages: [{ id: 'fund', name: 'Relief fund', content: 'The fund holds $90,000.' }],
})
const bobsCopy = Copy.load({ key: bob, items: relief.items() })

2Send the entries

new HttpRelay(address) reaches a relay over HTTP and sync sends the entries that the relay lacks and takes the entries that the copy lacks.

It returns a report where reached names the relays that answered and silent names the ones that did not. A relay holds sealed entries that it cannot read and every copy refuses an entry that a relay changed.

const relay = new HttpRelay('https://relay.protocol0.org')
const code = relief.propose({
  title: 'Send $40,000 to Hill End',
  change: { type: 'page', page: 'fund', content: 'The fund holds $50,000.' },
})

const sent = await sync(relief, [relay])
const reached = sent.reached.length
console.log('Ann reached ' + reached + ' relay and ' + sent.silent.length + ' stayed silent')

The program prints that Ann reached 1 relay and that no relay stayed silent.

3Collect them on another copy

Bob calls sync with the same relay and receives the proposal. He signs it and syncs again so that Ann can sync and collect his signature.

const got = await sync(bobsCopy, [relay])
console.log('Bob received ' + got.received + ' entry from the relay')

bobsCopy.sign(code)
await sync(bobsCopy, [relay])
await sync(relief, [relay])
console.log('Ann sees the proposal as ' + relief.view().proposals[0].status)

The program prints how many entries Bob received and that Ann sees the proposal as decided.

4Let a relay stay silent

sync takes a list of relays and uses the next one when a relay does not answer so no single relay can stop a community.

The relay at http://127.0.0.1:9 below is an address where nothing listens. You can run a relay of your own as the Run your own relay page shows.

const off = new HttpRelay('http://127.0.0.1:9')
const both = await sync(relief, [relay, off])
console.log('Ann reached ' + both.reached.length + ' of 2 relays')
It prints
Ann reached 1 relay and 0 stayed silent
Bob received 1 entry from the relay
Ann sees the proposal as decided
Ann reached 1 of 2 relays

The last line the program prints says that Ann reached 1 of the 2 relays.

Something wrong on this page