You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+131-14
Original file line number
Diff line number
Diff line change
@@ -10,41 +10,158 @@ The XMTP SDK bundles the core code libraries, components, tools, documentation,
10
10
11
11
The API revolves around a network Client that allows retrieving and sending messages to other network participants. A Client must be connected to a wallet on startup. If this is the very first time the Client is created, the client will generate a key bundle that is used to encrypt and authenticate messages. The key bundle persists encrypted in local storage using a wallet signature. The public side of the key bundle is also regularly advertised on the network to allow parties to establish shared encryption keys. All this happens transparently, without requiring any additional code.
12
12
13
+
```ts
14
+
import { Client } from'xmtp-js'
15
+
import { Wallet } from'ethers'
16
+
17
+
// You'll want to replace this with a wallet from your application
18
+
const wallet =Wallet.createRandom()
19
+
// Create the client with your wallet. This will connect to the XMTP testnet by default
A Client is created with `Client.create(wallet: ethers.Signer): Client` that requires passing in a connected Wallet. The Client will request a wallet signature in 2 cases:
37
+
A Client is created with `Client.create(wallet: ethers.Signer): Promise<Client>` that requires passing in a connected Wallet. The Client will request a wallet signature in 2 cases:
16
38
17
-
1.to sign the newly generated key bundle, this happens only the very first time when key bundle is not found in storage
18
-
2.to sign a random salt used to encrypt the key bundle in storage, this happens every time the Client is started (including the very first time)
39
+
1.To sign the newly generated key bundle, this happens only the very first time when key bundle is not found in storage
40
+
2.To sign a random salt used to encrypt the key bundle in storage, this happens every time the Client is started (including the very first time)
19
41
20
42
The Client will connect to XMTP testnet by default. CreateOptions can be used to override this and other parameters of the network connection.
21
43
22
44
Note that currently the Client uses browser's local storage, so starting it on a different device or browser and connecting to the same wallet will create a "split identity" situation where only one of the clients will be able to decrypt given incoming message depending on which of the advertised key bundles the sender chose to use. Similarly if local storage is cleared for whatever reason and a new key bundle is created, older messages encrypted with older bundles cannot be decrypted anymore and will cause the client to throw.
23
45
24
-
### Sending messages
46
+
```ts
47
+
import { Client } from'xmtp-js'
48
+
// Create the client with an `ethers.Signer` from your application
49
+
const xmtp =awaitClient.create(wallet)
50
+
```
25
51
26
-
To be able to send a message, the recipient must have already started their Client at least once and consequently advertised their key bundle on the network. Messages are addressed using wallet addresses. Message payload is a string but neither the SDK nor the network put any constraints on its contents or interpretation.
52
+
### Conversations
27
53
28
-
First message and first response between two parties is sent to three separate topics:
54
+
Most of the time, when interacting with the network, you'll want to do it through `conversations`.
29
55
30
-
1. sender's introduction topic
31
-
2. recipient's introduction topic
32
-
3. conversation topic shared by the sender and the recipient
56
+
```ts
57
+
import { Client } from'xmtp-js'
58
+
// Create the client with an `ethers.Signer` from your application
59
+
const xmtp =awaitClient.create(wallet)
60
+
const conversations =xmtp.conversations
61
+
```
33
62
34
-
Any following messages are sent to the conversation topic only.
63
+
#### List existing conversations
35
64
36
-
The introduction topics allow the participants to reconstruct the list of conversations that they participate(d) in.
65
+
You can get a list of all conversations that have had 1 or more messages exchanged in the last 30 days.
37
66
38
-
The conversation topics carry the contents of the conversations.
To be able to send a message, the recipient must have already started their Client at least once and consequently advertised their key bundle on the network. Messages are addressed using wallet addresses. Message payload is a string but neither the SDK nor the network put any constraints on its contents or interpretation.
There are two types of primitives for retrieving messages from the network. Use the `list` methods to retrieve past messages that were stored by the network. Use the `stream` methods to listen to new messages using asynchronous iteration. Both primitive types have variants for listing or streaming the introduction topics (to manage the list of conversations) or the conversation topics (to manage the contents of the conversations).
114
+
#### List messages in a conversation
115
+
116
+
You can receive the complete message history by calling `conversation.messages()`
117
+
118
+
```ts
119
+
for (const conversation ofawaitxmtp.conversations.list()) {
You can listen for any new messages (incoming or outgoing) in a conversation by calling `conversation.streamMessages()`.
43
133
44
134
A successfully received message (that makes it through the decoding and decryption without throwing) can be trusted to be authentic, i.e. that it was sent by the owner of the `message.senderAddress` wallet and that it wasn't modified in transit. The `message.sent` timestamp can be trusted to have been set by the sender.
45
135
46
136
The Stream returned by the `stream` methods is an asynchronous iterator and as such usable by a for-await-of loop. Note however that it is by its nature infinite, so any looping construct used with it will not terminate, unless the termination is explicitly initiated (by breaking the loop or by an external call to `Stream.return()`)
console.log(`New message from ${message.senderAddress}: ${message.text}`)
148
+
}
149
+
```
150
+
151
+
#### Under the hood
152
+
153
+
Using `xmtp.conversations` hides the details of this, but for the curious this is how sending a message on XMTP works. The first message and first response between two parties is sent to three separate topics:
154
+
155
+
1. Sender's introduction topic
156
+
2. Recipient's introduction topic
157
+
3. Conversation topic shared by the sender and the recipient
158
+
159
+
This is used to establish a shared secret and negotiate a topic to communicate on. Any following messages are sent to the conversation topic only.
160
+
161
+
The introduction topics allow the participants to reconstruct the list of conversations that they participate(d) in.
162
+
163
+
The conversation topics carry the contents of the conversations.
0 commit comments