// 
// Copyright (c) Microsoft and contributors.  All rights reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//   http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// 
// See the License for the specific language governing permissions and
// limitations under the License.
// 

var fs = require('fs');
if (!fs.existsSync) {
  fs.existsSync = require('path').existsSync;
}

var azure;
if (fs.existsSync('./../../lib/azure.js')) {
  azure = require('./../../lib/azure');
} else {
  azure = require('azure');
}

var util = require('util');

var topic = 'topic_1';
var subscription = 'subscription_1';

var serviceBusClient = azure.createServiceBusService();

var messageCount = 0;

function createSubscriptionAndSend(callback) {
  serviceBusClient.createTopic(topic, function (error) {
    if (error) {
      console.log(error);
    } else {
      console.log('Created the topic ' + topic);
      serviceBusClient.createSubscription(topic, subscription, function (error2) {
        if (error2) {
          console.log(error2);
        } else {
          console.log('Created subscription ' + subscription);

          // Send a new message every 30 seconds.
          setInterval(sendMessage, 30000);
          callback();
        }
      })
    }
  });
}

function sendMessage() {
  // Send a message to the topic
  serviceBusClient.sendTopicMessage(topic, 'message' + messageCount, function (error) {
    console.log('Message ' + messageCount + ' sent');
    messageCount++;
  });
}

function receiveMessages() {
  serviceBusClient.receiveSubscriptionMessage(topic, subscription, { timeoutIntervalInS: 55 } , function (error, message) {
    if (error) {
      console.log(error);
    } else {
      console.log('Message received. Content:');
      console.log(message);
    }

    receiveMessages();
  });
}

// Initiate the publisher part
createSubscriptionAndSend(function () {
  // Execute the receiver part
  receiveMessages();
});
@@ -1,15 +1,14 @@
 //
- 
 %0A// Copyrigh
@@ -61,25 +61,24 @@
 reserved.%0A//
- 
 %0A// Licensed
@@ -294,17 +294,16 @@
 E-2.0%0A//
- 
 %0A// Unle
@@ -513,17 +513,16 @@
 lied.%0A//
- 
 %0A// See 
@@ -621,17 +621,16 @@
 ense.%0A//
- 
 %0A%0Avar fs
@@ -651,45 +651,16 @@
 ');%0A
-if (!fs.existsSync) %7B%0A  fs.existsSync
+var path
  = r
@@ -677,23 +677,9 @@
 th')
-.existsSync;%0A%7D%0A
+;
 %0Avar
@@ -686,20 +686,24 @@
  azure;%0A
-if (
+try %7B%0A  
 fs.exist
@@ -708,16 +708,37 @@
 stsSync(
+path.join(__dirname, 
 './../..
@@ -753,18 +753,17 @@
 re.js'))
- %7B
+;
 %0A  azure
@@ -797,20 +797,29 @@
 re');%0A%7D 
-else
+catch (error)
  %7B%0A  azu
@@ -848,37 +848,8 @@
 %0A%7D%0A%0A
-var util = require('util');%0A%0A
 var 
@@ -1062,16 +1062,27 @@
 ateTopic
+IfNotExists
 (topic, 
@@ -1088,32 +1088,50 @@
  function (error
+, result, response
 ) %7B%0A    if (erro
@@ -1223,16 +1223,106 @@
 topic);%0A
+      // The MatchAll filter is the default filter that is used if no filter is specified%0A
       se
@@ -1387,25 +1387,42 @@
 ction (error
-2
+, result, response
 ) %7B%0A        
@@ -1430,17 +1430,16 @@
 f (error
-2
 ) %7B%0A    
@@ -1461,17 +1461,16 @@
 og(error
-2
 );%0A     
@@ -1544,17 +1544,16 @@
 ption);%0A
-%0A
         
@@ -1831,24 +1831,42 @@
 ction (error
+, result, response
 ) %7B%0A    cons
@@ -1882,19 +1882,25 @@
 Message 
-' +
+%25d sent',
  message
@@ -1908,18 +1908,8 @@
 ount
- + ' sent'
 );%0A 
@@ -2033,16 +2033,20 @@
 ption, %7B
+%0A   
  timeout
@@ -2060,19 +2060,20 @@
 lInS: 55
+%0A 
  %7D
- 
 , functi
@@ -2090,16 +2090,26 @@
  message
+, response
 ) %7B%0A    
@@ -2152,32 +2152,70 @@
 );%0A    %7D else %7B%0A
+      // Message received and deleted%0A
       console.lo
@@ -2247,37 +2247,28 @@
 ent:
-');%0A      console.log(
+ %5Cn%25s', 
 message
+.body
 );%0A 
@@ -2272,17 +2272,16 @@
 ;%0A    %7D%0A
-%0A
     rece
@@ -2430,8 +2430,9 @@
 s();%0A%7D);
+%0A

Update longpolling example