Skip to content

Commit ca6d413

Browse files
authored
Merge pull request #110 from xmtp/use-waku-filter
Use waku filter
2 parents 0ec6b34 + a819cee commit ca6d413

12 files changed

+110
-141
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const messages = await conversation.messages()
7171
// Send a message
7272
await conversation.send('gm')
7373
// Listen for new messages in the conversation
74-
for await (const message of conversation.streamMessages()) {
74+
for await (const message of await conversation.streamMessages()) {
7575
console.log(`[${message.senderAddress}]: ${message.text}`)
7676
}
7777
```
@@ -134,7 +134,7 @@ You can also listen for new conversations being started in real-time. This will
134134
_Warning: this stream will continue infinitely. To end the stream you can either break from the loop, or call `await stream.return()`_
135135

136136
```ts
137-
const stream = xmtp.conversations.stream()
137+
const stream = await xmtp.conversations.stream()
138138
for await (const conversation of stream) {
139139
console.log(`New conversation started with ${conversation.peerAddress}`)
140140
// Say hello to your new friend
@@ -193,7 +193,7 @@ The Stream returned by the `stream` methods is an asynchronous iterator and as s
193193
const conversation = await xmtp.conversations.newConversation(
194194
'0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'
195195
)
196-
for await (const message of conversation.streamMessages()) {
196+
for await (const message of await conversation.streamMessages()) {
197197
if (message.senderAddress === xmtp.address) {
198198
// This message was sent from me
199199
continue

dev/docker-compose.yml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ services:
88
- --store
99
- --message-db-connection-string=postgres://postgres:xmtp@db:5432/postgres?sslmode=disable
1010
- --lightpush
11+
- --filter
1112
- --ws-port=9001
1213
- --wait-for-db=30s
1314
ports:

package-lock.json

+31-84
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"@stardazed/streams-polyfill": "^2.4.0",
6565
"cross-fetch": "^3.1.5",
6666
"ethers": "^5.5.3",
67-
"js-waku": "^0.22.0",
67+
"js-waku": "^0.24.0",
6868
"protobufjs": "^6.11.2"
6969
},
7070
"devDependencies": {

src/Client.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Nodes = { [k: string]: string }
3535

3636
type NodesList = {
3737
testnet: Nodes
38+
dev: Nodes
3839
}
3940

4041
// Default maximum allowed content size
@@ -104,7 +105,7 @@ export type ClientOptions = NetworkOptions & KeyStoreOptions & ContentOptions
104105
export function defaultOptions(opts?: Partial<ClientOptions>): ClientOptions {
105106
const _defaultOptions: ClientOptions = {
106107
keyStoreType: KeyStoreType.networkTopicStoreV1,
107-
env: 'testnet',
108+
env: 'dev',
108109
waitForPeersTimeoutMs: 10000,
109110
codecs: [new TextCodec()],
110111
maxContentSize: MaxContentSize,
@@ -353,17 +354,17 @@ export default class Client {
353354
return message
354355
}
355356

356-
streamIntroductionMessages(): Stream<Message> {
357-
return new Stream<Message>(
357+
streamIntroductionMessages(): Promise<Stream<Message>> {
358+
return Stream.create<Message>(
358359
this,
359360
buildUserIntroTopic(this.address),
360361
noTransformation
361362
)
362363
}
363364

364-
streamConversationMessages(peerAddress: string): Stream<Message> {
365+
streamConversationMessages(peerAddress: string): Promise<Stream<Message>> {
365366
const topic = buildDirectMessageTopic(peerAddress, this.address)
366-
return new Stream<Message>(
367+
return Stream.create<Message>(
367368
this,
368369
topic,
369370
noTransformation,

src/Stream.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export default class Stream<T> {
2121
// if callback is undefined the stream is closed
2222
callback: ((wakuMsg: WakuMessage) => Promise<void>) | undefined
2323

24+
unsubscribeFn?: () => Promise<void>
25+
2426
constructor(
2527
client: Client,
2628
topic: string,
@@ -32,7 +34,6 @@ export default class Stream<T> {
3234
this.topic = topic
3335
this.client = client
3436
this.callback = this.newMessageCallback(messageTransformer, messageFilter)
35-
client.waku.relay.addObserver(this.callback, [topic])
3637
}
3738

3839
// returns new closure to handle incoming Waku messages
@@ -61,6 +62,27 @@ export default class Stream<T> {
6162
}
6263
}
6364

65+
private async start(): Promise<void> {
66+
if (!this.callback) {
67+
throw new Error('Missing callback for stream')
68+
}
69+
this.unsubscribeFn = await this.client.waku.filter.subscribe(
70+
this.callback,
71+
[this.topic]
72+
)
73+
}
74+
75+
static async create<T>(
76+
client: Client,
77+
topic: string,
78+
messageTransformer: MessageTransformer<T>,
79+
messageFilter?: MessageFilter
80+
): Promise<Stream<T>> {
81+
const stream = new Stream(client, topic, messageTransformer, messageFilter)
82+
await stream.start()
83+
return stream
84+
}
85+
6486
// To make Stream proper Async Iterable
6587
[Symbol.asyncIterator](): AsyncIterableIterator<T> {
6688
return this
@@ -74,7 +96,9 @@ export default class Stream<T> {
7496
if (!this.callback) {
7597
return { value: undefined, done: true }
7698
}
77-
this.client.waku.relay.deleteObserver(this.callback, [this.topic])
99+
if (this.unsubscribeFn) {
100+
await this.unsubscribeFn()
101+
}
78102
this.callback = undefined
79103
this.resolvers.forEach((resolve) =>
80104
resolve({ value: undefined, done: true })

src/conversations/Conversation.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default class Conversation {
2626
/**
2727
* Returns a Stream of any new messages to/from the peerAddress
2828
*/
29-
streamMessages(): Stream<Message> {
29+
streamMessages(): Promise<Stream<Message>> {
3030
return this.client.streamConversationMessages(this.peerAddress)
3131
}
3232

src/conversations/Conversations.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default class Conversations {
4949
* Will dedupe to not return the same conversation twice in the same stream.
5050
* Does not dedupe any other previously seen conversations
5151
*/
52-
stream(): Stream<Conversation> {
52+
stream(): Promise<Stream<Conversation>> {
5353
const messageTransformer: MessageTransformer<Conversation> = (
5454
msg: Message
5555
) => {
@@ -72,7 +72,7 @@ export default class Conversations {
7272
return true
7373
}
7474

75-
return new Stream<Conversation>(
75+
return Stream.create<Conversation>(
7676
this.client,
7777
buildUserIntroTopic(this.client.address),
7878
messageTransformer,

0 commit comments

Comments
 (0)