Azure SDK for JavaScript - Queue Storage

This documentation provides comprehensive guidance on using the Azure SDK for JavaScript to interact with Azure Queue Storage. Azure Queue Storage is a service that allows you to store large numbers of messages that can be accessed from anywhere in the world via an HTTP or HTTPS connection.

Getting Started

To get started with Azure Queue Storage using the JavaScript SDK, you'll need to install the appropriate package:

npm install @azure/storage-queue

Once installed, you can start interacting with your queues.

Core Concepts

Key Operations

1. Creating a Queue Client

You'll typically start by creating a QueueServiceClient, and then use it to get a QueueClient for a specific queue.


// Replace with your connection string or anonymous credentials
const connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
const queueName = "my-queue";

const { QueueServiceClient } = require("@azure/storage-queue");

async function createQueueClient() {
    const queueServiceClient = QueueServiceClient.fromConnectionString(connectionString);
    const queueClient = queueServiceClient.getQueueClient(queueName);
    console.log(`Queue client created for queue: ${queueName}`);
    return queueClient;
}
            

2. Creating a Queue

You can create a new queue if it doesn't already exist.


async function createQueue(queueClient) {
    try {
        await queueClient.create();
        console.log(`Queue "${queueClient.name}" created successfully.`);
    } catch (error) {
        if (error.statusCode === 409) {
            console.log(`Queue "${queueClient.name}" already exists.`);
        } else {
            console.error("Error creating queue:", error);
        }
    }
}
            

3. Sending a Message

Add a message to the queue.


async function sendMessage(queueClient, messageText) {
    try {
        const response = await queueClient.sendMessage(messageText);
        console.log("Message sent successfully:", response.messageId);
        return response.messageId;
    } catch (error) {
        console.error("Error sending message:", error);
    }
}
            

4. Receiving Messages

Retrieve one or more messages from the queue. The messages are temporarily hidden and will become visible again after the visibilityTimeout expires if they are not deleted.


async function receiveMessages(queueClient) {
    try {
        const peekedMessages = await queueClient.receiveMessages({ numberOfMessages: 5 });
        if (peekedMessages.length === 0) {
            console.log("No messages in the queue.");
            return;
        }

        console.log("Received messages:");
        for (const msg of peekedMessages) {
            console.log(`  Message ID: ${msg.messageId}, Text: ${msg.messageText}`);
        }
        return peekedMessages;
    } catch (error) {
        console.error("Error receiving messages:", error);
    }
}
            

5. Deleting a Message

Once a message has been processed, it should be deleted from the queue using its messageId and popReceipt.


async function deleteMessage(queueClient, messageId, popReceipt) {
    try {
        await queueClient.deleteMessage(messageId, popReceipt);
        console.log(`Message ${messageId} deleted successfully.`);
    } catch (error) {
        console.error(`Error deleting message ${messageId}:`, error);
    }
}
            

API Reference

Below is a summary of common methods available on the QueueClient.

Method Description Parameters
create() Creates a new queue. None
deleteQueue() Deletes a queue and all of its messages. None
sendMessage(messageText) Adds a message to the queue. messageText (string)
receiveMessages(options) Retrieves one or more messages from the front of the queue. options (object, optional) with properties like numberOfMessages, visibilityTimeout.
peekMessages(options) Retrieves one or more messages from the front of the queue without making them invisible. options (object, optional) with properties like numberOfMessages.
deleteMessage(messageId, popReceipt) Deletes a specific message from the queue. messageId (string), popReceipt (string)
clearMessages() Deletes all messages from the queue. None
Important: Always ensure you handle potential errors, such as queues not existing or message processing failures. The SDK provides detailed error objects to help diagnose issues.