npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cowood/service-bus

v1.0.5

Published

Azure Service Bus SDK for Node.js

Downloads

4

Readme

Azure Service Bus client library for Javascript

Azure Service Bus is a highly-reliable cloud messaging service from Microsoft

Use the client library for Azure Service Bus in your Node.js application to

  • Send messages to a Queue or Topic
  • Receive messages from a Queue or Subscription

Source code | Package (npm) | API Reference Documentation | Product documentation | Samples

Getting Started

Install the package

Install the Azure Service Bus client library using npm

npm install @azure/service-bus

Prerequisites: You must have an Azure subscription and a Service Bus Namespace to use this package. If you are using this package in a Node.js application, then use Node.js 6.x or higher.

Configure Typescript

TypeScript users need to have Node type definitions installed:

npm install @types/node

You also need to enable compilerOptions.allowSyntheticDefaultImports in your tsconfig.json. Note that if you have enabled compilerOptions.esModuleInterop, allowSyntheticDefaultImports is enabled by default. See TypeScript's compiler options handbook for more information.

Authenticate the client

Interaction with Service Bus starts with an instance of the ServiceBusClient class. You can instantiate this class using one of the 3 static methods on it

Key concepts

Once you have initialized the ServiceBusClient class, use the below methods to create client objects for Queues, Topics and Subscriptions to interact with existing Service Bus entities. Please note that the Queues, Topics and Subscriptions should already have been created prior to using this library.

  • createQueueClient
    • Takes the name of an existing Service Bus Queue instance, returns a QueueClient that you can use to send to and receive messages from the queue.
  • createTopicClient
    • Takes the name of an existing Service Bus Topic instance, returns a TopicClient that you can use to send messages to the topic
  • createSubscriptionClient
    • Takes the name of existing Service Bus Topic and Subscription instances, returns a SubscriptionClient that you can use to receive messages from the subscription.

Next, using the client object created in the previous step, create a sender or a receiver based on whether you want to send or receive messages

Examples

The following sections provide code snippets that cover some of the common tasks using Azure Service Bus

Send messages

Once you have created an instance of a QueueClient or SubscriptionClient class, create a sender using the createSender function. This gives you a sender which you can use to send messages.

You can also use the sendBatch method to send multiple messages using a single call.

const queueClient = serviceBusClient.createQueueClient("my-queue");
const sender = queueClient.createSender();
await sender.send({
  body: "my-message-body"
});
await sender.sendBatch([
  { body: "my-message-body-1" },
  { body: "my-message-body-2" },
  { body: "my-message-body-3" }
]);

Receive messages

Once you have created an instance of a QueueClient or SubscriptionClient class, create a receiver using the createReceiver function.

const queueClient = serviceBusClient.createQueueClient("my-queue");
const receiver = queueClient.createReceiver(ReceiveMode.peekLock);

You can use this receiver in one of 3 ways to receive messages:

Get an array of messages

Use the receiveMessages function which returns a promise that resolves to an array of messages.

const myMessages = await receiver.receiveMessages(10);

Register message handler

Use the registerMessageHandler to set up message handlers and have it running as long as you need. When you are done, call receiver.close() to stop receiving any more messages.

const myMessageHandler = async (message) => {
  // your code here
};
const myErrorHandler = (error) => {
  console.log(error);
};
receiver.registerMessageHandler(myMessageHandler, myErrorHandler);

Use async iterator

Use the getMessageIterator to get an async iterator over messages

for await (let message of receiver.getMessageIterator()){
  // your code here
}

Settle a message

Once you receive a message you can call complete(), abandon(), defer() or deadletter() on it based on how you want to settle the message. To learn more, please read Settling Received Messages

Send messages using Sessions

To send messages using sessions, you first need to create a session enabled Queue. You can do this in the Azure portal. Then, use an instance of QueueClient to create a sender using the createSender function. This gives you a sender which you can use to send messages.

When sending the message, set the sessionId property in the message body to ensure your message lands in the right session.

const queueClient = serviceBusClient.createQueueClient("my-session-queue");
const sender = queueClient.createSender();
await sender.send({
  body: "my-message-body",
  sessionId: "my-session"
});

Receive messages from Sessions

To receive messages from sessions, you first need to create a session enabled Queue and send messages to it. Then, use an instance of QueueClient or SubscriptionClient to create a receiver using the createReceiver function. Note that you will need to specify the session from which you want to receive messages.

const queueClient = serviceBusClient.createQueueClient("my-session-queue");
const receiver = queueClient.createReceiver(ReceiveMode.peekLock, { sessionId: "my-session" });

You can use this receiver in one of 3 ways to receive messages

Troubleshooting

AMQP Dependencies

The Service Bus library depends on the rhea-promise library for managing connections, sending and receiving messages over the AMQP protocol.

Enable logs

You can set the following environment variable to get the debug logs when using this library.

  • Getting debug logs from the Service Bus SDK
export DEBUG=azure*
  • Getting debug logs from the Service Bus SDK and the protocol level library.
export DEBUG=azure*,rhea*
  • If you are not interested in viewing the message transformation (which consumes lot of console/disk space) then you can set the DEBUG environment variable as follows:
export DEBUG=azure*,rhea*,-rhea:raw,-rhea:message,-azure:amqp-common:datatransformer
  • If you are interested only in errors, then you can set the DEBUG environment variable as follows:
export DEBUG=azure:service-bus:error,azure-amqp-common:error,rhea-promise:error,rhea:events,rhea:frames,rhea:io,rhea:flow

Logging to a file

  • Set the DEBUG environment variable as shown above and then run your test script as follows:
    • Logging statements from your test script go to out.log and logging statements from the sdk go to debug.log.
      node your-test-script.js > out.log 2>debug.log
    • Logging statements from your test script and the sdk go to the same file out.log by redirecting stderr to stdout (&1), and then redirect stdout to a file:
      node your-test-script.js >out.log 2>&1
    • Logging statements from your test script and the sdk go to the same file out.log.
        node your-test-script.js &> out.log

Next Steps

Please take a look at the samples directory for detailed examples on how to use this library to send and receive messages to/from Service Bus Queues, Topics and Subscriptions.

Contributing

If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.

Impressions