Skip to content

Instantly share code, notes, and snippets.

@bradoyler
Last active April 14, 2020 17:43
Show Gist options
  • Save bradoyler/4b421d7b3f5fe138eeb8db0eb36d0535 to your computer and use it in GitHub Desktop.
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
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