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
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);
}
});
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);
}
});
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Deepchand Prajapati
Deepchand has done B.Tech. He has expertise in Node JS & an enthusiastic team member.