Building a Decentralized Chat App using Calimero and BOS: Step-by-Step Guide
Favour Kelvin
October 11, 2023
•
5 minutes
In this guide, we will walk you through the step-by-step process of creating a decentralized chat application using Calimero and BOS, the Blockchain Operating System. Before delving into the technical details, let's understand what BOS is all about.
What is BOS (Blockchain Operating System)?
BOS, short for Blockchain Operating System, simplifies the development, deployment, and access of decentralized frontends. It serves as a gateway to the world of Web 3.0, with a strong emphasis on community-driven development.
Why Use BOS?
BOS serves as both a development environment and a social network, where users can deploy and explore new applications. Here's why BOS stands out:
1. Accessibility: BOS empowers users to locally run blockchain applications, ensuring robustness and censorship resistance while keeping the user experience smooth.
2. Security: All components' code resides on the blockchain, making it auditable and transparent. Users can confidently run the full stack locally.
3. Composability: BOS encourages developers to reuse and remix components, simplifying development. You can deploy new front-ends for smart contracts on the mainnet in under 10 minutes.
4. Chain Agnosticism: BOS is flexible and compatible with various blockchains, making it an ideal choice for developers working in multi-chain environments.
The Three Pillars of BOS
Now that we've scratched the surface of what BOS is, let's dig deeper into its workings, which are built on three fundamental pillars:
1. Components: These are composable frontends that address specific problems. Components are small web 3 applications stored entirely on-chain. Developers can fork and combine them to create full-fledged web applications.
2. Blockchains: Components can interact with any blockchain, with current support for all EVM chains (e.g., Polygon, zkSync) and NEAR. NEAR is chosen for cost-effective storage of HTML/CSS/JS.
3. Gateways: These gateways enable decentralized front-ends to be accessed by a broader audience. They consist of specialized virtual machines that load and execute frontends for various protocols, including Ethereum, Layer 2 solutions, and NEAR. The code for these frontends is stored on the NEAR blockchain.
Calimero X BOS Integration
The integration of BOS and Calimero accelerates front-end development for both projects. Calimero's private blockchain characteristics ensure data security and ownership. Users can enjoy advanced functionalities, leading to an enhanced Web3 experience.
Development Prerequisites
Before we roll up our sleeves and start building our chat application, ensure you've completed the following prerequisites:
Follow the instructions here to configure your NEAR BOS development environment. This will get your localhost:3000 up and running.
Head over to testnet.mynearwallet.com to create an account since we're working in a testing environment.
5. Open http://localhost:3000/<youraccount.near>/widget/<componentname> (case-sensitive) to access your component.
For example:
http://localhost:3000/fakela.testnet/widget/Main
Alternatively, you can set the loader URL in https://test.near.org/flags and open https://test.near.org/<youraccount.testnet>/widget/<component name> to view your component locally.
Create the Chat Application
Now that your component is rendering correctly, let's begin building the chat application.
In your code editor, let's start by defining the contract ID:
Note: The contractId is the account where the chat smart contract is deployed and that this contract can be found in the Calimero GitHub public repository.
const contract = "chat-simple.ws-protocol-63";
Add the following CSS styling for the Chat interface. These styled components define the visual appearance of the chat application, such as message boxes, buttons, text, etc.
Now, let's define state variables for your chat application. These variables set default values for various properties that the application will use to manage its data and behaviour.
Next, create functions to fetch data from the contract. These functions will retrieve information such as member lists, chat messages, and channel lists. In the code below
updateMemberList fetches a list of members, and
updateChannelList fetches a list of chat channels/groups.
They both use asynchronous calls to update the application's state with the fetched data.
Next, we will create helper functions that will handle actions such as sending messages, joining the chat, and verifying function access keys. In the code below
onChangeMessage update the application's state with the content of a user's message as they type it and
We will also define access key functions to join the chat, check membership status, and verify function access keys. In the code below the
joinCurb is called when a user wants to join a chat group.
isMember is used to check if a user is a member of a chat group, based on their account ID and the list of members. The code checks if the application is in a "bootstrapping" state. If so, it calls the verifyKey function.
verifyKey verifies the user's authentication key.
After verification, the updateMemberList and updateChannelList functions are called to fetch member and channel data.
Now, let's build the user interface for the chat application. This code below will render the chat interface, messages, input field, membership status, etc
Now that you've built the chat application using BOS and Calimero, go back to http://localhost:3000/<youraccount.near>/widget/<componentname> to load and test your application.
For the full source code, check out the Calimero GitHub repository.
We hope you find this guide helpful in building a decentralized chat application using Calimero and BOS. If you have any questions or need further assistance, please don't hesitate to contact our support team. Happy coding!