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 topics (JavaScript)
3
3
description: This tutorial shows you how to send messages to Azure Service Bus topics and receive messages from topics' subscriptions using the JavaScript programming language.
In this tutorial, you complete the following steps:
20
21
21
-
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 a Service Bus topic and receive messages from a Service Bus subscription to that topic.
22
+
1. Create a Service Bus namespace, using the Azure portal.
23
+
2. Create a Service Bus topic, using the Azure portal.
24
+
3. Create a Service Bus subscription to that topic, using the Azure portal.
25
+
4. Write a JavaScript application to use the [@azure/service-bus](https://www.npmjs.com/package/@azure/service-bus) package to:
26
+
* Send a set of messages to the topic.
27
+
* Receive those messages from the subscription.
22
28
23
29
> [!NOTE]
24
30
> This quick start provides step-by-step instructions for a simple scenario of sending a batch of messages to a Service Bus topic and receiving those messages from a subscription of the topic. 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).
25
31
26
32
## Prerequisites
27
33
- 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).
28
-
- Follow steps in the [Quickstart: Use the Azure portal to create a Service Bus topic and subscriptions to the topic](service-bus-quickstart-topics-subscriptions-portal.md). Note down the connection string, topic name, and a subscription name. You will use only one subscription for this quickstart.
34
+
-[Node.js LTS](https://nodejs.org/en/download/)
35
+
- Follow steps in the [Quickstart: Use the Azure portal to create a Service Bus topic and subscriptions to the topic](service-bus-quickstart-topics-subscriptions-portal.md). You will use only one subscription for this quickstart.
36
+
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 role to your resource.
44
+
* Run the code in the same terminal or command prompt.
45
+
* Note down your **topic** name and **subscription** 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 **topic** name you created
52
+
* Service Bus namespace **subscription**
53
+
54
+
---
29
55
30
56
> [!NOTE]
31
57
> - 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).
32
58
33
-
### Use Node Package Manager (NPM) to install the package
34
-
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.
59
+
## Use Node Package Manager (NPM) to install the package
35
60
36
-
```bash
37
-
npm install @azure/service-bus
38
-
```
61
+
### [Passwordless](#tab/passwordless)
62
+
63
+
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.
64
+
65
+
1. Install the following packages:
66
+
67
+
```bash
68
+
npm install @azure/service-bus @azure/identity
69
+
```
70
+
71
+
### [Connection string](#tab/connection-string)
72
+
73
+
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.
74
+
75
+
1. Install the following package:
76
+
77
+
```bash
78
+
npm install @azure/service-bus
79
+
```
80
+
81
+
---
39
82
40
83
## Send messages to a topic
41
84
The following sample code shows you how to send a batch of messages to a Service Bus topic. See code comments for details.
42
85
86
+
### [Passwordless](#tab/passwordless)
87
+
88
+
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.
89
+
90
+
1. Open your favorite editor, such as [Visual Studio Code](https://code.visualstudio.com/)
91
+
2. Create a file called `sendtotopic.js` and paste the below code into it. This code will send a message to your topic.
// subscribe and specify the message and error handlers
314
+
receiver.subscribe({
315
+
processMessage: myMessageHandler,
316
+
processError: myErrorHandler
317
+
});
318
+
319
+
// Waiting long enough before closing the sender to send messages
320
+
await delay(5000);
321
+
322
+
await receiver.close();
323
+
await sbClient.close();
324
+
}
325
+
326
+
// call the main function
327
+
main().catch((err) => {
328
+
console.log("Error occurred: ", err);
329
+
process.exit(1);
330
+
});
331
+
```
332
+
3. Replace `<SERVICE BUS NAMESPACE CONNECTION STRING>` with the connection string to the namespace.
333
+
4. Replace `<TOPIC NAME>` with the name of the topic.
334
+
5. Replace `<SUBSCRIPTION NAME>` with the name of the subscription to the topic.
335
+
6. Then run the command in a command prompt to execute this file.
336
+
337
+
```console
338
+
node receivefromsubscription.js
339
+
```
340
+
341
+
### [Connection string](#tab/connection-string)
342
+
131
343
1. Open your favorite editor, such as [Visual Studio Code](https://code.visualstudio.com/)
132
344
2. Create a file called **receivefromsubscription.js** and paste the following code into it. See code comments for details.
133
345
@@ -175,27 +387,30 @@ The following sample code shows you how to send a batch of messages to a Service
175
387
});
176
388
```
177
389
3. Replace `<SERVICE BUS NAMESPACE CONNECTION STRING>` with the connection string to the namespace.
178
-
1. Replace `<TOPIC NAME>`with the name of the topic.
179
-
1. Replace `<SUBSCRIPTION NAME>`with the name of the subscription to the topic.
180
-
1. Then run the command in a command prompt to execute this file.
390
+
4. Replace `<TOPIC NAME>` with the name of the topic.
391
+
5. Replace `<SUBSCRIPTION NAME>` with the name of the subscription to the topic.
392
+
6. Then run the command in a command prompt to execute this file.
181
393
182
394
```console
183
395
node receivefromsubscription.js
184
396
```
185
-
1. You should see the following output.
186
397
187
-
```console
188
-
Received message: Albert Einstein
189
-
Received message: Werner Heisenberg
190
-
Received message: Marie Curie
191
-
Received message: Steven Hawking
192
-
Received message: Isaac Newton
193
-
Received message: Niels Bohr
194
-
Received message: Michael Faraday
195
-
Received message: Galileo Galilei
196
-
Received message: Johannes Kepler
197
-
Received message: Nikolaus Kopernikus
198
-
```
398
+
---
399
+
400
+
You should see the following output.
401
+
402
+
```console
403
+
Received message: Albert Einstein
404
+
Received message: Werner Heisenberg
405
+
Received message: Marie Curie
406
+
Received message: Steven Hawking
407
+
Received message: Isaac Newton
408
+
Received message: Niels Bohr
409
+
Received message: Michael Faraday
410
+
Received message: Galileo Galilei
411
+
Received message: Johannes Kepler
412
+
Received message: Nikolaus Kopernikus
413
+
```
199
414
200
415
In the Azure portal, navigate to your Service Bus namespace, switch to **Topics** in the bottom pane, and select your topic to see the **Service Bus Topic** page for your topic. On this page, you should see 10 incoming and 10 outgoing messages in the **Messages** chart.
201
416
@@ -209,6 +424,14 @@ On this page, if you select a subscription in the bottom pane, you get to the **
If you receive an error when running the **passwordless** version of the JavaScript code about required claims, 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.
430
+
431
+
## Clean up resources
432
+
433
+
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