Web5-Messaging is a demo app showing how to use React.js and the Web5 library to generate a DID (Decentralized Digital Identifier) and interact with a Decentralized Web Network (DWN) to create, read, update, and delete (CRUD operations) messages stored in a decentralized manner.
- Node.js
- npm
-
Clone the repository:
git clone https://github.com/Developerayo/web5-messaging.git -
Navigate to the project directory:
cd web5-messaging -
Install dependencies:
npm install -
Start the development server:
npm start
src/: Contains the source code of the application.index.js: Entry point of the application.App.js: Main component of the application.components/: Contains two reusable componentsForm.js&ListMessages.js.helpers/: Contains custom hooks and utility functionsuseWeb5.jsuseMessages.js.
The App component renders the application layout and includes other components, designed using Chakra UI for styling and layout.
This component renders a form that allows users to create messages.
This component renders a list of messages. Enabling you to update or delete messages.
This custom hook manages the web5 connection as well as the user's DID (Decentralised Identifier). It returns an object that contains the web5 instance as well as the user's DID.
This is how it works:
-
Importing the required modules from the "react" library,
useStateanduseEffect.Web5from@tbd54566975/web5
-
Two state variables are initialized by the
useWeb5function:web5: To save the Web5 instance. -myDid: To save the user'sDID(Decentralized Identifier).
-
In the
useEffecthook, we created an asynchronous method namedinitializethat callsWeb5.connect()in an effort to connect to Web5. It is expected that this method will return an object containing the Web5 instance and the user's DID. -
Next, the state's values for
web5andmyDidare set. -
The hook returns an object that includes
myDidand theweb5instance.
This custom hook is used for message management. It returns an object that contains the messages as well as functions for creating, updating, and deleting messages.
This is how it works:
-
Import the necessary modules:
useStateanduseEffectfrom the 'react' library.
-
The
useMessagesfunction initialises the state variablemessagesto keep the list of messages and accepts'web5as an argument. -
The
useEffecthook creates the asynchronous functionfetchMessages, which retrieves messages from the Web5 network using theweb5.dwn.records.querymethod. -
To generate a new message, we are utilizing the
createMessagemethod.firstName,lastName,messageText, andimageFileare the four arguments it requires. It uses theweb5.dwn.records.createfunction to send a request to generate a new record after converting the image file to base64. -
In the
updateMessagefunction.messageIdandupdatedMessageDataare the two parameters it requires and it uses theweb5.dwn.records.readmethod to read the record from the Web5 network and therecord.updatemethod to update the state. -
A message can be deleted by its ID using the
deleteMessagemethod. Theweb5.dwn.records.deletemethod makes a request to delete the record. -
The hook gives back an object with the
messagesarray as well as thecreateMessage,updateMessage, anddeleteMessageoperations.
-
web5.dwn.records.query: Used to search through Web5 network records (messages). -
web5.dwn.records.create: Used to add a new record (message) to the Web5 network. -
web5.dwn.records.read: This function is used to read a record (message) from the Web5 network based on its ID.
4.record.update: This Web5 network function is used to modify an existing record (message).
5.web5.dwn.records.delete: Used to remove a record (message) from the Web5 network based on its ID.
