Using Azure service bus cloud messaging service in nodeJS application

Posted By : Deepchand Prajapati | 27-Sep-2017

What is Azure Service Bus :

Azure service bus is used for messaging between different applications. Queues, topics, subscriptions are features of service bus that can be integrated into applications. Azure service bus topics are same as queues, messages are stored in the same manner as in queues only difference is that topic creates different subscriptions for different applications depending on filters applied. Different subscribers use different filters for receiving messages from the topic and only those messages are received by the subscriber which matches the filter conditions. Below we can see how to integrate the service bus topics into any node.js application

 

Npm module to be used :

 

Npm module 'azure' can be used for interaction of Azure service bus into an application.

 

Install module :

npm install azure
 

 

Access key :

Create a service bus in an azure account and get access key corresponding to this service bus to use this access key in application.

let the access key be : 


var access key = 'Endpoint=sb://development-env-service-bus.servicebus.windows.net/;SharedAccessKeyName=SendListen;SharedAccessKey=zrOvobohzOxtL31TE/1qisX+F6NcJNu4ROP11BT8Z9A=';
var serviceBusService = azure.createServiceBusService(accessKey);
 

 

Creating Topic in service bus :

 

Now create a topic in the service bus :

  serviceBusService.createTopicIfNotExists('TopicName', function(error) { 
if (!error) {
                Logger.info(" Topic created ");
            } else {
                Logger.info("Error in creating topic ", error);
            } 
})
 
 

Create subscription on the topic :

 serviceBusService.createSubscription('TopicName', 'SubscriptionName', function(error) {
                    if (!error) {
                        Logger.info('SubscriptionName created');
                    } else {
                        Logger.info(' error in creating subscription', error);
                    }
                });
 

 

Send the message to the topic, we call sendTopicMessage method of the azure object.
var message = 'This is messge to be sent to topic';

serviceBusService.sendTopicMessage('topicName', message, function(error) {
     if (error) {
       console.log(error);
     }
   });

 
 

Receive a message from the subscription :

serviceBusService.receiveSubscriptionMessage('TopicName', 'Subscriptions', function(error, receivedMessage){
   if(!error){
       console.log(receivedMessage);
   }
});
 
 

Deleting topic from service bus :

 serviceBusService.deleteTopic('TopicName', function (error) {
   if (error) {
       console.log(error);
   }
});
 
 
 
So this how we can use service bus, Thanks

About Author

Author Image
Deepchand Prajapati

Deepchand has done B.Tech. He has expertise in Node JS & an enthusiastic team member.

Request for Proposal

Name is required

Comment is required

Sending message..