git ssb

0+

mixmix / ssb-server-plugin-intro



Commit 660c6b9bd39795033a2324ad53d2c6b960928d2b

add notes

mix irving committed on 1/12/2018, 10:37:05 PM
Parent: 987538efa0b0ba8fb17a68dd4229d47941f0ba08

Files changed

1_simple_demo/server.jschanged
2_actual_friends/config.jschanged
2_actual_friends/server.jschanged
package-lock.jsonchanged
package.jsonchanged
3_actual_friends_remote_module/client.jsadded
3_actual_friends_remote_module/config.jsadded
3_actual_friends_remote_module/server.jsadded
README.mdadded
1_simple_demo/server.jsView
@@ -14,6 +14,7 @@
1414 const server = Server(config)
1515
1616 console.log('*** updating manifest ***')
1717 // this is required for ssb-client to consume
18 +// it's a list of methods that can be called remotely, without this code we won't be able to call our new plugin
1819 const manifest = server.getManifest()
1920 fs.writeFileSync(Path.join(config.path, 'manifest.json'), JSON.stringify(manifest))
2_actual_friends/config.jsView
@@ -2,9 +2,9 @@
22 // const Config = require('ssb-config')
33 const ssbKeys = require('ssb-keys')
44 const Path = require('path')
55
6-const appName = "ssb"
6 +const appName = "ssb" // <<< NOTE THIS IS YOUR DEFAULT IDENTITY
77 const opts = null // can set things in here
88
99 const config = Config(appName, opts)
1010 Object.assign(config, {
2_actual_friends/server.jsView
@@ -14,6 +14,7 @@
1414 const server = Server(config)
1515
1616 console.log('*** updating manifest ***')
1717 // this is required for ssb-client to consume
18 +// it's a list of methods that can be called remotely, without this code we won't be able to call our new plugin
1819 const manifest = server.getManifest()
1920 fs.writeFileSync(Path.join(config.path, 'manifest.json'), JSON.stringify(manifest))
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 138978 bytes
New file size: 140344 bytes
package.jsonView
@@ -14,7 +14,8 @@
1414 "pull-stream": "^3.6.1",
1515 "scuttlebot": "^10.5.0",
1616 "ssb-client": "^4.5.2",
1717 "ssb-config": "^2.2.0",
18- "ssb-keys": "^7.0.13"
18 + "ssb-keys": "^7.0.13",
19 + "ssb-server-actual-friends": "^1.0.0"
1920 }
2021 }
3_actual_friends_remote_module/client.jsView
@@ -1,0 +1,50 @@
1 +const Client = require('ssb-client')
2 +const config = require('./config')
3 +
4 +const map = require('lodash/map')
5 +
6 +Client(config.keys, config, (err, ssbServer) => {
7 + // ssbServer (also commonly called sbot)
8 + // a remote connection to our server
9 + if (err) throw err
10 +
11 + // console.log('methods', ssbServer)
12 + console.log('actualFriends methods:', ssbServer.actualFriends)
13 +
14 + ssbServer.whoami((err, data) => {
15 + if (err) throw err
16 + const myId = data.id
17 + const dominicId = '@EMovhfIrFk4NihAKnRNhrfRaqIhBv1Wj8pTxJNgvCCY=.ed25519'
18 +
19 + ssbServer.actualFriends.get((err, view) => {
20 + if (err) throw err
21 +
22 + // console.log(JSON.stringify(view[id.id], null, 2))
23 +
24 + // actualFriends(view, myId)
25 + checkFriendShip(view, myId, dominicId)
26 +
27 + ssbServer.close()
28 + })
29 + })
30 +})
31 +
32 +function actualFriends (view, id) {
33 + var idData = view[id]
34 + var orderedData = map(idData.mentions, (value, key) => {
35 + return { key , value }
36 + })
37 + .sort((a,b) => b.value < a.value ? -1 : 1)
38 +
39 + write(orderedData)
40 +}
41 +
42 +function checkFriendShip (view, id1, id2) {
43 + console.log(`${id1} mentions ${id2} ${view[id1].mentions[id2]} times`)
44 + console.log(`${id2} mentions ${id1} ${view[id2].mentions[id1]} times`)
45 +}
46 +
47 +function write(object) {
48 + process.stdout.write(JSON.stringify(object, null, 2))
49 +}
50 +
3_actual_friends_remote_module/config.jsView
@@ -1,0 +1,15 @@
1 +const Config = require('ssb-config/inject')
2 +// const Config = require('ssb-config')
3 +const ssbKeys = require('ssb-keys')
4 +const Path = require('path')
5 +
6 +const appName = "ssb"
7 +const opts = null // can set things in here
8 +
9 +const config = Config(appName, opts)
10 +Object.assign(config, {
11 + appName,
12 + keys: ssbKeys.loadOrCreateSync(Path.join(config.path, 'secret')),
13 +})
14 +
15 +module.exports = config
3_actual_friends_remote_module/server.jsView
@@ -1,0 +1,20 @@
1 +const Server = require('scuttlebot')
2 +const fs = require('fs')
3 +const Path = require('path')
4 +
5 +const config = require('./config')
6 +console.log('config:', config)
7 +
8 +console.log('*** installing ssb-server plugins ***')
9 +Server
10 + .use(require('scuttlebot/plugins/master'))
11 + .use(require('ssb-server-actual-friends'))
12 +
13 +console.log('*** starting ssb-server ***')
14 +const server = Server(config)
15 +
16 +console.log('*** updating manifest ***')
17 +// this is required for ssb-client to consume
18 +// it's a list of methods that can be called remotely, without this code we won't be able to call our new plugin
19 +const manifest = server.getManifest()
20 +fs.writeFileSync(Path.join(config.path, 'manifest.json'), JSON.stringify(manifest))
README.mdView
@@ -1,0 +1,66 @@
1 +# Scuttlebot plugins intro
2 +
3 +repo ([ssb](%f2PZYbacgJpNq4buNCVsjG6j55K8olw80QxPjF2Teqs=.sha256) | [github](https://www.github.com:mixmix/ssb-server-plugin-intro))
4 +
5 +In these 3 folders, I walk through a progression of making a simple plguin, through to making a plugins which creates and accesses `flumeview`
6 +
7 +## Project 1 : Simple Demo
8 +
9 +I've put the code into folders because in this project, you need to run a couple of different file seperately
10 +
11 +e.g.
12 +```
13 +▾ 1_simple_demo/
14 + client.js // start this second
15 + config.js
16 + server.js // start this first
17 + ssb-server-counter.js // our plugin
18 +```
19 +
20 +For each project you need to start a scuttlebutt-server (scuttlebot/ sbot), which loads in your plugin, and then run you can run the client code that connects to this, and executes commands. e.g.
21 +
22 +```bash
23 +node 1_simple_demo/server.js
24 +
25 +// open another terminal in this repo
26 +node 1_simple_demo/client.js
27 +```
28 +
29 +### `config.js`
30 +
31 +This first project has a config which starts the server with a **test identity** (`ssb_demo`). This means the database will start our empty (creates it at `~/.ssb_demo`) and won't have any of your Patchwork data
32 +
33 +### `manifest.json`
34 +
35 +This was a major gotcha for me. Basically `ssb-client` needs a list of methods which it is allowed to call remotely (on the server).
36 +This is provided by it consuming a `manifest.json`.
37 +You _can_ pass this in as one of the options when you instantiate your client i.e. `Client(keys, { manifest: ... }, fn)`.
38 +But it's easier to let `ssb-client` go and get it from the default location `~/.ssb/manifest.json` (or more generally `~/.<appName>/manifest.json`
39 +
40 +
41 +That's why there's this line in the **server** file:
42 +```js
43 +console.log('*** updating manifest ***')
44 +// this is required for ssb-client to consume
45 +// it's a list of methods that can be called remotely, without this code we won't be able to call our new plugin
46 +const manifest = server.getManifest()
47 +fs.writeFileSync(Path.join(config.path, 'manifest.json'), JSON.stringify(manifest))
48 +```
49 +
50 +Once the server has loaded all it's plugins, get it to generate that manifest.
51 +Then save that manifest in the place `ssb-client` will go looking for it.
52 +
53 +## Project 2
54 +
55 +### `config.js`
56 +
57 +This project sets the your identity (in `config.js`) to `ssb`. This is the default identity, so if you'vebeen running Patchwork/ Patchbay etc, this is connecting to your **real identity**.
58 +Appending messages will publish them to your friends!
59 +Views are not published so messing around with those is chill.
60 +
61 +
62 +## Project 3
63 +
64 +A trivial increment - I published the module as a plugin, and am requiring it in instead of using the local one.
65 +
66 +

Built with git-ssb-web