Skip to content

Commit b626506

Browse files
author
magne
committed
Add filtering example on readme
1 parent a7615a7 commit b626506

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,49 @@ await client.close()
276276

277277
### Filtering
278278

279-
Work in progress ⚠️
279+
It is possible to filter messages both on the publishing and consumer sides
280+
281+
```typescript
282+
const client = await connect({
283+
hostname: "localhost",
284+
port: 5552,
285+
username: "rabbit",
286+
password: "rabbit",
287+
vhost: "/",
288+
})
289+
290+
const publisher = await client.declarePublisher(
291+
{ stream: streamName, publisherRef: `my-publisher-${randomUUID()}` },
292+
(msg) => msg.applicationProperties!["test"].toString() // Extraction of message filtering value
293+
)
294+
const message1 = "test1"
295+
const message2 = "test2"
296+
const message3 = "test3"
297+
const applicationProperties1 = { test: "A" }
298+
const applicationProperties2 = { test: "B" }
299+
300+
await publisher.send(Buffer.from(message1), { applicationProperties: applicationProperties1 })
301+
await publisher.send(Buffer.from(message2), { applicationProperties: applicationProperties1 })
302+
await publisher.send(Buffer.from(message3), { applicationProperties: applicationProperties2 })
303+
304+
await client.declareConsumer(
305+
{
306+
stream: streamName,
307+
offset: Offset.first(),
308+
// Filter option for the consumer
309+
filter: {
310+
values: ["A", "B"],
311+
postFilterFunc: (msg) => msg.applicationProperties!["test"] === "A",
312+
matchUnfiltered: true,
313+
},
314+
},
315+
(msg) => filteredMsg.push(msg.content.toString("utf-8"))
316+
)
317+
318+
await sleep(2000)
319+
320+
await client.close()
321+
```
280322

281323
## Running Examples
282324

0 commit comments

Comments
 (0)