README.mdView |
---|
16 | 16 … | `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. |
17 | 17 … | |
18 | 18 … | We close the connection to the server using `server.close()` otherwise the connection stays open forever! |
19 | 19 … | |
| 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!) |