|
| 1 | +--- |
| 2 | +title: How to use Azure Service Bus queues in Node.js - azure/service-bus | Microsoft Docs |
| 3 | +description: Learn how to use Service Bus queues in Azure from a Node.js app. |
| 4 | +services: service-bus-messaging |
| 5 | +documentationcenter: nodejs |
| 6 | +author: axisc |
| 7 | +manager: timlt |
| 8 | +editor: spelluru |
| 9 | + |
| 10 | +ms.assetid: a87a00f9-9aba-4c49-a0df-f900a8b67b3f |
| 11 | +ms.service: service-bus-messaging |
| 12 | +ms.workload: tbd |
| 13 | +ms.tgt_pltfrm: na |
| 14 | +ms.devlang: nodejs |
| 15 | +ms.topic: article |
| 16 | +ms.date: 04/10/2019 |
| 17 | +ms.author: aschhab |
| 18 | + |
| 19 | +--- |
| 20 | +# How to use Service Bus queues with Node.js and the azure/service-bus package |
| 21 | +> [!div class="op_multi_selector" title1="Programming language" title2="Node.js pacakge"] |
| 22 | +> - [(Node.js | azure-sb)](service-bus-nodejs-how-to-use-queues.md) |
| 23 | +> - [(Node.js | @azure/service-bus)](service-bus-nodejs-how-to-use-queues-new-package.md) |
| 24 | +
|
| 25 | +In this tutorial, you learn how to create Node.js applications to send messages to and receive messages from a Service Bus queue using the new [@azure/service-bus](https://www.npmjs.com/package/@azure/service-bus) package. This package uses the faster [AMQP 1.0 protocol](service-bus-amqp-overview.md) whereas the older [azure-sb](https://www.npmjs.com/package/azure-sb) package used [Service Bus REST run-time APIs](/rest/api/servicebus/service-bus-runtime-rest). The samples are written in JavaScript. |
| 26 | + |
| 27 | +## Prerequisites |
| 28 | +- An Azure subscription. To complete this tutorial, you need an Azure account. You can activate your [MSDN subscriber benefits](https://azure.microsoft.com/pricing/member-offers/credit-for-visual-studio-subscribers/?WT.mc_id=A85619ABF) or sign up for a [free account](https://azure.microsoft.com/free/?WT.mc_id=A85619ABF). |
| 29 | +- If you don't have a queue to work with, follow steps in the [Use Azure portal to create a Service Bus queue](service-bus-quickstart-portal.md) article to create a queue. Note down the connection string for your Service Bus instance and the name of the queue you created. We'll use these values in the samples. |
| 30 | + |
| 31 | +> [!NOTE] |
| 32 | +> - This tutorial works with samples that you can copy and run using [Nodejs](https://nodejs.org/). For instructions on how to create a Node.js application, see [Create and deploy a Node.js application to an Azure Website](../app-service/app-service-web-get-started-nodejs.md), or [Node.js cloud service using Windows PowerShell](../cloud-services/cloud-services-nodejs-develop-deploy-app.md). |
| 33 | +> - The new [@azure/service-bus](https://www.npmjs.com/package/@azure/service-bus) package does not support creation of queues yet. Please use the [@azure/arm-servicebus](https://www.npmjs.com/package/@azure/arm-servicebus) package if you want to programmatically create them. |
| 34 | +
|
| 35 | +### Use Node Package Manager (NPM) to install the package |
| 36 | +To install the npm package for Service Bus, open a command prompt that has `npm` in its path, change the directory to the folder where you want to have your samples and then run this command. |
| 37 | + |
| 38 | +```bash |
| 39 | +npm install @azure/service-bus |
| 40 | +``` |
| 41 | + |
| 42 | +## Send messages to a queue |
| 43 | +Interacting with a Service Bus queue starts with instantiating the `ServiceBusClient` class and using it to instantiate the `QueueClient` class. Once you have the queue client, you can create a sender and use either `send` or `sendBatch` method on it to send messages. |
| 44 | + |
| 45 | +1. Open your favorite editor, such as Visual Studio Code |
| 46 | +2. Create a file called `send.js` and paste the below code into it |
| 47 | + |
| 48 | + ```javascript |
| 49 | + const { ServiceBusClient } = require("@azure/service-bus"); |
| 50 | + |
| 51 | + // Define connection string and related Service Bus entity names here |
| 52 | + const connectionString = ""; |
| 53 | + const queueName = ""; |
| 54 | + |
| 55 | + async function main(){ |
| 56 | + const sbClient = ServiceBusClient.createFromConnectionString(connectionString); |
| 57 | + const queueClient = sbClient.createQueueClient(queueName); |
| 58 | + const sender = queueClient.createSender(); |
| 59 | + |
| 60 | + try { |
| 61 | + for (let i = 0; i < 10; i++) { |
| 62 | + const message= { |
| 63 | + body: `Hello world! ${i}`, |
| 64 | + label: `test`, |
| 65 | + userProperties: { |
| 66 | + myCustomPropertyName: `my custom property value ${i}` |
| 67 | + } |
| 68 | + }; |
| 69 | + console.log(`Sending message: ${message.body}`); |
| 70 | + await sender.send(message); |
| 71 | + } |
| 72 | + |
| 73 | + await queueClient.close(); |
| 74 | + } finally { |
| 75 | + await sbClient.close(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + main().catch((err) => { |
| 80 | + console.log("Error occurred: ", err); |
| 81 | + }); |
| 82 | + ``` |
| 83 | +3. Enter the connection string and name of your queue in the above code. |
| 84 | +4. Then run the command `node send.js` in a command prompt to execute this file. This command will send 10 messages to your queue. |
| 85 | + |
| 86 | + The messages you send can have some standard properties like `label` and `messageId`. If you want to set any custom properties, use the `userProperties`, which is a json object that can hold key-value pairs of your custom data. |
| 87 | + |
| 88 | + Service Bus queues support a maximum message size of 256 KB in the [Standard tier](service-bus-premium-messaging.md) and 1 MB in the [Premium tier](service-bus-premium-messaging.md). There's no limit on the number of messages held in a queue but there's a cap on the total size of the messages held by a queue. This queue size is defined at creation time, with an upper limit of 5 GB. For more information about quotas, see [Service Bus quotas](service-bus-quotas.md). |
| 89 | + |
| 90 | +## Receive messages from a queue |
| 91 | +Interacting with a Service Bus queue starts with instantiating the `ServiceBusClient` class and using it to instantiate the `QueueClient` class. Once you have the queue client, you can create a receiver and use either `receiveMessages` or `registerMessageHandler` method on it to receive messages. |
| 92 | + |
| 93 | +1. Open your favorite editor, such as Visual Studio Code |
| 94 | +2. Create a file called `recieve.js` and paste the below code into it |
| 95 | + |
| 96 | + ```javascript |
| 97 | + const { ServiceBusClient, ReceiveMode } = require("@azure/service-bus"); |
| 98 | + |
| 99 | + // Define connection string and related Service Bus entity names here |
| 100 | + const connectionString = ""; |
| 101 | + const queueName = ""; |
| 102 | + |
| 103 | + async function main(){ |
| 104 | + const sbClient = ServiceBusClient.createFromConnectionString(connectionString); |
| 105 | + const queueClient = sbClient.createQueueClient(queueName); |
| 106 | + const receiver = queueClient.createReceiver(ReceiveMode.ReceiveAndDelete); |
| 107 | + try { |
| 108 | + const messages = await receiver.receiveMessages(10) |
| 109 | + console.log("Received messages:"); |
| 110 | + console.log(messages.map(message => message.body)); |
| 111 | + |
| 112 | + await queueClient.close(); |
| 113 | + } finally { |
| 114 | + await sbClient.close(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + main().catch((err) => { |
| 119 | + console.log("Error occurred: ", err); |
| 120 | + }); |
| 121 | + ``` |
| 122 | +3. Enter the connection string and name of your queue in the above code. |
| 123 | +4. Then run the command `node receiveMessages.js` in a command prompt to execute this file. This command will try to receive a maximum of 10 messages from your queue. The actual count you receive may depend on the number of messages in the queue and network latency. |
| 124 | + |
| 125 | + The `createReceiver` method takes in a `ReceiveMode` which is an enum with values [ReceiveAndDelete](message-transfers-locks-settlement.md#settling-receive-operations) and [PeekLock](message-transfers-locks-settlement.md#settling-receive-operations). Remember to [settle your messages](message-transfers-locks-settlement.md#settling-receive-operations) if you use the `PeekLock` mode by using any of `complete()`, `abandon()`, `defer()`, or `deadletter()` methods on the message. |
| 126 | + |
| 127 | +## Next steps |
| 128 | +To learn more, see the following resources. |
| 129 | +- [Queues, topics, and subscriptions](service-bus-queues-topics-subscriptions.md) |
| 130 | +- Checkout other [Nodejs samples for Service Bus on GitHub](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/javascript) |
| 131 | +- [Node.js Developer Center](https://azure.microsoft.com/develop/nodejs/) |
| 132 | + |
0 commit comments