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.
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')
})
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, ...)
}
Get notified on new client authentication request.
Data include:
{
cliPubHex: ..., // hex of pub key of connecting client
}
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
}
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, ...)
}
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
}