Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@azure/service-bus topic subscription test (send & receive) to confirm your subscription filters is working as expected
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()
@bradoyler
Copy link
Author

bradoyler commented Apr 14, 2020

console output:

>> sent: { label: 'TEST', body: { foo: 'bar', ts: 2020-04-14T17:34:27.677Z } }

...after 5 seconds (receive timeout), you should get this:

>> received: [ { body: { foo: 'bar', ts: '2020-04-14T17:34:27.677Z' },
    label: 'TEST',
    messageId: '8be4dc2b94a74ffe9813b2bc3d35d071' } ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment