git ssb

0+

mixmix / ssb-client-basic-tutorial



Tree: 732c846359bc5138a27a082a1fa03a3d8ba0bf85

Files: 732c846359bc5138a27a082a1fa03a3d8ba0bf85 / v01.js

835 bytesRaw
1const Connection = require('ssb-client')
2const pull = require('pull-stream')
3
4console.log('Connecting')
5
6Connection((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
38function 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}
43

Built with git-ssb-web