Option 1: Getting started with a Social Feed
The Orbis SDK can allow you to create any type of social application, in this example we are going to create a contextual social feed which could be used as a decentralized commenting system for an NFT marketplace for example.
Step 1: Create a post
Once users are connected to their Ceramic did, they can start sharing content on the network. The post function from our SDK allows users to share a post from their did while following our schemas.
We are starting from our previous example to connect the user to Ceramic:
/** 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>
<Share orbis={orbis} />
</>
:
<button onClick={() => connect()}>Connect</button>
}
</div>
);
}
/**
* If the user is connected we show a textarea and submit button
* to allow users to share content on Orbis.
* The 'orbis' object must be passed as a parameter to maintain the connection.
*/
function Share({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 share() {
setLoading(true);
/**
* The createPost() function accept a JSON object that must contain a body
* which is the actual content of the post being shared.
*/
let res = await orbis.createPost({body: text});
/** Check if post was shared with success or not */
if(res.status == 200) {
console.log("Shared post with stream_id: ", res.doc);
} else {
console.log("Error sharing post: ", res);
alert("Error sharing post.");
}
setLoading(false);
}
return(
<div>
<textarea placeholder="Share your post here..." value={text} onChange={(e) => setText(e.target.value)} />
{loading ?
<button>Loading...</button>
:
<button onClick={() => share()}>Share</button>
}
</div>
);
}This post would have been shared in what we call the global feed but you could also get users to share content in a specific context by sharing this informations in the object passed to the orbis.createPost() function like this:
Step 2: Retrieving posts
Now that we started sharing posts on Orbis we need to be able to retrieve them. Posts can be queried by context by using the orbis.getPosts() function.
Again, we will be starting from the example we were working on before with users sharing posts in the context of an imaginary NFT marketplace. Here is how it works:
If instead of loading posts from a specific context (in our example it’s https://nftmarketplace/collections/azuki/5060) you want to load posts from the global feed, you can simply use this:
Last updated