├── .gitignore ├── example.js ├── index.js ├── package.json └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var memdb = require('memdb') 2 | var PeerStatus = require('.') 3 | 4 | var user = PeerStatus() 5 | var i = 0 6 | 7 | user.open(function () { 8 | createFriend(function (err, key) { 9 | if (err) throw err 10 | user.addPeer(key) 11 | }) 12 | user.on('peer-data', function (data) { 13 | console.log(data) 14 | }) 15 | // setTimeout(function () { 16 | // createFriend(function (err, key) { 17 | // user.addFriend(key) 18 | // }) 19 | // }, 3000) 20 | }) 21 | 22 | function createFriend (cb) { 23 | var friend = PeerStatus({db: memdb()}) 24 | var name = 'robot-' + i 25 | i++ 26 | 27 | console.log('making a friend', name) 28 | friend.open(function () { 29 | cb(null, friend.key) 30 | setInterval(function () { 31 | doStatus() 32 | }, 5000) 33 | doStatus() 34 | }) 35 | 36 | function doStatus () { 37 | friend.appendStatus({name: name, status: 'online', message: 'beep boop ' + new Date()}, function (err) { 38 | if (err) throw cb(err) 39 | }) 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var events = require('events') 2 | var path = require('path') 3 | var util = require('util') 4 | var hypercore = require('hypercore') 5 | var createSwarm = require('hyperdrive-archive-swarm') 6 | var thunky = require('thunky') 7 | 8 | module.exports = PeerStatus 9 | 10 | function PeerStatus (opts) { 11 | if (!(this instanceof PeerStatus)) return new PeerStatus(opts) 12 | opts = opts || {} 13 | events.EventEmitter.call(this) 14 | 15 | var self = this 16 | self.options = opts 17 | self.status = null 18 | self.open = thunky(open) 19 | 20 | if (!opts.db) var dbDir = path.join(opts.home || require('os-homedir')(), '.peer-status.db') 21 | self.db = opts.db || require('level-party')(dbDir, {valueEncoding: 'json'}) 22 | self.core = hypercore(self.db) 23 | 24 | function open (cb) { 25 | self._open(cb) 26 | } 27 | 28 | self._emitError = function (err) { 29 | if (err) self.emit('error', err) 30 | } 31 | } 32 | 33 | util.inherits(PeerStatus, events.EventEmitter) 34 | 35 | PeerStatus.prototype._open = function (cb) { 36 | var self = this 37 | self.db.get('!peerstatus!!user', function (_, keys) { 38 | var newUser = !(keys) 39 | keys = keys || {} 40 | self._feed = self.core.createFeed(keys.user, {live: true}) 41 | self._peerKeyFeed = self.core.createFeed(keys.peers, {live: true}) 42 | self.key = self._feed.key.toString('hex') 43 | if (newUser) { 44 | keys = { 45 | user: self.key, 46 | peers: self._peerKeyFeed.key.toString('hex') 47 | } 48 | return self.db.put('!peerstatus!!user', keys, done) 49 | } 50 | self._feed.open(function (err) { 51 | if (err) return cb(err) 52 | if (!self._feed.blocks) return done() 53 | self._feed.get(Math.max(self._feed.blocks - 1, 1), function (err, data) { 54 | if (err) return cb(err) 55 | self.status = JSON.parse(data.toString()) 56 | done() 57 | }) 58 | }) 59 | 60 | function done () { 61 | self._peerKeyFeed.open(function () { 62 | self._connectPeers() 63 | self._swarm = createSwarm(self._feed) 64 | cb() 65 | }) 66 | } 67 | }) 68 | } 69 | 70 | PeerStatus.prototype.appendStatus = function (data, cb) { 71 | var self = this 72 | cb = cb || self._emitError 73 | self._feed.append(JSON.stringify(data), function (err) { 74 | if (err) return cb(err) 75 | self.status = data 76 | cb() 77 | }) 78 | } 79 | 80 | PeerStatus.prototype.addPeer = function (key, cb) { 81 | this._peerKeyFeed.append(key, cb) 82 | } 83 | 84 | PeerStatus.prototype.removePeer = function (key, cb) { 85 | this._peerSwarms[key].close() 86 | // TODO delete(?!) in hypercore feed 87 | cb() 88 | } 89 | 90 | PeerStatus.prototype._connectPeers = function () { 91 | var self = this 92 | var keyStream = self._peerKeyFeed.createReadStream({live: true}) 93 | self._peerSwarms = [] 94 | 95 | keyStream.on('data', function (data) { 96 | var key = data.toString() 97 | var feed = self.core.createFeed(key, {sparse: true}) 98 | var swarm = createSwarm(feed, {upload: false}) 99 | self._peerSwarms[key] = swarm 100 | feed.open(function () { 101 | if (feed.blocks) return connectFeed() 102 | feed.prioritize({start: 0}) 103 | feed.once('update', function () { 104 | // wait for first block to get appended 105 | feed.unprioritize({start: 0}) 106 | connectFeed() 107 | }) 108 | }) 109 | 110 | function connectFeed () { 111 | var dataStream = feed.createReadStream({live: true, start: feed.blocks - 1}) 112 | dataStream.on('data', function (data) { 113 | // todo: turn into stream 114 | self.emit('peer-data', { 115 | key: key, 116 | data: JSON.parse(data.toString()) 117 | }) 118 | }) 119 | } 120 | }) 121 | } 122 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "peer-status-feed", 3 | "version": "1.2.0", 4 | "description": "Share a status feed and subscribe to peer feeds.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "standard" 8 | }, 9 | "author": "Joe Hand (https://joeahand.com/)", 10 | "license": "MIT", 11 | "dependencies": { 12 | "hypercore": "^4.10.0", 13 | "hyperdrive-archive-swarm": "^4.1.2", 14 | "level-party": "^3.0.4", 15 | "os-homedir": "^1.0.1", 16 | "thunky": "^1.0.1" 17 | }, 18 | "devDependencies": { 19 | "memdb": "^1.3.1" 20 | }, 21 | "repository": { 22 | "type": "git", 23 | "url": "git+https://github.com/joehand/peer-status-feed.git" 24 | }, 25 | "bugs": { 26 | "url": "https://github.com/joehand/peer-status-feed/issues" 27 | }, 28 | "homepage": "https://github.com/joehand/peer-status-feed#readme", 29 | "keywords": [ 30 | "dat", 31 | "hypercore", 32 | "p2p" 33 | ] 34 | } 35 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Peer Status Feed 2 | 3 | Like a letter but it doesn't need to go to the post office, stamps are free, and all your friends receive it just by having your address. 4 | 5 | Share your status with friends! Whenever something consequential happens, the user can share their deepest feelings to any friends they've shared their key with. All data is sent directly between friends. 6 | 7 | The user can update their own feed with status updates (i.e. arbitrary data). The user can also subscribe to friends' feeds. Social! 8 | 9 | Need a friend to test peer-status-feed with? [Peer robot](https://github.com/joehand/peer-robot) is a friend that can beep and boop status updates to you! 10 | 11 | ## Installation 12 | 13 | ``` 14 | npm install peer-status-feed 15 | ``` 16 | 17 | ## Usage 18 | 19 | Create a `PeerStatus` feed for the user. Add friends! Add status updates! Have a party on peer to peer networks. 20 | 21 | Feeds are download only, so friends will only get your latest status if you are online or if they have it saved (not via other friends). 22 | 23 | On connection, it automatically connects to old friends (like the good old days) and their most recent status update is downloaded. As friend updates happen, they will be downloaded if connection stays open. Previously downloaded updates are cached and available without connection. 24 | 25 | See `example.js` for example. 26 | 27 | Check out [are-you-around](https://github.com/joehand/are-you-around) and [peer-robot](https://github.com/joehand/peer-robot) to see how to use it to make apps and robots. 28 | 29 | ## API 30 | 31 | ### var user = PeerStatus(opts) 32 | 33 | options are: 34 | 35 | * `db`: level compatible database 36 | * `home`: base dir to put database (if not specified in `opts.db`) 37 | 38 | ### user.open(cb) 39 | 40 | opens database and hypercore feeds. 41 | 42 | If an existing user is in database, it will also connect to existing friend feeds. 43 | 44 | ### user.appendStatus(data, [cb]) 45 | 46 | Append a status update to the feed. Data can be any object to be json serialized. 47 | 48 | ### user.addPeer(key, cb) 49 | 50 | Add a friend (peer)! Will connect to the friend's status feed and download the latest status update. 51 | 52 | ### user.on('peer-data', data) 53 | 54 | Emitted when user receives data from peer (`data = {key: peerKey, data: {}`) 55 | 56 | ### user.key 57 | 58 | Key to share to with friends to subscribe to status. Populated after open. 59 | 60 | ### user.status 61 | 62 | Most recent user status. Populated after open for existing user. 63 | 64 | ## License 65 | 66 | MIT --------------------------------------------------------------------------------