Code Monkey home page Code Monkey logo

cabal-core's Introduction

cabal-core

Core database, replication, swarming, and chat APIs for cabal.

Usage

npm install cabal-core

API

var Cabal = require('cabal-node')

var cabal = Cabal([storage][, key][, opts])

Create a cabal p2p database using storage storage, which must be either a string (filepath to directory on disk) or an instance of random-access-storage.

key is a hex string of the key, without any prefixes (like cabal://).

If this is a new database, key can be omitted and will be generated.

You can pass opts.db as a levelup or leveldown instance to use persistent storage for indexing instead of using memory. For example:

var level = require('level')
var cabal = Cabal(storage, key, { db: level('/tmp/bot.db') })

cabal.getLocalKey(cb)

Returns the local user's key (as a hex string).

var ds = cabal.replicate()

Creates a new, live replication stream. This duplex stream can be piped into any transport expressed as a node stream (tcp, websockets, udp, utp, etc).

cabal.ready(cb)

Call cb() when the underlying indexes are caught up.

Channels

cabal.channels.get(function (error, channels) {})

Retrieve a list of all channel names that exist in this cabal.

cabal.channels.events.on('add', function (channel) {})

Emitted when a new channel is added to the cabal.

Messages

var rs = cabal.messages.read(channel, opts)

Returns a readable stream of messages (most recent first) from a channel.

Pass opts.limit to set a maximum number of messages to read.

cabal.messages.events.on('message', fn)

Calls fn with every new message that arrives, regardless of channel.

cabal.messages.events.on(channel, fn)

Calls fn with every new message that arrives in channel.

Network

var swarm = require('cabal-core/swarm')

cabal.swarm(cb)

Joins the P2P swarm for a cabal. This seeks out peers who are also part of this cabal by various means (internet, local network), connects to them, and replicates cabal messages between them.

The returned object is an instance of discovery-swarm.

cabal.on('peer-added', function (key) {})

Emitted when you connect to a peer. key is a hex string of their public key.

cabal.on('peer-dropped', function (key) {})

Emitted when you lose a connection to a peer. key is a hex string of their public key.

Publishing

cabal.publish(message, opts, cb)

Publish message to your feed. message must have a type field set. If not, it defaults to chat/text. In general, a message is formatted as

{
  type: 'chat/text',
  content: {
    text: 'hello world',
    channel: 'cabal-dev'
  }
}

A timestamp field is set automatically with the current system time.

type is an unrestricted field: you can make up new message types and clients will happily ignore them until someone implements support for them. Well documented types include

chat/text

{
  type: 'chat/text',
  content: {
    text: 'whatever the user wants to say',
    channel: 'some channel name. if it didnt exist before, it does now!'
  }
}

License

AGPLv3

cabal-core's People

Contributors

cblgh avatar cinnamon-bun avatar d4tocchini avatar fenwick67 avatar hackergrrl avatar lachenmayer avatar luandro avatar markbennett avatar nikolaiwarner avatar noffle avatar okdistribute avatar saranrapjs avatar

Watchers

 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.