Documentation
Search
K
Comment on page

Getting, creating & deleting threads

Threads are groups of messages between two or more individuals. In a chat product, this is the familiar one-on-one DM or group chat thread. A notifications thread can be thought of as a one-way thread, where only one sender has permissions to send messages, and the other "recipient" simply reads messages.

Get all threads for a wallet

import type {
// ... previous imports
Thread,
ThreadId,
} from '@dialectlabs/sdk';
// ... code from previous examples
// Fetch all threads the wallet is a part of, across all provided backends
const threads: Thread[] = await sdk.threads.findAll();
Note that the threads returned here are fetched from both on- and off-chain backends: Backends.Solana & Backends.DialectCloud.

Get a thread by its id & read its messages

import type {
// ... previous imports
ThreadMessage,
} from '@dialectlabs/sdk';
// ... code from previous examples
// Choose a given thread
const thread = threads[0];
// Fetch for a single thread by its id. N.b. the ThreadId type specifies both the address of the thread *as well as* the specified backend; threads of a given id may exist in any kind of backend. See the ThreadId type.
const query: FindThreadByIdQuery = {
id: thread.id,
}
const thread = await sdk.threads.find(query);
// Call the messages() method to read messages
const messages = await thread.messages();

Create a new thread

import type {
// ... previous imports
CreateThreadCommand,
ThreadMemberScope,
} from '@dialectlabs/sdk';
const recipient = '3vuCFLbVWsNeWgyxkb2xiLQuxKDW83HWiTMmodT8gmtk';
const command: CreateThreadCommand = {
encrypted: false,
me: {
scopes: [ThreadMemberScope.ADMIN, ThreadMemberScope.WRITE],
},
otherMembers: [
{
address: recipient,
scopes: [ThreadMemberScope.ADMIN, ThreadMemberScope.WRITE],
},
],
};
const thread = await sdk.threads.create(command);

Delete a thread

Check the delete-solana-thread example script.
const threadId = thread.id; // Keep for re-querying to confirm deletion
await thread.delete();
const query: FindThreadByIdQuery = {
id: threadId,
};
const refetchedThread = await sdk.threads.find(query); // Will be null