Skip to content

Commit 7c49830

Browse files
authored
Merge pull request #301255 from TimShererWithAquent/us439212-03
Freshness Edit: Azure Messaging Service (3 of 10)
2 parents 4024a0d + 9538fc6 commit 7c49830

File tree

1 file changed

+91
-85
lines changed

1 file changed

+91
-85
lines changed

articles/service-bus-messaging/service-bus-nodejs-how-to-use-queues.md

Lines changed: 91 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,57 @@
11
---
2-
title: Get started with Azure Service Bus queues (JavaScript)
2+
title: 'Quickstart: Get started with Azure Service Bus queues (JavaScript)'
33
description: This tutorial shows you how to send messages to and receive messages from Azure Service Bus queues using the JavaScript programming language.
44
author: spelluru
55
ms.author: spelluru
6-
ms.date: 12/08/2023
6+
ms.date: 06/13/2025
77
ms.topic: quickstart
88
ms.devlang: javascript
99
ms.custom: devx-track-js, mode-api
10+
#customer intent: As a developer, I want to learn how to send and receive messages with Azure Service Bus queues by using the Python programming language.
1011
---
1112

12-
# Send messages to and receive messages from Azure Service Bus queues (JavaScript)
13+
# Quickstart: Send messages to and receive messages from Azure Service Bus queues (JavaScript)
1314
> [!div class="op_single_selector" title1="Select the programming language:"]
14-
> * [C#](service-bus-dotnet-get-started-with-queues.md)
15-
> * [Java](service-bus-java-how-to-use-queues.md)
16-
> * [JavaScript](service-bus-nodejs-how-to-use-queues.md)
17-
> * [Python](service-bus-python-how-to-use-queues.md)
15+
> - [C#](service-bus-dotnet-get-started-with-queues.md)
16+
> - [Java](service-bus-java-how-to-use-queues.md)
17+
> - [JavaScript](service-bus-nodejs-how-to-use-queues.md)
18+
> - [Python](service-bus-python-how-to-use-queues.md)
1819
19-
In this tutorial, you complete the following steps:
20+
This quickstart provides step-by-step instructions for a simple scenario of sending messages to a Service Bus queue and receiving them. In this quickstart, you complete the following tasks:
2021

21-
1. Create a Service Bus namespace, using the Azure portal.
22-
2. Create a Service Bus queue, using the Azure portal.
23-
3. Write a JavaScript application to use the [@azure/service-bus](https://www.npmjs.com/package/@azure/service-bus) package to:
24-
1. Send a set of messages to the queue.
25-
1. Receive those messages from the queue.
22+
- Create a Service Bus namespace, using the Azure portal.
23+
- Create a Service Bus queue, using the Azure portal.
24+
- Write a JavaScript application to use the [@azure/service-bus](https://www.npmjs.com/package/@azure/service-bus) package to:
25+
26+
- Send a set of messages to the queue.
27+
- Receive those messages from the queue.
2628

27-
> [!NOTE]
28-
> This quick start provides step-by-step instructions for a simple scenario of sending messages to a Service Bus queue and receiving them. You can find pre-built JavaScript and TypeScript samples for Azure Service Bus in the [Azure SDK for JavaScript repository on GitHub](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/servicebus/service-bus/samples/v7).
29+
You can find prebuilt JavaScript and TypeScript samples for Azure Service Bus in the [Azure SDK for JavaScript repository on GitHub](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/servicebus/service-bus/samples/v7).
30+
31+
If you're new to the service, see [Service Bus overview](service-bus-messaging-overview.md) before you begin.
2932

3033
## Prerequisites
3134

32-
If you're new to the service, see [Service Bus overview](service-bus-messaging-overview.md) before you do this quickstart.
35+
- An Azure subscription. To complete this quickstart, you need an Azure account. You can activate your [Monthly Azure credits for Visual Studio subscribers](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).
3336

34-
- 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).
3537
- [Node.js LTS](https://nodejs.org/en/download/package-manager/)
3638

3739
### [Passwordless](#tab/passwordless)
3840

39-
To use this quickstart with your own Azure account, you need:
40-
* Install [Azure CLI](/cli/azure/install-azure-cli), which provides the passwordless authentication to your developer machine.
41-
* Sign in with your Azure account at the terminal or command prompt with `az login`.
42-
* Use the same account when you add the appropriate data role to your resource.
43-
* Run the code in the same terminal or command prompt.
44-
* Note down your **queue** name for your Service Bus namespace. You'll need that in the code.
41+
To use this quickstart with your own Azure account:
42+
43+
- Install [Azure CLI](/cli/azure/install-azure-cli), which provides the passwordless authentication to your developer machine.
44+
- Sign in with your Azure account at a command prompt with `az login`.
45+
- Use the same account when you add the appropriate data role to your resource.
46+
- Run the code in the same Command Prompt window.
47+
- Make a note of your **queue** name for your Service Bus namespace. You need that in the code.
4548

4649
### [Connection string](#tab/connection-string)
4750

48-
Note down the following, which you'll use in the code below:
49-
* Service Bus namespace **connection string**
50-
* Service Bus namespace **queue** you created
51+
Make a note of the following values to use in the code:
52+
53+
- Service Bus namespace **connection string**
54+
- Service Bus namespace **queue** you created
5155

5256
---
5357

@@ -65,23 +69,23 @@ Note down the following, which you'll use in the code below:
6569

6670
### [Passwordless](#tab/passwordless)
6771

68-
1. To install the required npm package(s) 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.
72+
1. To install the required npm packages for Service Bus, open a Command Prompt window that has `npm` in its path and change the directory to the folder where you want to have your samples.
6973

7074
1. Install the following packages:
7175

72-
```bash
73-
npm install @azure/service-bus @azure/identity
74-
```
76+
```bash
77+
npm install @azure/service-bus @azure/identity
78+
```
7579

7680
### [Connection string](#tab/connection-string)
7781

78-
1. To install the required npm package(s) 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.
82+
1. To install the required npm packages for Service Bus, open a Command Prompt window that has `npm` in its path and change the directory to the folder where you want to have your samples.
7983

8084
1. Install the following package:
8185

82-
```bash
83-
npm install @azure/service-bus
84-
```
86+
```bash
87+
npm install @azure/service-bus
88+
```
8589

8690
---
8791

@@ -91,13 +95,13 @@ The following sample code shows you how to send a message to a queue.
9195

9296
### [Passwordless](#tab/passwordless)
9397

94-
You must have signed in with the Azure CLI's `az login` in order for your local machine to provide the passwordless authentication required in this code.
98+
Sign in with the Azure CLI command `az login` for your local machine to provide the passwordless authentication required in this code.
9599

96-
1. Open your favorite editor, such as [Visual Studio Code](https://code.visualstudio.com/).
97-
1. Create a file called `send.js` and paste the below code into it. This code sends the names of scientists as messages to your queue.
100+
1. Open a text editor, such as [Visual Studio Code](https://code.visualstudio.com/).
101+
1. Create a file called `send.js` and paste the following code into it. This code sends the names of scientists as messages to your queue.
98102

99103
> [!IMPORTANT]
100-
> The passwordless credential is provided with the [**DefaultAzureCredential**](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential).
104+
> The passwordless credential is provided by using the [**DefaultAzureCredential**](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential).
101105
102106
```javascript
103107
const { ServiceBusClient } = require("@azure/service-bus");
@@ -178,23 +182,24 @@ You must have signed in with the Azure CLI's `az login` in order for your local
178182
});
179183
```
180184

181-
3. Replace `<SERVICE-BUS-NAMESPACE>` with your Service Bus namespace.
182-
4. Replace `<QUEUE NAME>` with the name of the queue.
183-
5. Then run the command in a command prompt to execute this file.
185+
1. Replace `<SERVICE-BUS-NAMESPACE>` with your Service Bus namespace.
186+
1. Replace `<QUEUE NAME>` with the name of the queue.
187+
1. To run the code in this file, use this command at a command prompt:
184188

185-
```console
186-
node send.js
187-
```
188-
6. You should see the following output.
189+
```console
190+
node send.js
191+
```
189192

190-
```console
191-
Sent a batch of messages to the queue: myqueue
192-
```
193+
You should see the following output.
194+
195+
```console
196+
Sent a batch of messages to the queue: myqueue
197+
```
193198

194199
### [Connection string](#tab/connection-string)
195200

196-
1. Open your favorite editor, such as [Visual Studio Code](https://code.visualstudio.com/).
197-
1. Create a file called `send.js` and paste the below code into it. This code sends the names of scientists as messages to your queue.
201+
1. Open a text editor, such as [Visual Studio Code](https://code.visualstudio.com/).
202+
1. Create a file called `send.js` and paste the following code into it. This code sends the names of scientists as messages to your queue.
198203

199204
```javascript
200205
const { ServiceBusClient } = require("@azure/service-bus");
@@ -270,29 +275,29 @@ You must have signed in with the Azure CLI's `az login` in order for your local
270275
process.exit(1);
271276
});
272277
```
273-
3. Replace `<CONNECTION STRING TO SERVICE BUS NAMESPACE>` with the connection string to your Service Bus namespace.
274-
4. Replace `<QUEUE NAME>` with the name of the queue.
275-
5. Then run the command in a command prompt to execute this file.
278+
1. Replace `<CONNECTION STRING TO SERVICE BUS NAMESPACE>` with the connection string to your Service Bus namespace.
279+
1. Replace `<QUEUE NAME>` with the name of the queue.
280+
1. To run the code in this file, use this command at a command prompt:
276281

277-
```console
278-
node send.js
279-
```
280-
6. You should see the following output.
282+
```console
283+
node send.js
284+
```
285+
You should see the following output.
281286

282-
```console
283-
Sent a batch of messages to the queue: myqueue
284-
```
287+
```console
288+
Sent a batch of messages to the queue: myqueue
289+
```
285290

286291
---
287292

288293
## Receive messages from a queue
289294

290295
### [Passwordless](#tab/passwordless)
291296

292-
You must have signed in with the Azure CLI's `az login` in order for your local machine to provide the passwordless authentication required in this code.
297+
Sign in with the Azure CLI command `az login` for your local machine to provide the passwordless authentication required in this code.
293298

294-
1. Open your favorite editor, such as [Visual Studio Code](https://code.visualstudio.com/)
295-
2. Create a file called `receive.js` and paste the following code into it.
299+
1. Open a text editor, such as [Visual Studio Code](https://code.visualstudio.com/).
300+
1. Create a file called `receive.js` and paste the following code into it.
296301

297302
```javascript
298303
const { delay, ServiceBusClient, ServiceBusMessage } = require("@azure/service-bus");
@@ -342,18 +347,19 @@ You must have signed in with the Azure CLI's `az login` in order for your local
342347
process.exit(1);
343348
});
344349
```
345-
3. Replace `<SERVICE-BUS-NAMESPACE>` with your Service Bus namespace.
346-
4. Replace `<QUEUE NAME>` with the name of the queue.
347-
5. Then run the command in a command prompt to execute this file.
348350

349-
```console
350-
node receive.js
351-
```
351+
1. Replace `<SERVICE-BUS-NAMESPACE>` with your Service Bus namespace.
352+
1. Replace `<QUEUE NAME>` with the name of the queue.
353+
1. To run the code in this file, use this command at a command prompt:
354+
355+
```console
356+
node receive.js
357+
```
352358

353359
### [Connection string](#tab/connection-string)
354360

355-
1. Open your favorite editor, such as [Visual Studio Code](https://code.visualstudio.com/)
356-
2. Create a file called `receive.js` and paste the following code into it.
361+
1. Open your favorite editor, such as [Visual Studio Code](https://code.visualstudio.com/).
362+
1. Create a file called `receive.js` and paste the following code into it.
357363

358364
```javascript
359365
const { delay, ServiceBusClient, ServiceBusMessage } = require("@azure/service-bus");
@@ -400,14 +406,13 @@ You must have signed in with the Azure CLI's `az login` in order for your local
400406
});
401407
```
402408

403-
3. Replace `<CONNECTION STRING TO SERVICE BUS NAMESPACE>` with the connection string to your Service Bus namespace.
404-
405-
4. Replace `<QUEUE NAME>` with the name of the queue.
406-
5. Then run the command in a command prompt to execute this file.
409+
1. Replace `<CONNECTION STRING TO SERVICE BUS NAMESPACE>` with the connection string to your Service Bus namespace.
410+
1. Replace `<QUEUE NAME>` with the name of the queue.
411+
1. To run the code in this file, use this command at a command prompt:
407412

408-
```console
409-
node receive.js
410-
```
413+
```console
414+
node receive.js
415+
```
411416

412417
---
413418

@@ -426,26 +431,27 @@ Received message: Johannes Kepler
426431
Received message: Nikolaus Kopernikus
427432
```
428433

429-
On the **Overview** page for the Service Bus namespace in the Azure portal, you can see **incoming** and **outgoing** message count. You may need to wait for a minute or so and then refresh the page to see the latest values.
434+
On the **Overview** page for the Service Bus namespace in the Azure portal, you can see **incoming** and **outgoing** message count. You might need to wait for a minute or so and then refresh the page to see the latest values.
430435

431-
:::image type="content" source="./media/service-bus-java-how-to-use-queues/overview-incoming-outgoing-messages.png" alt-text="Incoming and outgoing message count":::
436+
:::image type="content" source="./media/service-bus-java-how-to-use-queues/overview-incoming-outgoing-messages.png" alt-text="Screenshot shows iIncoming and outgoing message count." lightbox="./media/service-bus-java-how-to-use-queues/overview-incoming-outgoing-messages.png":::
432437

433438
Select the queue on this **Overview** page to navigate to the **Service Bus Queue** page. You see the **incoming** and **outgoing** message count on this page too. You also see other information such as the **current size** of the queue, **maximum size**, **active message count**, and so on.
434439

435-
:::image type="content" source="./media/service-bus-java-how-to-use-queues/queue-details.png" alt-text="Queue details":::
440+
:::image type="content" source="./media/service-bus-java-how-to-use-queues/queue-details.png" alt-text="Screenshot shows Queue detail information." lightbox="./media/service-bus-java-how-to-use-queues/queue-details.png":::
436441

437442
## Troubleshooting
438443

439-
If you receive one of the following errors when running the **passwordless** version of the JavaScript code, make sure you are signed in via the Azure CLI command, `az login` and the [appropriate role](#azure-built-in-roles-for-azure-service-bus) is applied to your Azure user account:
444+
If you receive one of the following errors when you run the **passwordless** version of the JavaScript code, sign in by using the Azure CLI command, `az login`. The [appropriate role](service-bus-nodejs-how-to-use-queues.md?tabs=passwordless#azure-built-in-roles-for-azure-service-bus) is applied to your Azure user account:
440445

441-
* 'Send' claim(s) are required to perform this operation
442-
* 'Receive' claim(s) are required to perform this operation
446+
- 'Send' claims are required to perform this operation
447+
- 'Receive' claims are required to perform this operation
443448

444449
## Clean up resources
445450

446451
Navigate to your Service Bus namespace in the Azure portal, and select **Delete** on the Azure portal to delete the namespace and the queue in it.
447452

448-
## Next steps
453+
## Related content
454+
449455
See the following documentation and samples:
450456

451457
- [Azure Service Bus client library for JavaScript](https://www.npmjs.com/package/@azure/service-bus)

0 commit comments

Comments
 (0)