Code Monkey home page Code Monkey logo

dazaar-guild's Introduction

dazaar-guild

Dazaar logo

Socket, RPC API patterns for a decentralized web based on Dazaar

npm install dazaar-guild

Learn more about Dazaar in our intro blogpost and whitepaper.

Usage

Setup server side

const { Bastion, Priv } = require('dazaar-guild')

const bastion = new Bastion({
  dir: './data',
  customValidate: (key, cb) => {
    console.log('bastion: validationRequest for', key.toString('hex'))
    cb(null)
  }
})

const MEM = {}

bastion.on('prv_auth', data => {
  if (MEM[data.cliPubHex]) {
    console.error('cli already active', cliPubHex)
    return
  }

  MEM[data.cliPubHex] = true
  priv.servePriv({
    cliPubHex: data.cliPubHex
  }, () => {
  data.confirm()
  })
})

bastion.on('message', p => {
  if (p.data === 'hello') {
    p.reply('world')
  }
})

bastion.start()

const priv = new Priv({
  dir: './data',
  customValidate: (key, cb) => {
    console.log('priv: validationRequest for', key.toString('hex'))
    cb(null)
  }
})

priv.on('message', p => {
  if (p.data === 'hello') {
    p.reply('world (priv)')
  }
})

And then client side

const { Client } = require('dazaar-guild')

const client = new Client({
  bastionKey: process.argv[2]
}, {
})

client.start()

client.on('message', p => {
  console.log('received pub message', p)
})

client.on('pub_connected', () => {
  client.bcastPub('message', 'hello')
  client.auth()
})

client.on('auth', msg => {
  console.log('received authentication handshake')
  client.connectPriv(msg)
})

client.on('prv_message', p => {
  console.log('received prv message', p)
})

client.on('prv_connected', () => {
  setTimeout(() => {
    client.bcastPriv('message', 'hello')
  }, 500)
  console.log('connected priv')
})

API

const bastion = Bastion(opts)

Create a public Dazaar-Bastion instance. Bastion inherits from EventEmitter.

Options include:

{
  dir: ..., // data directory for hypercore storage
  customValidate: (key, cb) // custom client validation logic (ideal for whitelisting, ...)
}

bastion.on('prv_auth', data => {...})

Get notified on new client authentication request.

Data include:

{
  cliPubHex: ..., // hex of pub key of connecting client
}

bastion.on('message', data => {...})

Generic message received from client, useful for chatter and build low level complex patterns (ie. RPC)

Data include:

{
  data: ..., // incoming data
  reply: ..., // function to reply back to the client
}

const priv = Priv(opts)

Create a private Dazaar instance, dedicated to a single client. Priv inherits from EventEmitter.

Options include:

{
  dir: ..., // data directory for hypercore storage
  customValidate: (key, cb) // custom client validation logic (ideal for whitelisting, ...)
}

priv.on('message', data => {...})

Generic message received from client, useful for chatter and build low level complex patterns (ie. RPC)

Data include:

{
  data: ..., // incoming data
  reply: ..., // function to reply back to the client
}

dazaar-guild's People

Contributors

prdn avatar rbndg avatar

Stargazers

Gábor Mihálcz avatar Cryptomamy avatar John Carvalho avatar

Watchers

James Cloos avatar John Carvalho avatar Davide Scola avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.