开发者社区> 镜像站> NPM> react-native-ssb-client
react-native-ssb-client
Secure Scuttlebutt client-side API for React Native apps
Last updated 9 months ago by staltz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install react-native-ssb-client 
SYNC missed versions from official npm registry.

SSB Client for React Native apps

Similar to ssb-client, but for React Native apps that run ssb-server using nodejs-mobile-react-native.

Install

Prerequisites:

  • React Native 0.60 or higher
    • React Native 0.59 and lower are supported only in react-native-ssb-client version 5.0.0
  • nodejs-mobile-react-native as a dependency in your React Native project
  • react-native-ssb-shims as a dependency in your React Native project
npm install --save react-native-ssb-client

Use

In your backend code (your nodejs-mobile project), make sure that you have ssb-server or secret-stack installed, and add the following configurations set up:

 const SecretStack = require('secret-stack');
 const ssbKeys = require('ssb-keys');
+const rnBridge = require('rn-bridge');
+const rnChannelPlugin = require('multiserver-rn-channel');
+const NoauthTransformPlugin = require('multiserver/plugins/noauth');

 const config = makeConfig('ssb', {
   connections: {
     incoming: {
       net: [{scope: 'private', transform: 'shs', port: 26831}],
+      channel: [{scope: 'device', transform: 'noauth'}],
     },
     outgoing: {
       net: [{transform: 'shs'}],
     },
   },
 });

+function rnChannelTransport(ssb: any) {
+  ssb.multiserver.transport({
+    name: 'channel',
+    create: () => rnChannelPlugin(rnBridge.channel),
+  });
+}

+function noAuthTransform(ssb: any, cfg: any) {
+  ssb.multiserver.transform({
+    name: 'noauth',
+    create: () =>
+      NoauthTransformPlugin({
+        keys: {publicKey: Buffer.from(cfg.keys.public, 'base64')},
+      }),
+  });
+}

 SecretStack({appKey: require('ssb-caps').shs})
   .use(require('ssb-db'))
+  .use(noAuthTransform)
+  .use(rnChannelTransport)
   .use(require('ssb-master'))
   .use(require('ssb-conn'))
   .use(require('ssb-blobs'))
   .use(require('ssb-ebt'))
   .call(null, config);

In your frontend code we assume you have access to the muxrpc manifest object. Then, in your frontend code you import this library:

import ssbClient from 'react-native-ssb-client'

// ...

ssbClient(manifest)
  .use(somePlugin) // optional
  .call(null, (err, ssb) => {
    // You can now use `ssb` with all the muxrpc APIs from the backend
  })

NOTE! This library does not start the backend, you have to do that yourself by important nodejs-mobile-react-native and calling nodejs.start() or startProject().

API

  • ssbClient(manifest): this configures your muxrpc client, where manifest is an object describing the muxrpc APIs we want
  • .use(plugin): call this to attach a client-side plugin to your final muxrpc object. Plugins are {name, init} objects, where name is a string, and init(ssb): void is a function; much like secret-stack plugins are
  • .call(null, cb): call this to start using the muxrpc, it will be provided to you in the callback cb
  • .callPromise(): as an alternative to the above, you can call this to get a Promise that resolves with the muxrpc ssb object

Plugins

When setting up the client, you can register plugins. These look and feel like ssb-server or secret-stack plugins, in fact, in many cases they are so similar that a plugin intended for ssb-server might work just fine for react-native-ssb-client!

You can use client-side plugins when you are sure you don't want to run this code in the backend. For instance, a client-side plugin is the perfect place to put a light cache, in order to avoid a request to the backend. See e.g. ssb-cached-about.

Below is a simple plugin that just publishes a greeting message in the DB:

const greeterPlugin = {
  name: 'greetings',

  init: function (ssb) {
    return {
      greet: (cb) => {
        ssb.publish({type: 'post', text: 'Hello world!'}, cb)
      },
    }
  }
}

To install it:

 ssbClient(manifest)
+  .use(greeterPlugin)
   .call(null, (err, ssb) => {

   })

To use it:

 ssbClient(manifest)
   .use(greeterPlugin)
   .call(null, (err, ssb) => {
+    // Will publish a message on our feed:
+    ssb.greetings.greet((err, val) => { /* ... */ })
   })

Note

Some dependencies, such as events and assert, are in this library just to polyfill necessary Node.js utilities that are used in muxrpc and multiserver — important dependencies of this library.

License

MIT

Current Tags

  • 6.0.0-rc0                                ...           canary (a year ago)
  • 7.1.0                                ...           latest (9 months ago)

37 Versions

  • 7.1.0                                ...           9 months ago
  • 7.0.0                                ...           a year ago
  • 6.0.4                                ...           a year ago
  • 6.0.3                                ...           a year ago
  • 6.0.2                                ...           a year ago
  • 6.0.1                                ...           a year ago
  • 6.0.0                                ...           a year ago
  • 6.0.0-rc0                                ...           a year ago
  • 5.0.0                                ...           a year ago
  • 5.0.0-rc3                                ...           a year ago
  • 5.0.0-rc2                                ...           a year ago
  • 5.0.0-rc1                                ...           a year ago
  • 5.0.0-rc0                                ...           a year ago
  • 4.0.0                                ...           3 years ago
  • 3.0.2                                ...           3 years ago
  • 3.0.1                                ...           3 years ago
  • 3.0.1-rc.1                                ...           3 years ago
  • 3.0.0                                ...           3 years ago
  • 3.0.0-rc.1                                ...           3 years ago
  • 2.0.8                                ...           3 years ago
  • 2.0.7                                ...           3 years ago
  • 2.0.5                                ...           3 years ago
  • 2.0.4                                ...           3 years ago
  • 2.0.3                                ...           3 years ago
  • 2.0.2                                ...           3 years ago
  • 2.0.1                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.1.0                                ...           3 years ago
  • 1.0.8                                ...           3 years ago
  • 1.0.7                                ...           3 years ago
  • 1.0.6                                ...           3 years ago
  • 1.0.5                                ...           3 years ago
  • 1.0.4                                ...           3 years ago
  • 1.0.3                                ...           3 years ago
  • 1.0.2                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 2
Last Month 3
Dependencies (6)
Dev Dependencies (2)
Dependents (0)
None