Social Messaging App
P2P Log Store
APIs, Articles
Howto Guides

Example Private Message

Send and receive encrypted messages on ssb

$ git clone https://github.com/ssbc/ssb-example-pm.git
$ cd ssb-example-pm
$ npm install

$ ./ssb-example-pm.js -h
List messages: ssb-example-pm.js
Send message:  ssb-example-pm.js {recp} [message...]

$ ./ssb-example-pm.js @hxGxqPrplLjRG2vtjQL87abX4QKqeLgCwQpS730nNwE=.ed25519 "hello, this is a big secret"
$ ./ssb-example-pm.js

a minute ago   you 

hello there, this is a big secret

How it works

Normally, messages have an object in the content attribute. If content is a string, that means the message is encrypted -- the string is the base64-encoded ciphertext. Recipients are hidden, so we try to decrypt. If successful, the message was for the user:

// list private messages
function listPMs (sbot, selfId) {
    // all type: post messages, in order received

    // encrypted?
    pull.filter(function (msg) { return typeof msg.value.content === 'string' }),

    // attempt to decrypt    
    pull.asyncMap(function (msg, cb) {
      sbot.private.unbox(msg.value.content, function (err, content) {
        if (content)
          msg.value.content = content
        cb(null, msg)

    // successfully decrypted?
    pull.filter(function (msg) { return typeof msg.value.content !== 'string' }),

    // render
    pull.drain(renderPost.bind(null, selfId), function (err) {
      if (err) throw err

To publish, we use https://github.com/ssbc/ssb-message-schemas to build the message content, and sbot.private.publish to write to the feed. Note, the local user is included in the recipients, so we can read our own messages.

function publishPM (sbot, selfId, recpId, msg) {
  var recps = (selfId !== recpId) ? [selfId, recpId] : [selfId]

  // create the type: post message
  var post = schemas.post(
    msg,  // text content
    null, // reply topmost msg
    null, // reply parent
    null, // mention links
    recps // recipient links
  sbot.private.publish(post, recps, function (err, msg) {
    if (err) throw err