You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -3,7 +3,7 @@ title: Get started with Azure Service Bus queues (JavaScript)
3
3
description: This tutorial shows you how to send messages to and receive messages from Azure Service Bus queues using the JavaScript programming language.
In this tutorial, you learn how to use the [@azure/service-bus](https://www.npmjs.com/package/@azure/service-bus) package in a JavaScript program to send messages to and receive messages from a Service Bus queue.
19
+
In this tutorial, you complete the following steps:
20
+
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. Write a .NET console application to receive those messages from the queue.
20
26
21
27
> [!NOTE]
22
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).
23
29
24
30
## Prerequisites
31
+
32
+
If you're new to the service, see [Service Bus overview](service-bus-messaging-overview.md) before you do this quickstart.
33
+
25
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).
26
-
- 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 namespace and the name of the **queue** you created.
35
+
-[Node.js LTS](https://nodejs.org/en/download/)
36
+
- 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.
37
+
38
+
### [Passwordless](#tab/passwordless)
39
+
40
+
To use this quickstart with your own Azure account, you need:
41
+
* Install [Azure CLI](/cli/azure/install-azure-cli), which provides the passwordless authentication to your developer machine.
42
+
* Sign in with your Azure account at the terminal or command prompt with `az login`.
43
+
* Use the same account when you add the appropriate data role to your resource.
44
+
* Run the code in the same terminal or command prompt.
45
+
* Note down your **queue** name for your Service Bus namespace. You'll need that in the code.
46
+
47
+
### [Connection string](#tab/connection-string)
48
+
49
+
Note down the following, which you'll use in the code below:
50
+
* Service Bus namespace **connection string**
51
+
* Service Bus namespace **queue** you created
52
+
53
+
---
27
54
28
55
> [!NOTE]
29
56
> - 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/quickstart-nodejs.md), or [Node.js cloud service using Windows PowerShell](../cloud-services/cloud-services-nodejs-develop-deploy-app.md).
30
57
31
-
### Use Node Package Manager (NPM) to install the package
32
-
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.
## Use Node Package Manager (NPM) to install the package
66
+
67
+
### [Passwordless](#tab/passwordless)
68
+
69
+
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.
70
+
71
+
1. Install the following packages:
72
+
73
+
```bash
74
+
npm install @azure/service-bus @azure/identity
75
+
```
76
+
77
+
### [Connection string](#tab/connection-string)
78
+
79
+
1. To install the required npm package(s) forService 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.
80
+
81
+
1. Install the following package:
82
+
83
+
```bash
84
+
npm install @azure/service-bus
85
+
```
86
+
87
+
---
37
88
38
89
## Send messages to a queue
39
-
The following sample code shows you how to send a message to a queue.
90
+
91
+
The following sample code shows you how to send a message to a queue.
92
+
93
+
### [Passwordless](#tab/passwordless)
94
+
95
+
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.
96
+
97
+
1. Open your favorite editor, such as [Visual Studio Code](https://code.visualstudio.com/).
98
+
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.
99
+
100
+
The passwordless credential is provided with the [**DefaultAzureCredential**](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential).
// subscribe and specify the message and error handlers
328
+
receiver.subscribe({
329
+
processMessage: myMessageHandler,
330
+
processError: myErrorHandler
331
+
});
332
+
333
+
// Waiting long enough before closing the sender to send messages
334
+
await delay(20000);
335
+
336
+
await receiver.close();
337
+
await sbClient.close();
338
+
}
339
+
// call the main function
340
+
main().catch((err) => {
341
+
console.log("Error occurred: ", err);
342
+
process.exit(1);
343
+
});
344
+
```
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.
348
+
349
+
```console
350
+
node receive.js
351
+
```
352
+
353
+
### [Connection string](#tab/connection-string)
354
+
133
355
1. Open your favorite editor, such as [Visual Studio Code](https://code.visualstudio.com/)
134
356
2. Create a file called `receive.js` and paste the following code into it.
135
357
@@ -177,35 +399,52 @@ The following sample code shows you how to send a message to a queue.
177
399
process.exit(1);
178
400
});
179
401
```
402
+
180
403
3. Replace `<CONNECTION STRING TO SERVICE BUS NAMESPACE>` with the connection string to your Service Bus namespace.
181
-
1. Replace `<QUEUE NAME>`with the name of the queue.
182
-
1. Then, run the command in a command prompt to execute this file.
183
404
184
-
```console
185
-
node receive.js
186
-
```
187
-
1. You should see the following output.
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.
188
407
189
408
```console
190
-
Received message: Albert Einstein
191
-
Received message: Werner Heisenberg
192
-
Received message: Marie Curie
193
-
Received message: Steven Hawking
194
-
Received message: Isaac Newton
195
-
Received message: Niels Bohr
196
-
Received message: Michael Faraday
197
-
Received message: Galileo Galilei
198
-
Received message: Johannes Kepler
199
-
Received message: Nikolaus Kopernikus
409
+
node receive.js
200
410
```
201
411
412
+
---
413
+
414
+
You should see the following output.
415
+
416
+
```console
417
+
Received message: Albert Einstein
418
+
Received message: Werner Heisenberg
419
+
Received message: Marie Curie
420
+
Received message: Steven Hawking
421
+
Received message: Isaac Newton
422
+
Received message: Niels Bohr
423
+
Received message: Michael Faraday
424
+
Received message: Galileo Galilei
425
+
Received message: Johannes Kepler
426
+
Received message: Nikolaus Kopernikus
427
+
```
428
+
202
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.
203
430
204
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":::
205
432
206
433
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.
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:
440
+
441
+
* 'Send' claim(s) are required to perform this operation
442
+
* 'Receive' claim(s) are required to perform this operation
443
+
444
+
## Clean up resources
445
+
446
+
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.
0 commit comments