Azure Service Bus client library samples for TypeScript

These sample programs show how to use the TypeScript client libraries for Azure Service Bus in some common scenarios.

File Name Description
sendMessages.ts Demonstrates how to send messages to Service Bus Queue/Topic
topicSubscriptionWithRuleOperationsSample.ts Demonstrates how to filter messages in Service Bus
receiveMessagesLoop.ts Demonstrates how to receive Service Bus messages in a loop
receiveMessagesStreaming.ts Demonstrates how to receive Service Bus messages in a stream
usingAadAuth.ts This sample how to create a namespace using AAD token credentials
usingNamedKeyCredential.ts This sample demonstrates how to authenticate using AzureNamedKeyCredential
browseMessages.ts Demonstrates how to browse a Service Bus message
session.ts Demonstrates how to send/receive messages to/from session enabled queues/subscriptions in Service Bus
scheduledMessages.ts Demonstrates how to schedule messages to appear on a Service Bus Queue/Subscription at a later time
useProxy.ts This sample demonstrates how to create a ServiceBusClient meant to be used in an environment where outgoing network requests have to go through a proxy server
advanced/administrationClient.ts Demonstrates how to manage the resources of a service bus namespace.
advanced/ruleManager.ts Demonstrates how to manage subscription-level rules using RuleManager.
advanced/sessionRoundRobin.ts Demonstrates how to continually read through all the available sessions
advanced/deferral.ts Demonstrates how to defer a message for later processing.
advanced/listingEntities.ts Demonstrates how the ServiceBusAdministrationClient can be used to list the entities of a service bus namespace
advanced/sessionState.ts Demonstrates usage of SessionState.
advanced/movingMessagesToDLQ.ts Demonstrates scenarios as to how a Service Bus message can be explicitly moved to the DLQ
advanced/processMessageFromDLQ.ts Demonstrates retrieving a message from a dead letter queue, editing it and sending it back to the main queue
exceedMaxDeliveryCount.ts Demonstrates exceeding the max delivery count, then processing the messages sent to the dead letter queue

Prerequisites

The sample programs are compatible with LTS versions of Node.js.

Before running the samples in Node, they must be compiled to JavaScript using the TypeScript compiler. For more information on TypeScript, see the TypeScript documentation. Install the TypeScript compiler using:

npm install -g typescript

You need an Azure subscription and the following Azure resources to run these sample programs:

Samples retrieve credentials to access the service endpoint from environment variables. Alternatively, edit the source code to include the appropriate credentials. See each individual sample for details on which environment variables/credentials it requires to function.

Adapting the samples to run in the browser may require some additional consideration. For details, please see the package README.

Setup

To run the samples using the published version of the package:

  1. Install the dependencies using npm:
npm install
  1. Compile the samples:
npm run build
  1. Edit the file sample.env, adding the correct credentials to access the Azure service and run the samples. Then rename the file from sample.env to just .env. The sample programs will read this file automatically.

  2. Run whichever samples you like (note that some samples may require additional setup, see the table above):

node dist/sendMessages.js

Alternatively, run a single sample with the correct environment variables set (setting up the .env file is not required if you do this), for example (cross-platform):

npx cross-env SERVICEBUS_CONNECTION_STRING="<servicebus connection string>" QUEUE_NAME="<queue name>" node dist/sendMessages.js

Next Steps

Take a look at our API Documentation for more information about the APIs that are available in the clients.