Messaging
Every application has a built-in inbox for receiving messages and outbox for sending messages. This allows users and applications to send data between each other knowing nothing than the other user's DID and application name.
This example sends a contact record to a user's Verida Wallet mobile application.
Live Editor
VeridaMessagingExample = () => {const [status, setStatus] = useState(null)const [veridaContext, setVeridaContext] = useState(undefined)this.CONTEXT_NAME = "Verida Tutorial"React.useEffect(async () => {// See the login example for an explaination of thisif (!veridaContext) {if (hasSession(this.CONTEXT_NAME)) {// we know we have a session already// globalLoginFunction is a globally defined version of the login function// See https://developers.verida.io/docs/tutorial/SSOlet ctx = await globalLoginFunction(this.CONTEXT_NAME);setVeridaContext(ctx);}}}, []);sendTextMessage = async function () {setStatus("Initializing messaging... please wait.")const messaging = await veridaContext.getMessaging()setStatus("Sending message...")// The data message we are sendingconst data = {data: [{subject:"A new message from the tutorial!",message:"this is a plain text message from the tutorial. You can edit this message in the browser."}]}// set the subjectconst subject = "A new message from the tutorial!"// we need to set it to the correct message typeconst messageType = "inbox/type/message"const config = {recipientContextName: "Verida: Vault"}// Now send the message// DID is set to whatever DID you logged in withlet did = veridaContext.account.accountDidawait messaging.send(did, messageType, data, subject, config)setStatus("Text message sent. Please check the inbox in the Verida Wallet")}sendDataMessage = async function () {setStatus("Initalizing messaging... please wait.")const messaging = await veridaContext.getMessaging()setStatus("Sending message...")// The data message we are sendingconst data = {data: [{name: "Verida Example", // try editing these values and resend the messagefirstName: "Verida",lastName: "Example",email: "verida.example@example.com",schema:"https://common.schemas.verida.io/social/contact/v0.1.0/schema.json",},],}const subject = "New contact details for " + data.data[0].name// we need to set it to the correct message typeconst messageType = "inbox/type/dataSend"const config = {recipientContextName: "Verida: Vault",}// Now send the message// DID is set to whatever DID you logged in withlet did = veridaContext.account.accountDidawait messaging.send(did, messageType, data, subject, config)setStatus("Data message sent. Please check the inbox in the Verida Wallet")}let statusMessage = <div></div>if (status !== null) {statusMessage = (<div className="tutorial-status admonition admonition-info alert alert--info">{status}</div>)}// if we have a veridaContext we are logged inif (veridaContext) {return (<div><div><button onClick={this.sendTextMessage}>Send a Plain Text Message</button></div><div><button onClick={this.sendDataMessage}>Send Data Message</button></div><div>{statusMessage}</div></div>)} else {return (<div className="admonition admonition-danger alert alert--danger"><h2>Please login <a href="/docs/tutorial/SSO#runcode">here</a></h2></div>)}}
Result
Loading...
Messaging documentation
See the Verida Messaging documentation for complete documentation.