Option 2: Decentralized communication platform
Step 1: Creating a conversation
import React, { useState, useEffect } from 'react';
/** Import Orbis SDK */
import { Orbis } from "@orbisclub/orbis-sdk";
/**
* Initialize the Orbis class object:
* You can make this object available on other components by passing it as a prop or by using a context.
*/
let orbis = new Orbis();
export default function App() {
/** The user object returned by the connect function can be stored in state */
const [user, setUser] = useState();
/** Calls the Orbis SDK and handle the results */
async function connect() {
let res = await orbis.connect();
/** Check if connection is successful or not */
if(res.status == 200) {
console.log("Connected to Ceramic with: ", res.did);
setUser(res.did);
} else {
console.log("Error connecting to Ceramic: ", res);
alert("Error connecting to Ceramic.");
}
}
return(
<div>
{user ?
<>
<p>Connected with: {user}</p>
<CreateConversation orbis={orbis} />
</>
:
<button onClick={() => connect()}>Connect</button>
}
</div>
);
}
function CreateConversation({orbis}) {
const [loading, setLoading] = useState(false);
const [text, setText] = useState("");
/** We are calling the Orbis SDK to share a new post from this user */
async function createConversation() {
setLoading(true);
/**
* The createConversation() function accept a JSON object that must contain a 'recipients' object
* which is an array containing all of the 'dids' that will be part of the conversation. The sender's
* 'did' will be added automatically.
*/
let res = await orbis.createConversation({recipients: [text]});
/** Check if conversation was created with success or not */
if(res.status == 200) {
console.log("Save this conversation_id to use in the following examples: ", res.doc);
alert("Save this conversation_id to use in the following examples: " + res.doc);
} else {
console.log("Error creating conversation: ", res);
alert("Error creating conversation.");
}
setLoading(false);
}
return(
<div>
{/** You can use this did to test: did:pkh:eip155:1:0x222bc13f54b2f14e41945c9f2f3b9f00b4ec9b40 */}
<input placeholder="Recipient's DiD" type="text" value={text} onChange={(e) => setText(e.target.value)} />
{loading ?
<button>Loading...</button>
:
<button onClick={() => createConversation()}>Create conversation</button>
}
</div>
);
}Step 2: Sending a message to a conversation
Step 3: Retrieving messages from a conversation
Last updated