Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including ssb-channel with all npm packages installed. Try it out:

var ssbChannel = require("ssb-channel")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

ssb-channel v1.0.2

open channels for rpc between secure scuttlebot nodes

SSB Channel

open channels for rpc between secure scuttlebot nodes

example:

var muxrpc     = require('muxrpc')
var Serializer = require('pull-serializer')
var auth       = require('ssb-domain-auth')
var chan       = require('ssb-channel')

var ssb        = muxrpc(ssbManifest, false, serialize)()
var localhost  = chan.connect(ssb, 'localhost')

localhost.on('connect', function() {
  // authenticate the connection
  auth.getToken('localhost', function(err, token) {
    if (err) return localhost.close(), console.error('Token fetch failed', err)
    ssb.auth(token, function(err) {
      if (err) return localhost.close(), console.error('Auth failed', err)

      // get session info
      ssb.whoami(function(err, user) {
        console.log(user)
      })
    })
  })
})

localhost.on('error', function(err) {
  // attempt a reconnect
  console.log('Connection Error', err)
  localhost.reset()
})

localhost.on('reconnecting', function(err) {
  console.log('Attempting Reconnect')
})

function serialize (stream) {
  return Serializer(stream, JSON, {split: '\n\n'})
}

var ssbManifest = {
  // this is just subset of the ssb api
  auth: 'async',
  whoami: 'async'
}

api:

var chan = require('ssb-channel')

// create a new channel and connect to the address
var mychan = chan.connect(muxRpcAPI, address, optOnConnectCB)

// connect to a new address
mychan.connect(address, optOnConnectCB)

// drop connection
mychan.close(optOnCloseCB)

// drop connection (if present) and reconnect
// - wait is optional, defaults to immediate
mychan.reconnect({ wait: 10*1000 }, optOnConnectCB)

// events
mychan.on('connect', cb)
mychan.on('reconnect', cb)
mychan.on('error', cb) // emitted on close
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free