SAFE Network API - Getting started (draft)


#21

Hi @pdx, @am2on, currently the browser is set to build with network libraries only, we will be updating the documentation soon for those who want to build for the mock as there is a minor issue in the browser build for mock we first need to solve (https://maidsafe.atlassian.net/browse/MAID-2150). As you can imagine this is having a lower priority than the other functional issues we are working on, so please be a bit patient that we’ll get there soon.
There is also an ongoing effort to provide tools for development, so you can work with the mock using the browser and loading locally stored webapps, trust me, I’m also as anxious as you to have them ready as hey’ll help us all to speed up in developing safe apps.


#22

When I run example 3, it only works the first time I run it, on subsequent runs it generates the error message

Error from webapp: Error: Unexpected (probably a logic error): Could not connect to the SAFE Network(…)

If I restart the browser and then wait for a while, it will work again, but then also just one single time.


#23

Hi @intrz, this is a known issue related to the number of safe clients allowed, and it will be solved in next release of the binaries. See some details of the issue here.


#24

The issue still exists to some extent with the new binaries on test 18.

If I run one of the examples I have to wait for about 1 minute before I can click reload, if I reload before this the same error will show again.


#25

Hi @intrz, can you please publish the example that is giving you problem on the Test18, share the safe:// and the steps to reproduce it?


#26

Go to safe://nasa.gov, then open the dev console and click reload. It you reloaded without waiting at least one minute you’ll see the error

Error from webapp: Error: Unexpected (probably a logic error): Could not connect to the SAFE Network(…)


#27

Hi @intrz, thanks for that. Unfortunately I’m not able to reproduce it. So in your case does it connect the first time when you loaded the website? or it doesn’t even connect the first time?
Also, can you please run the browser from a shell console so you can then see any error thrown there when that happens?


#28

Yes, it connects the first time I load the website and it connects again if I wait more than one minute between each reload.

I 17-08-10 09:58:03.423323 Failed to Bootstrap with 138.68.147.195:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:03.423323 Failed to Bootstrap with 138.68.145.41:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:03.423824 Failed to Bootstrap with 138.68.154.44:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:03.508813 Failed to Bootstrap with 139.59.185.49:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:03.509314 Failed to Bootstrap with 138.68.155.8:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:03.513818 Failed to Bootstrap with 138.68.147.124:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:03.514818 Failed to Bootstrap with 138.68.157.171:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:03.515319 Failed to Bootstrap with 138.68.157.147:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:03.515818 Failed to Bootstrap with 138.68.157.221:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:03.519821 Bootstrapping(7f8b2b..) Connection to 72827e.. failed: PeerNotFound
I 17-08-10 09:58:03.519821 Bootstrapping(7f8b2b..) Connection failed: The chosen proxy node already has connections to the maximum number of clients allowed per proxy.
I 17-08-10 09:58:03.519821 Bootstrapping(7f8b2b..) Lost connection to proxy PublicId(name: 72827e..).
I 17-08-10 09:58:04.757239 Failed to Bootstrap with 138.68.145.41:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:04.757739 Failed to Bootstrap with 138.68.157.147:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:04.757739 Failed to Bootstrap with 178.62.61.231:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:04.757739 Failed to Bootstrap with 139.59.185.49:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:04.758239 Failed to Bootstrap with 138.68.147.183:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:04.758740 Bootstrapping(7f8b2b..) Connection to ec42d7.. failed: PeerNotFound
I 17-08-10 09:58:04.758740 Bootstrapping(7f8b2b..) Connection failed: The chosen proxy node already has connections to the maximum number of clients allowed per proxy.
I 17-08-10 09:58:04.758740 Bootstrapping(7f8b2b..) Lost connection to proxy PublicId(name: ec42d7..).
I 17-08-10 09:58:06.134827 Failed to Bootstrap with 138.68.154.44:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:06.135327 Failed to Bootstrap with 138.68.155.8:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:06.135327 Failed to Bootstrap with 138.68.145.41:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:06.135828 Failed to Bootstrap with 138.68.147.195:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:06.135828 Failed to Bootstrap with 139.59.174.145:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:06.136328 Failed to Bootstrap with 138.68.157.147:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:06.136328 Failed to Bootstrap with 138.68.157.171:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:06.136829 Failed to Bootstrap with 178.62.61.231:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:06.136829 Failed to Bootstrap with 138.68.157.71:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:06.137329 Failed to Bootstrap with 138.68.147.124:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:06.137329 Failed to Bootstrap with 139.59.185.49:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:06.137829 Failed to Bootstrap with 138.68.157.221:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:06.137829 Failed to Bootstrap with 138.68.147.183:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:06.138830 Bootstrapping(7f8b2b..) Connection to 8d8991.. failed: PeerNotFound
I 17-08-10 09:58:06.138830 Bootstrapping(7f8b2b..) Connection failed: The chosen proxy node already has connections to the maximum number of clients allowed per proxy.
I 17-08-10 09:58:06.138830 Bootstrapping(7f8b2b..) Lost connection to proxy PublicId(name: 8d8991..).
I 17-08-10 09:58:07.517986 Failed to Bootstrap with 138.68.157.147:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:07.517986 Failed to Bootstrap with 139.59.174.145:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:07.519991 Failed to Bootstrap with 139.59.185.49:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:07.520990 Failed to Bootstrap with 138.68.155.8:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:07.521489 Failed to Bootstrap with 138.68.154.44:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:07.521489 Failed to Bootstrap with 138.68.157.171:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:07.521489 Failed to Bootstrap with 138.68.157.221:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:07.521989 Failed to Bootstrap with 138.68.147.124:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:07.521989 Failed to Bootstrap with 178.62.61.231:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:07.521989 Failed to Bootstrap with 138.68.157.71:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:07.522490 Failed to Bootstrap with 138.68.145.41:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:07.522490 Failed to Bootstrap with 138.68.157.132:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:07.522490 Failed to Bootstrap with 138.68.147.195:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:07.522490 Failed to Bootstrap with 138.68.157.95:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:07.522990 Failed to Bootstrap with 139.59.190.50:5483: (ClientNotWhitelisted) Our Client is not whitelisted
I 17-08-10 09:58:07.522990 Failed to Bootstrap with 138.68.147.183:5483: (ClientNotWhitelisted) Our Client is not whitelisted
E 17-08-10 09:58:07.522990 Bootstrapper has no active children left - bootstrap has failed
I 17-08-10 09:58:07.522990 Bootstrapping(7f8b2b..) Failed to bootstrap. Terminating.

#29

Hi @intrz, I raised a ticket to track this. I will contact you to try to debug it on your PC in the next few days since I cannot reproduce it and cannot understand what could be happening there.
One more question in the meantime, do you have any other web pages connecting to the network, and/or any other desktop apps connecting to the network when you reproduce this issue?


#30

I’m only running the SAFE browser. I just tried it on another computer in a different location and I get the exact same behavior.

I open the browser, then open the dev console and go to the site, no other tabs open. It works fine, then I click reload and I get the error message in the dev console, then if I wait for a minute and click reload again it loads without any error again.


#31

Hi @intrz, we were able to reproduce the issue on Test-18 and it’s not reproducible on Test-19. Please raise a ticket on github if you happen to experience any other issue/s.


#32

It’s released now! But I’m having issues – I’ve installed

"@maidsafe/safe-node-app": "0.2.2"

and imported

import safe from '@maidsafe/safe-node-app';

and now I’m getting

TypeError: exists is not a function
at
  Function.getRoot
  node_modules/bindings/bindings.js:158
    > 158 | if (exists(join(dir, 'package.json')) || exists(join(dir, 'node_modules')))

It seems this error is caused by safe-app itself; commenting out the import statement fixes the error. I wasn’t able to find much help online. Any ideas?

N.B. I’m using React with create-react-app. Here’s a link to a working demo of the error: https://github.com/cooperka/personal-website/commit/9d4474b9cf1df9f8a690fb3320f1be0d132cb2f8

After a bit more digging, it looks like safe-app requires weak which requires bindings which creates a function called exists (source), but in my case this function is undefined because fs.accessSync, fs.existsSync, and path.existsSync are all undefined. This happens using [node v6.4 + npm v4.0] and also [node v8.4 + npm v5.4]. I’ll keep digging tomorrow.


#33

@cooperka, it looks like you’re setting up a website, and not a node/electron app.

safe-node-app is built for these kind of things. If you’re building a website, you’ll need to use the SAFE Browser APIs which are built into the browser itself, and can be accessed via the window object.

See: http://docs.maidsafe.net/beaker-plugin-safe-app/


#34

I see, thanks for the clarification. I tried that originally but window.safeApp is undefined in my SAFE Browser (built for production from latest master). My code can’t use it, and when I open the console (from any website) and begin typing window.safe I see auto-suggestions for window.safeAppGroupId but nothing else. Any idea why it wouldn’t exist for me?


#35

You’ll need to be on a safe: protocol url, the APIs are only loaded on safe: or localhost: protocol links.

So you can actually use localhost to access a site you’re serving locally (from create-react-app, for eg). Say you’re serving http://127.0.0.1:8080, you can access this in the SAFE Browser on localhost://p:8080 (localhost://p:<your port>)


#36

Ah, fantastic! Thank you again. The other site I had tried was beaker:start which also didn’t have the APIs; I should have tried on a real SAFE site. It’s all working now.

@maidsafe_team could we get this info integrated into the OP’s guide?


#37

Hi I am using localhost in peruse browser and I’ve tested to run the 3 example. The first 2 example runs perfectly but the third example coming out with this error.

image


#38

Hi @JackTiew,
There was a change in the MutableData API a few months back and the getKeys and getValues don’t return a handle anymore but just an array. I’ll try to update the OP with the updated version. Please try with this one which is the updated version of the example 3:

  function SAFE_Tutorial_Example_3() {
    console.log('SAFE Example #3: Iterating over MutableData entries');

    let appInfo = {
      name: 'Example safe-app web page #3',
      id: 'net.maidsafe.tutorial.webapp.example3',
      version: '0.1.0',
      vendor: 'MaidSafe Ltd.',
    }

    let access = {
      '_public': ['Read', 'Insert', 'Delete', 'ManagePermissions']
    };

    const typeTag = 15000;
    const MUTABLE_DATA_ENTRIES = { key1: 'value1', key2: 'value2' };

    window.safeApp.initialise(appInfo)
      .then((appToken) => window.safeApp.authorise(appToken, access)
        .then((authURI) => window.safeApp.connectAuthorised(appToken, authURI))
        .then(_ => window.safeMutableData.newRandomPublic(appToken, typeTag))
        .then((mdata) => window.safeMutableData.quickSetup(mdata, MUTABLE_DATA_ENTRIES)
          .then(_ => window.safeMutableData.getKeys(mdata))
          .then((keys) => keys.map((key) => console.log("Key:", key.toString())))
          .then(_ => window.safeMutableData.getValues(mdata))
          .then((values) => values.map((value) => console.log("Value:", value.buf.toString())))
          .then(_ => window.safeMutableData.getEntries(mdata)
            .then((entries) => window.safeMutableDataEntries.forEach(entries,
              (key, value) => console.log("Entry: (", key.toString(), ",", value.buf.toString(), ")"))
            .then(() => {
              console.log("Entries iteration finished");

              // Make sure that MutableData Entries instance is freed from memory.
              window.safeMutableDataEntries.free(entries);
              console.log("MutableData Entries instance freed.");
            }))
          )
          .then(_ => {
            // Make sure that SAFEApp instance is freed from memory.
            window.safeApp.free(appToken);
            console.log("SAFEApp instance freed.");

            // Make sure that MutableData instance is freed from memory.
            window.safeMutableData.free(mdata);
            console.log("MutableData instance freed.");
          })
        )
      )
      .catch((err) => {
         console.error("Error from webapp: ", err);
      });
  };

#39

Ok thank you it working now!


#40