Option 2: Decentralized communication platform

The Orbis SDK allows you to create decentralized messaging system. All messages are decentralized and stored on Ceramic but encrypted using Lit Protocol.

Because all of the messages sent using the Orbis protocol are using the same schemas, messages sent using your application can be accessed from many different apps and vice-versa.

Step 1: Creating a conversation

A message needs to be part of a conversation so we need to create a conversation object before being able to send messages. Let’s see how it works:

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

On Orbis, each message is encrypted using LitProtocol, so the DMs sent using Orbis are still stored on Ceramic but in an encrypted way, here is an example:

Now that we created a conversation, we can use the conversation_id created in the previous example to send messages to this conversation.

Step 3: Retrieving messages from a conversation

We will keep on using the conversation_id created in the first step in the example. We are now going to load the previous message sent in this conversation and decrypt them using the Orbis SDK.

Last updated