Tutorial: Azure Functions Queue Binding with JavaScript

This tutorial guides you through creating an Azure Function that uses a queue binding to process messages from an Azure Storage Queue. We'll focus on a JavaScript implementation.

Prerequisites

1. Create a New Azure Functions Project

Using Visual Studio Code

  1. Open Visual Studio Code.
  2. Press F1 to open the Command Palette.
  3. Type and select Azure Functions: Create New Project....
  4. Choose a folder for your project.
  5. Select your preferred language: JavaScript.
  6. Choose a template: Azure Queue Storage trigger.
  7. Name your queue trigger function (e.g., QueueTriggerJS).
  8. Provide the name of your storage account connection string (e.g., AzureWebJobsStorage).
  9. Specify the name of the queue to monitor (e.g., myqueue-items).
If you are using the Azure Storage Emulator, ensure it is running. The default connection string `UseDevelopmentStorage=true` will work. For a real Azure Storage Account, you'll need to configure the connection string in your local.settings.json file.

2. Understand the Generated Files

Visual Studio Code will create a project structure that typically includes:

function.json Example

This file defines the input and output bindings for your function. The queue trigger binding is specified here.


{
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "myqueue-items",
      "connection": "AzureWebJobsStorage"
    }
  ],
  "scriptFile": "index.js"
}
        

index.js Example

This is where your JavaScript code resides. It receives messages from the queue.


module.exports = async function (context, myQueueItem) {
    context.log('JavaScript queue trigger function processed work item:', myQueueItem);

    // 'myQueueItem' contains the message from the queue.
    // It can be a string, JSON object, or other data types depending on what was put into the queue.

    // Example: If the queue item is a JSON object
    if (typeof myQueueItem === 'object' && myQueueItem !== null) {
        context.log('Processing JSON message:', myQueueItem);
        if (myQueueItem.name) {
            context.log('Hello, ' + myQueueItem.name + '!');
        }
    } else {
        context.log('Processing string message:', myQueueItem);
    }

    // You can add further processing here, like writing to a database,
    // sending an email, or triggering another service.
};
        
The context.log function is used for logging messages that appear in the Functions host logs.

3. Running the Function Locally

  1. Ensure your Azure Storage Emulator is running or your Azure Storage Account is configured in local.settings.json.
  2. Open the integrated terminal in VS Code (Ctrl+` or Cmd+`).
  3. Navigate to your project directory.
  4. Run the command: func start

The Functions host will start, and your queue trigger function will be active, listening for messages on the specified queue.

4. Testing the Function

You can add messages to the queue using various methods:

When a message is added to the myqueue-items queue, your JavaScript Azure Function will automatically trigger and process it.

5. Deploying to Azure

Once you're ready to deploy:

  1. Ensure you are logged into Azure via the Azure Functions extension in VS Code.
  2. Right-click on your project folder in VS Code Explorer.
  3. Select Deploy to Function App....
  4. Choose an existing Function App or create a new one.

After deployment, your function will run in Azure and process messages from your Azure Storage Queue.

Advanced Concepts

Output Bindings

You can also add output bindings to send data to other Azure services. For example, to write to another queue or a table:


{
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "myqueue-items",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "outputQueueItem",
      "type": "queue",
      "direction": "out",
      "queueName": "processed-items",
      "connection": "AzureWebJobsStorage"
    }
  ],
  "scriptFile": "index.js"
}
        

And in your index.js:


module.exports = async function (context, myQueueItem) {
    context.log('JavaScript queue trigger function processed work item:', myQueueItem);

    // Process the message
    const processedMessage = `Processed: ${myQueueItem}`;

    // Set the output binding to send to another queue
    context.bindings.outputQueueItem = processedMessage;

    context.log(`Sent "${processedMessage}" to processed-items queue.`);
};
        

Error Handling and Retries

Azure Functions automatically retries queue-triggered functions on exceptions. You can configure retry policies in your host.json file.

Message Serialization

When sending JSON data to the queue, ensure your function correctly parses it. JavaScript Azure Functions often automatically deserialize JSON messages if the content type is appropriate.