Last active
April 14, 2020 17:43
-
-
Save bradoyler/4b421d7b3f5fe138eeb8db0eb36d0535 to your computer and use it in GitHub Desktop.
@azure/service-bus topic subscription test (send & receive) to confirm your subscription filters is working as expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ServiceBusClient, ReceiveMode } = require('@azure/service-bus') | |
const connectionString = process.env['servicebus-connection'] | |
async function send () { | |
const sbClient = ServiceBusClient.createFromConnectionString(connectionString) | |
const client = sbClient.createTopicClient('topic-name') | |
const sender = client.createSender() | |
// set label=TEST for use in subscription filter | |
const msg = { label: 'TEST', body: { foo: 'bar', ts: new Date() } } | |
await sender.send(msg) | |
console.log('>> sent:', msg) | |
await sender.close() | |
await sbClient.close() | |
} | |
async function listen () { | |
const sbClient = ServiceBusClient.createFromConnectionString(connectionString) | |
// NOTE: idea here is to create a test subscription that has a 'label=TEST' filter | |
const subscriptionClient = sbClient.createSubscriptionClient('topic-name', 'test-sub') | |
const receiver = subscriptionClient.createReceiver(ReceiveMode.receiveAndDelete) | |
try { | |
// this receiver will trigger after 5 messages or 5 seconds | |
const messages = await receiver.receiveMessages(5, 5) | |
console.log('>> received:', messages.map(({ body, label, messageId }) => ({ body, label, messageId }))) | |
await subscriptionClient.close() | |
} finally { | |
await sbClient.close() | |
} | |
} | |
send() | |
listen() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
console output:
...after 5 seconds (receive timeout), you should get this: