git ssb

0+

mixmix / ssb-client-basic-tutorial



Commit 732c846359bc5138a27a082a1fa03a3d8ba0bf85

v01 - a pull-stream query

mixmix committed on 10/17/2018, 7:14:35 AM
Parent: 6a6bdd02661ff78d984eb5b4df7130ae3ab01135

Files changed

README.mdchanged
v01.jsadded
README.mdView
@@ -16,4 +16,33 @@
1616 `whoami` is an asynchronous method which calls back with the details of the feed your scuttlebot is currently running. It's the basic "hello world" of scuttlebutt.
1717
1818 We close the connection to the server using `server.close()` otherwise the connection stays open forever!
1919
20 +
21 +## `v01` - a pull-stream query!
22 +
23 +We introduce [**pull-stream**](https://github.com/pull-stream/pull-stream), which is a really common way to handle data in scuttlebutt.
24 +The basic idea is a every complete pull-stream connects a source of data and runs that into a sink (some output).
25 +Along the way, your data might go _through_ some steps which filter or modify the data
26 +
27 +Have a read of `v01.js` and see if you can guess what it does.
28 +Run it by running `node v01.js` in the terminal and seeing what comes out.
29 +_Kick the tyres_ by modifying the code and running it again to see what happens!
30 +
31 +```js
32 +pull(
33 + server.query.read(opts), // the source
34 + pull.filter(msg => msg.value.content.type === 'post'), // filter 'through'
35 + pull.collect(onDone) // the sink
36 +)
37 +```
38 +
39 +The `pull` function wrapping the source, through, and sink connects these into a complete stream which data will immediately flow through.
40 +
41 +The source is provided by [**ssb-query**](https://github.com/dominictarr/ssb-query) which is super fancy, but we'll get to that later. All you need to know now is that opts says "gimme the last 100 messages going _backwards_ from right now".
42 +
43 +The `pull.filter` gets passed each one of the results that the source spits out, and we've set it up only to let `post` type messages continue on.
44 +
45 +The sink is a `pull.collect`, which waits until the stream is finished (here when we've pulled 100 messages), collecting all the results then passing them as an Array to the callback `onDone`.
46 +
47 +
48 +NOTE - you need to be using a server with the ssb-query plugin installed for this to work (most have this!)
v01.jsView
@@ -1,0 +1,42 @@
1 +const Connection = require('ssb-client')
2 +const pull = require('pull-stream')
3 +
4 +console.log('Connecting')
5 +
6 +Connection((err, server) => {
7 + if (err) throw err
8 + console.log('Connection established')
9 +
10 + const opts = {
11 + limit: 100,
12 + reverse: true
13 + }
14 +
15 + pull(
16 + server.query.read(opts),
17 + pull.filter(msg => msg.value.content.type === 'post'),
18 + pull.collect(onDone)
19 + )
20 +
21 + function onDone (err, msgs) {
22 + if (err) {
23 + console.error(err)
24 + server.close()
25 + return
26 + }
27 +
28 + msgs.forEach(msg => {
29 + prettyPrint(msg)
30 + console.log('------')
31 + })
32 +
33 + console.log(`${msgs.length} messages`)
34 + server.close()
35 + }
36 +})
37 +
38 +function prettyPrint (msg) {
39 + console.log(JSON.stringify(msg, null, 2))
40 + // this just print the full object out as a string that's been nicely indented
41 + // with each level of nesting
42 +}

Built with git-ssb-web