ssb-db

20.4.1 · active · verified Wed Apr 22

ssb-db is a core component within the Secure Scuttlebutt (SSB) ecosystem, providing a secure, replicatable, append-only database for cryptographic message feeds. It is built as a plugin for secret-stack applications, enabling peer-to-peer data synchronization and communication without a central authority. The package ensures message unforgeability through digital signing tied to unique public/private key pairs, forming immutable "feeds." Currently at version 20.4.1, ssb-db has a moderate release cadence, with several minor versions released in recent history (v20.x in 2024), indicating active maintenance and feature development. Key differentiators include its foundational role in the decentralized SSB protocol, strict append-only data model (though flexible through delta encoding for "deletions"), and built-in support for message encryption and indexing. It's designed for applications requiring resilient, offline-first, and censorship-resistant data storage.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates initializing an `ssb-db` instance using `secret-stack`, publishing a new message to the current identity's feed, and then streaming all messages from the log and specifically from the current identity's history. It showcases basic message creation and retrieval patterns.

/**
 * create an ssb-db instance and add a message to it.
 */
var pull = require('pull-stream')

//create a secret-stack instance and add ssb-db, for persistence.
var createApp = require('secret-stack')({})
  .use(require('ssb-db'))


// create the db instance.
// Only one instance may be created at a time due to os locks on port and database files.

var app = createApp(require('ssb-config'))

//your public key, the default key of this instance.
console.log("App ID:", app.id)

//or, called remotely
app.whoami(function (err, data) {
  if (err) return console.error(err)
  console.log("Whoami ID:", data.id) //your id
})

// publish a message to default identity
//  - feed.add appends a message to your key's chain.
//  - the `type` attribute is required.

app.publish({ type: 'post', text: 'My First Post!' }, function (err, msg) {
  if (err) return console.error(err)
  // the message as it appears in the database:
  console.log("Published message:", msg)

  // and its hash:
  console.log("Message key:", msg.key)

  // collect all the messages into an array, calls back, and then ends
  // https://github.com/pull-stream/pull-stream/blob/master/docs/sinks/collect.md
  pull(
    app.createLogStream(),
    pull.collect(function (err, messagesArray) {
      if (err) return console.error(err)
      console.log("All messages in log:", messagesArray)
    })
  )

  // collect all messages for a particular keypair into an array, calls back, and then ends
  // https://github.com/pull-stream/pull-stream/blob/master/docs/sinks/collect.md
  pull(
    app.createHistoryStream({id: app.id}),
    pull.collect(function (err, messagesArray) {
      if (err) return console.error(err)
      console.log("Messages for app.id:", messagesArray)
      app.close() // Close the app instance to release locks
    })
  )
})

view raw JSON →