Connect to Web5 and on a DWN in Web5

Connect to Web5 and on a DWN in Web5


The Web5 JS SDK provides an intuitive interface that allows you to swiftly develop Web5 applications. By using the Web5.connect() method to initialize the SDK, you can quickly access APIs for working with Decentralized Identifiers (DIDs) and Decentralized Web Nodes (DWNs).

In this practical guide, we will connect to the free DIF community node instance, located at https://dwn.gcda.xyz, which is hosted on Google Cloud.

Let’s get started by opening our previously created index.js file and import the Web5 class

// index.js

import { Web5 } from "@web5/api"
Enter fullscreen mode

Exit fullscreen mode



Connect to Web5

We’ll use the Web5.connect() method that initializes an instance of Web5, allowing your application to connect to an existing Decentralized Identifier (DID) or create a new one through an identity agent. This method also spins up a local Decentralized Web Node (DWN) linked to the DID.

The local DWN can synchronize with a specified remote DWN, ensuring connectivity and data consistency across devices. In the technical preview of the Web5 JS SDK, a remote DWN is automatically provisioned and synced with the local DWN every two minutes.

Example usage:

const { web5, did: userDid } = await Web5.connect();
Enter fullscreen mode

Exit fullscreen mode



Options

The connection process allows the app to either request access to a user’s local identity app (desktop or mobile) or create an in-app DID if the user lacks an identity app. The outputs from this method will be used in subsequent API calls.



Parameters

  • options: Web5ConnectOptions (optional)

    • Return Value:
    • web5: Instance of Web5 for accessing the local DWN and DID methods.
    • did: Instance of DidApi representing the connected or created DID.

Examples

Connect without parameters:

   const { web5, did: userDid } = await Web5.connect();
Enter fullscreen mode

Exit fullscreen mode

Connect to the DIF Community Node:

   const { web5, did } = await Web5.connect({
     didCreateOptions: {
       dwnEndpoints: ['https://dwn.gcda.xyz'] // Community DWN instance on Google Cloud
     },
     registration: {
       onSuccess: () => {
         console.log("Connected success fully")
       },
       onFailure: (error) => {
         console.error('Connection failed:', error);
       },
     },
   });
Enter fullscreen mode

Exit fullscreen mode

Connect with an existing DWN endpoint:

   const { web5, did } = await Web5.connect({
     techPreview: {
       dwnEndpoints: ['https://dwn.your-domain.org/'],
     },
   });
Enter fullscreen mode

Exit fullscreen mode

Connect with an existing agent and DID:

   const { web5, did } = await Web5.connect({
     agent: identityAgent,
     connectedDid: existingDid,
   });
Enter fullscreen mode

Exit fullscreen mode

Configure sync interval:

   const { web5, did } = await Web5.connect({
     sync: '5s', // Set sync interval to 5 seconds
   });
Enter fullscreen mode

Exit fullscreen mode



Implementation in our project

For our practical guide, we’ll proceed with the connection the DIF free DWN, and at this point we just have to add the following line to our index.js file

// index.js

// ....

const { web5, did } = await Web5.connect({
     didCreateOptions: {
       dwnEndpoints: ['https://dwn.gcda.xyz'] // Community DWN instance on Google Cloud
     },
     registration: {
       onSuccess: () => {
         console.log("Connected success fully")
       },
       onFailure: (error) => {
         console.error('Connection failed:', error);
       },
     },
   });

// Display the connected DID in the console
console.log("nConnected did:", did);
Enter fullscreen mode

Exit fullscreen mode

now open your console and run the project by typing

node index.js
Enter fullscreen mode

Exit fullscreen mode

the output should look a the foolwing

SECURITY WARNING: You have not set a password, which defaults to a static, guessable value. This significantly compromises the security of your data. Please configure a secure, unique password.
iterations: 2, time lapsed: 1 ms
iterations: 2, time lapsed: 0 ms

Connected successfully

Connected did: did:dht:oqrbjwa7xsghig37wgjzfh5hrrhxp5skycscfdyobm6ooskhjfxy
Enter fullscreen mode

Exit fullscreen mode

at this point your index.js file should look as the follwing

Good job so far



Next ⏩️ ⏩️ ⏩️ Create records on a DWN



Prev ⏪️ ⏪️ ⏪️ Setting Up the Workspace for Decentralized Web Nodes in JavaScript



Source link
lol

By stp2y

Leave a Reply

Your email address will not be published. Required fields are marked *

No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.