Aral Balkan

Mastodon icon RSS feed icon

Kappa Architecture workshop

Kappa Architecture Workshop is an excellent online resource by Stephen Whitmore (of Cabal fame), Mathias Buus (one of the cornerstones of the DAT Project), et al., that gives you an introduction to Kappa Architecture using modules from the DAT Node.js ecosystem like hypercore, multifeed, discovery-swarm1 and kappa-core2.

The examples take you from the very basics – such as how to make peer to connections and send simple ephemeral chat messages (code below) to P2P replicated feeds with multiple writers and beyond.

You can follow along with the workshop online, view my working files as I do, and also submit any issues you may run into or improvements you might want to suggest on the workshop’s source code repository.

const discovery = require('discovery-swarm')
const swarm = discovery()

const nickname = 'person' +  Math.floor(Math.random() * 42) + 1

swarm.join('my-very-very-simple-p2p-app')

swarm.on('connection', function (connection, info) {
  console.log(`Found a peer: ${info.host}:${info.port}`)

  process.stdin.on('data', function (data) {
    connection.write(JSON.stringify({
      type: 'chat-message',
      nickname,
      text: data.toString().trim(),
      timestamp: new Date().toISOString()
    }))
  })

  connection.on('data', function (data) {
    data = JSON.parse(data)
    console.log(`${data.timestamp} ${data.nickname}: ${data.text}`)
  })
})

  1. See the new, improved version called hyperswarm, which you should be able to replace discovery-swarm with in the examples in the workshop. ↩︎

  2. For an example of kappa-core in use in a real-world library, see cabal-core, the – uhum ­– core of Cabal. ↩︎