Pp2pbuilders
learn › Storyteller & Pear Baby Rooms

12 min read · also at #/learn/rooms-baby-3

Pear Baby Rooms 3: a room that remembers

Rooms 1–2 are walkie-talkies: miss the moment, miss the message. Real apps
remember. The P2P way is not a shared database — it is *everyone keeps
their own signed log, and everyone replicates everyone else's*.
(Everything is a signed log.)

Each peer writes their own log

const Corestore = require('corestore')
const store = new Corestore('./room-data')

const mine = store.get({ name: 'baby-rooms/chat' })
await mine.ready()

async function say (text) {
  await mine.append(Buffer.from(JSON.stringify({ text, ts: Date.now() })))
}

Hypercore signs every block for us — lesson 2's hand-rolled signatures come
for free now.

Tell peers about your log, replicate theirs

swarm.on('connection', (socket) => {
  store.replicate(socket)                       // sync every known core
  socket.write(JSON.stringify({ core: mine.key.toString('hex') }) + '\n')
  socket.on('data', (d) => {
    for (const line of d.toString().split('\n')) {
      if (!line.trim()) continue
      const theirs = store.get(Buffer.from(JSON.parse(line).core, 'hex'))
      theirs.ready().then(() => follow(theirs))
    }
  })
})

function follow (core) {
  const print = async (i) =>
    console.log(JSON.parse((await core.get(i)).toString()).text)
  for (let i = 0; i < core.length; i++) print(i)      // history!
  core.on('append', () => print(core.length - 1))     // live tail
}

Start a room, chat, kill everything, restart: history is still there.
Join late from a third machine: the first follow loop backfills everything
you missed.

What you just learned

no write ever conflicts.

the reason append-only wins (the foundations chapter explains why).

That is availability, and relays fix it.

You now hold the exact architecture of p2pbuilders — it is this lesson plus
an indexer and anti-spam. Graduate to the
Storyteller track to turn logs into a designed app.

« Pear Baby Rooms 2: who said that? Storyteller 1: a story is a log »