| title | Get started with Azure Service Bus topics (JavaScript) | |||
|---|---|---|---|---|
| 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. | |||
| author | spelluru | |||
| ms.author | spelluru | |||
| ms.date | 12/08/2023 | |||
| ms.topic | quickstart | |||
| ms.devlang | javascript | |||
| ms.custom |
|
Send messages to an Azure Service Bus topic and receive messages from subscriptions to the topic (JavaScript)
[!div class="op_single_selector" title1="Select the programming language:"]
In this tutorial, you complete the following steps:
- Create a Service Bus namespace, using the Azure portal.
- Create a Service Bus topic, using the Azure portal.
- Create a Service Bus subscription to that topic, using the Azure portal.
- Write a JavaScript application to use the @azure/service-bus package to:
- Send a set of messages to the topic.
- Receive those messages from the subscription.
Note
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.
- An Azure subscription. To complete this tutorial, you need an Azure account. You can activate your MSDN subscriber benefits or sign up for a free account.
- Node.js LTS
- Follow steps in the Quickstart: Use the Azure portal to create a Service Bus topic and subscriptions to the topic. You will use only one subscription for this quickstart.
To use this quickstart with your own Azure account, you need:
- Install Azure CLI, which provides the passwordless authentication to your developer machine.
- Sign in with your Azure account at the terminal or command prompt with
az login. - Use the same account when you add the appropriate role to your resource.
- Run the code in the same terminal or command prompt.
- Note down your topic name and subscription for your Service Bus namespace. You'll need that in the code.
Note down the following, which you'll use in the code below:
- Service Bus namespace connection string
- Service Bus namespace topic name you created
- Service Bus namespace subscription
Note
- This tutorial works with samples that you can copy and run using Nodejs. For instructions on how to create a Node.js application, see Create and deploy a Node.js application to an Azure Website, or Node.js Cloud Service using Windows PowerShell.
[!INCLUDE service-bus-create-namespace-portal]
[!INCLUDE service-bus-create-topic-subscription-portal]
[!INCLUDE service-bus-passwordless-template-tabbed]
-
To install the required npm package(s) for Service Bus, open a command prompt that has
npmin its path, change the directory to the folder where you want to have your samples and then run this command. -
Install the following packages:
npm install @azure/service-bus @azure/identity
-
To install the required npm package(s) for Service Bus, open a command prompt that has
npmin its path, change the directory to the folder where you want to have your samples and then run this command. -
Install the following package:
npm install @azure/service-bus
The following sample code shows you how to send a batch of messages to a Service Bus topic. See code comments for details.
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.
-
Open your favorite editor, such as Visual Studio Code
-
Create a file called
sendtotopic.jsand paste the below code into it. This code will send a message to your topic.[!IMPORTANT] The passwordless credential is provided with the DefaultAzureCredential.
const { ServiceBusClient } = require("@azure/service-bus"); const { DefaultAzureCredential } = require("@azure/identity"); // Replace `<SERVICE-BUS-NAMESPACE>` with your namespace const fullyQualifiedNamespace = "<SERVICE-BUS-NAMESPACE>.servicebus.windows.net"; // Passwordless credential const credential = new DefaultAzureCredential(); const topicName = "<TOPIC NAME>"; const messages = [ { body: "Albert Einstein" }, { body: "Werner Heisenberg" }, { body: "Marie Curie" }, { body: "Steven Hawking" }, { body: "Isaac Newton" }, { body: "Niels Bohr" }, { body: "Michael Faraday" }, { body: "Galileo Galilei" }, { body: "Johannes Kepler" }, { body: "Nikolaus Kopernikus" } ]; async function main() { // create a Service Bus client using the passwordless authentication to the Service Bus namespace const sbClient = new ServiceBusClient(fullyQualifiedNamespace, credential); // createSender() can also be used to create a sender for a queue. const sender = sbClient.createSender(topicName); try { // Tries to send all messages in a single batch. // Will fail if the messages cannot fit in a batch. // await sender.sendMessages(messages); // create a batch object let batch = await sender.createMessageBatch(); for (let i = 0; i < messages.length; i++) { // for each message in the array // try to add the message to the batch if (!batch.tryAddMessage(messages[i])) { // if it fails to add the message to the current batch // send the current batch as it is full await sender.sendMessages(batch); // then, create a new batch batch = await sender.createMessageBatch(); // now, add the message failed to be added to the previous batch to this batch if (!batch.tryAddMessage(messages[i])) { // if it still can't be added to the batch, the message is probably too big to fit in a batch throw new Error("Message too big to fit in a batch"); } } } // Send the last created batch of messages to the topic await sender.sendMessages(batch); console.log(`Sent a batch of messages to the topic: ${topicName}`); // Close the sender await sender.close(); } finally { await sbClient.close(); } } // call the main function main().catch((err) => { console.log("Error occurred: ", err); process.exit(1); });
-
Replace
<SERVICE BUS NAMESPACE CONNECTION STRING>with the connection string to your Service Bus namespace. -
Replace
<TOPIC NAME>with the name of the topic. -
Then run the command in a command prompt to execute this file.
node sendtotopic.js -
You should see the following output.
Sent a batch of messages to the topic: mytopic
-
Open your favorite editor, such as Visual Studio Code
-
Create a file called
sendtotopic.jsand paste the below code into it. This code will send a message to your topic.const { ServiceBusClient } = require("@azure/service-bus"); const connectionString = "<SERVICE BUS NAMESPACE CONNECTION STRING>" const topicName = "<TOPIC NAME>"; const messages = [ { body: "Albert Einstein" }, { body: "Werner Heisenberg" }, { body: "Marie Curie" }, { body: "Steven Hawking" }, { body: "Isaac Newton" }, { body: "Niels Bohr" }, { body: "Michael Faraday" }, { body: "Galileo Galilei" }, { body: "Johannes Kepler" }, { body: "Nikolaus Kopernikus" } ]; async function main() { // create a Service Bus client using the connection string to the Service Bus namespace const sbClient = new ServiceBusClient(connectionString); // createSender() can also be used to create a sender for a queue. const sender = sbClient.createSender(topicName); try { // Tries to send all messages in a single batch. // Will fail if the messages cannot fit in a batch. // await sender.sendMessages(messages); // create a batch object let batch = await sender.createMessageBatch(); for (let i = 0; i < messages.length; i++) { // for each message in the array // try to add the message to the batch if (!batch.tryAddMessage(messages[i])) { // if it fails to add the message to the current batch // send the current batch as it is full await sender.sendMessages(batch); // then, create a new batch batch = await sender.createMessageBatch(); // now, add the message failed to be added to the previous batch to this batch if (!batch.tryAddMessage(messages[i])) { // if it still can't be added to the batch, the message is probably too big to fit in a batch throw new Error("Message too big to fit in a batch"); } } } // Send the last created batch of messages to the topic await sender.sendMessages(batch); console.log(`Sent a batch of messages to the topic: ${topicName}`); // Close the sender await sender.close(); } finally { await sbClient.close(); } } // call the main function main().catch((err) => { console.log("Error occurred: ", err); process.exit(1); });
-
Replace
<SERVICE BUS NAMESPACE CONNECTION STRING>with the connection string to your Service Bus namespace. -
Replace
<TOPIC NAME>with the name of the topic. -
Then run the command in a command prompt to execute this file.
node sendtotopic.js -
You should see the following output.
Sent a batch of messages to the topic: mytopic
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.
-
Open your favorite editor, such as Visual Studio Code
-
Create a file called receivefromsubscription.js and paste the following code into it. See code comments for details.
const { delay, ServiceBusClient, ServiceBusMessage } = require("@azure/service-bus"); const { DefaultAzureCredential } = require("@azure/identity"); // Replace `<SERVICE-BUS-NAMESPACE>` with your namespace const fullyQualifiedNamespace = "<SERVICE-BUS-NAMESPACE>.servicebus.windows.net"; // Passwordless credential const credential = new DefaultAzureCredential(); const topicName = "<TOPIC NAME>"; const subscriptionName = "<SUBSCRIPTION NAME>"; async function main() { // create a Service Bus client using the passwordless authentication to the Service Bus namespace const sbClient = new ServiceBusClient(fullyQualifiedNamespace, credential); // createReceiver() can also be used to create a receiver for a queue. const receiver = sbClient.createReceiver(topicName, subscriptionName); // function to handle messages const myMessageHandler = async (messageReceived) => { console.log(`Received message: ${messageReceived.body}`); }; // function to handle any errors const myErrorHandler = async (error) => { console.log(error); }; // subscribe and specify the message and error handlers receiver.subscribe({ processMessage: myMessageHandler, processError: myErrorHandler }); // Waiting long enough before closing the sender to send messages await delay(5000); await receiver.close(); await sbClient.close(); } // call the main function main().catch((err) => { console.log("Error occurred: ", err); process.exit(1); });
-
Replace
<SERVICE BUS NAMESPACE CONNECTION STRING>with the connection string to the namespace. -
Replace
<TOPIC NAME>with the name of the topic. -
Replace
<SUBSCRIPTION NAME>with the name of the subscription to the topic. -
Then run the command in a command prompt to execute this file.
node receivefromsubscription.js
-
Open your favorite editor, such as Visual Studio Code
-
Create a file called receivefromsubscription.js and paste the following code into it. See code comments for details.
const { delay, ServiceBusClient, ServiceBusMessage } = require("@azure/service-bus"); const connectionString = "<SERVICE BUS NAMESPACE CONNECTION STRING>" const topicName = "<TOPIC NAME>"; const subscriptionName = "<SUBSCRIPTION NAME>"; async function main() { // create a Service Bus client using the connection string to the Service Bus namespace const sbClient = new ServiceBusClient(connectionString); // createReceiver() can also be used to create a receiver for a queue. const receiver = sbClient.createReceiver(topicName, subscriptionName); // function to handle messages const myMessageHandler = async (messageReceived) => { console.log(`Received message: ${messageReceived.body}`); }; // function to handle any errors const myErrorHandler = async (error) => { console.log(error); }; // subscribe and specify the message and error handlers receiver.subscribe({ processMessage: myMessageHandler, processError: myErrorHandler }); // Waiting long enough before closing the sender to send messages await delay(5000); await receiver.close(); await sbClient.close(); } // call the main function main().catch((err) => { console.log("Error occurred: ", err); process.exit(1); });
-
Replace
<SERVICE BUS NAMESPACE CONNECTION STRING>with the connection string to the namespace. -
Replace
<TOPIC NAME>with the name of the topic. -
Replace
<SUBSCRIPTION NAME>with the name of the subscription to the topic. -
Then run the command in a command prompt to execute this file.
node receivefromsubscription.js
You should see the following output.
Received message: Albert Einstein
Received message: Werner Heisenberg
Received message: Marie Curie
Received message: Steven Hawking
Received message: Isaac Newton
Received message: Niels Bohr
Received message: Michael Faraday
Received message: Galileo Galilei
Received message: Johannes Kepler
Received message: Nikolaus KopernikusIn 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.
:::image type="content" source="./media/service-bus-nodejs-how-to-use-topics-subscriptions/topic-page-portal.png" alt-text="Incoming and outgoing messages":::
If you run only the send app next time, on the Service Bus Topic page, you see 20 incoming messages (10 new) but 10 outgoing messages.
:::image type="content" source="./media/service-bus-nodejs-how-to-use-topics-subscriptions/updated-topic-page.png" alt-text="Updated topic page":::
On this page, if you select a subscription in the bottom pane, you get to the Service Bus Subscription page. You can see the active message count, dead-letter message count, and more on this page. In this example, there are 10 active messages that haven't been received by a receiver yet.
:::image type="content" source="./media/service-bus-nodejs-how-to-use-topics-subscriptions/active-message-count.png" alt-text="Active message count":::
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 is applied to your Azure user account.
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.
See the following documentation and samples: