Search code examples
javascriptnode.jsazureazure-functionsazure-servicebus-topics

Using a Node js Azure function to send custom properties on a message to a service bus topic


I am using a Nodejs Azure function to send messages to a service bus topic. I want to use a custom property on that message to filter which subscriptions are allowed to process the messages. This is what my Azure function code looks like.

Bindings

{
  "bindings": [
    
    {
      "type": "serviceBus",
      "direction": "out",
      "connection": "ServiceBusConnectionString",
      "name": "event",
      "topicName": "mytopic"
    }
  ]
}

index.js

const event = {
        id: eventId,
        dataVersion: version,
        eventType: eventType,
        data: data,
        eventTime: eventTime,
        customProperties: {
            number: 5
        }
    }
    context.bindings.event = JSON.stringify(event);

The event reaches the topic but is not filtered by my subscription because number does not appear as a custom property. I verified this by creating a test filter in Azure and then looked at the message using the service bus explorer.

Can someone help me with the correct way to pass custom properties. Any references to some examples will help because the official documentation does not explain how to do this. As a reference, this page has an example of how to achieve this without using Azure functions https://github.com/Huachao/azure-content/blob/master/articles/service-bus/service-bus-nodejs-how-to-use-topics-subscriptions.md


Solution

    • Event schema: Below mentioned are the properties that are used by all event publishers:
    [ 
    { "topic": string, 
    "subject": string, 
    "id": string, 
    "eventType": string, 
    "eventTime": string,
     "data":{ 
         object-unique-to-each-publisher 
         },
    "dataVersion": string, 
    "metadataVersion": string 
    }
    ]
    
    
    • As mentioned in the MSDOC, the application specific properties which can be used for custom message metadata in Service Bus.
    applicationProperties?: [key: string]: function.
    
    

    Example:

    MessageId?: string|int|Buffer
    
    
    • I have also observed that this can be achieved by trying manually through Service Bus Explorer:
    1. Sending a message

    enter image description here

    1. Two types of properties can be configured for the messages as shown below:

    enter image description here

    1. Configured a custom property:

    enter image description here

    enter image description here