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.js
useMessages.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,
useState
anduseEffect.
Web5
from@tbd54566975/web5
-
Two state variables are initialized by the
useWeb5
function:web5
: To save the Web5 instance. -myDid
: To save the user'sDID
(Decentralized Identifier).
-
In the
useEffect
hook, we created an asynchronous method namedinitialize
that 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
web5
andmyDid
are set. -
The hook returns an object that includes
myDid
and theweb5
instance.
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:
useState
anduseEffect
from the 'react' library.
-
The
useMessages
function initialises the state variablemessages
to keep the list of messages and accepts'web5
as an argument. -
The
useEffect
hook creates the asynchronous functionfetchMessages
, which retrieves messages from the Web5 network using theweb5.dwn.records.query
method. -
To generate a new message, we are utilizing the
createMessage
method.firstName
,lastName
,messageText
, andimageFile
are the four arguments it requires. It uses theweb5.dwn.records.create
function to send a request to generate a new record after converting the image file to base64. -
In the
updateMessage
function.messageId
andupdatedMessageData
are the two parameters it requires and it uses theweb5.dwn.records.read
method to read the record from the Web5 network and therecord.update
method to update the state. -
A message can be deleted by its ID using the
deleteMessage
method. Theweb5.dwn.records.delete
method makes a request to delete the record. -
The hook gives back an object with the
messages
array as well as thecreateMessage
,updateMessage
, anddeleteMessage
operations.
-
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.