NodeJS and Windows Azure: Using Service Bus Queues

I have been doing a lot of work with NodeJS and Windows Azure lately. I am planning to write a series of blog post about the techniques required build NodeJS applications that leverage different Windows Azure components. I am also planning on deep diving into the different elements of the NodeJS modules to integrate with Windows Azure.

Let’s begin with a simple tutorial of how to implement NodeJS applications that leverage one of the most popular components of the Windows Azure Service Bus: Queues. When using the NodeJS module for Windows Azure, developers can perform different operations on Azure Service Bus queues. The following sections will provide an overview of some of those operations.

Getting Started

The initial step to use Azure Service Bus queues from a NodeJS application is to instantiate the ServiceBusService object as illustrated in the following code:

   1: process.env.AZURE_SERVICEBUS_NAMESPACE= "MY NAMESPACE...";
   2: process.env.AZURE_SERVICEBUS_ACCESS_KEY= "MY ACCESS KEY....;
   3: var sb= require('azure');
   4: var serviceBusService = sb.createServiceBusService();

Creating a Queue

Create a service bus queue using NodeJS is accomplished by invoking a createQueueIfNotExists operation of the ServiceBusService object. The operation can take similar several parameters to customize the settings of the queue. The following code illustrates this process.

   1: function createQueueTest(queuename)
   2: {
   3:      serviceBusService.createQueueIfNotExists(queuename,  function(error){
   4:         if(!error){
   5:           console.log('queue created...');
   6:           
   7:         }
   8:       })
   9: }

Sending a Message

Placing a message in a service bus queue from NodeJS can be accomplished using the sendQueueMessage operation of the ServiceBusService object. In addition to the message payload, we can include additional properties that describe metadata associated with the message. The following NodeJS code illustrates the process of enqueueing a message in an Azure Service Bus queue using NodeJS.

   1: function sendMessageTest(queue)
   2: {
   3:  var message = {
   4:     body: 'Test message',
   5:     customProperties: {
   6:         testproperty: 'TestValue'
   7:     }}
   8:  
   9:     serviceBusService.sendQueueMessage(queue, message, function(error){
  10:     if(!error){
  11:         console.log('Message sent....');
  12:     }})   
  13: }

Receiving a Message

Similarly, to the process of enqueuing a message, we can dequeue a message from a service bus queue by invoking the ReceiveMessage operation from the ServiceBusService object. By default, messages are deleted from the queue as they are read; however, you can read (peek) and lock the message without deleting it from the queue by setting the optional parameter isPeekLock to true. The following NodeJS code illustrates this technique.

   1: function receiveMessageTest(queue)
   2: {
   3:   serviceBusService.receiveQueueMessage(queue, function(error, receivedMessage){
   4:     if(!error){
   5:         console.log(receivedMessage);
   6:     }
   7: })
   8: }

Putting it all together

The following code offers a very simple way to test the different operations we explored in this blog post using simple URL patterns such as http://<server>/SEND, http://<server>/RECEIVE and http://<server>/CREATE. Now go have some fun testing Windows Azure Service Bus Queues from NodeJS.

   1: function azureQueueTest(request, response)
   2: {
   3:  
   4:   var test= url.parse(request.url).path;
   5:  
   6: console.log(test);
   7:  
   8:     var queuename= 'myqueue';
   9:   
  10:  
  11:   if(test='CREATE')
  12:   {
  13:     createQueueTest(queuename);
  14:     console.log("queue created...");
  15:  
  16:   }
  17:  
  18:   if(test= 'SEND') 
  19:   {
  20:  
  21:     sendMessageTest(queuename);
  22:     console.log('enqueing message...');
  23:   } 
  24:  
  25:   if(test= 'RECEIVE')
  26:   {
  27:     receiveMessageTest(queuename);
  28:   }
  29:     
  30:     response.end();
  31: }
  32:  
  33: function createQueueTest(queuename)
  34: {
  35:      serviceBusService.createQueueIfNotExists(queuename,  function(error){
  36:         if(!error){
  37:           console.log('queue created...');
  38:           
  39:         }
  40:       })
  41:  
  42: }
  43:  
  44:  
  45: function sendMessageTest(queue)
  46: {
  47:  var message = {
  48:     body: 'Test message',
  49:     customProperties: {
  50:         testproperty: 'TestValue'
  51:     }}
  52:  
  53:     serviceBusService.sendQueueMessage(queue, message, function(error){
  54:     if(!error){
  55:         console.log('Message sent....');
  56:     }})   
  57: }
  58:  
  59: function receiveMessageTest(queue)
  60: {
  61:   serviceBusService.receiveQueueMessage(queue, function(error, receivedMessage){
  62:     if(!error){
  63:         console.log(receivedMessage);
  64:     }
  65: });
  66: }
  67:  
  68: process.env.AZURE_SERVICEBUS_NAMESPACE= "MY NAMESPACE...";
  69: process.env.AZURE_SERVICEBUS_ACCESS_KEY= "MY ACCESS KEY...";
  70:  
  71: //var test= "RECEIVE";
  72: var url = require('url');
  73: var sb= require('azure');
  74: var serviceBusService = sb.createServiceBusService();
  75:  
  76: var http= require('http');
  77: var server= http.createServer(azureQueueTest);
  78: console.log('service listening at localhost:1111...');
  79: server.listen(1111);

No Comments