Azure Functions: Support for HTTP Streams in Node.js is Generally Available
Published May 21 2024 09:11 AM 1,420 Views

Azure Functions support for HTTP streams in Node.js is now generally available. With this feature, customers can stream HTTP requests to and responses from their Node.js Functions Apps. Streaming is a mechanism for transmitting data over HTTP in a continuous and efficient manner. Instead of sending all the data at once, streams allow data to be transmitted in small, manageable chunks, which can be processed as they arrive. They are particularly valuable in scenarios where low latency, high throughput, and efficient resource utilization are crucial. 

 

Ever since the preview release of this feature in February of this year, we've heard positive feedback from customers that have used this feature for various use cases including, but not limited to, streaming OpenAI responses, delivering dynamic content, processing large data etc. Today, at MS Build 2024, we announce General Availability of HTTP Streaming for Azure Functions using Node.js.

HTTP Streams in Node.js is supported only in the Azure Functions Node.js v4 programming model. Follow these instructions to try out HTTP Streams for your Node.js apps.

Prerequisites

 

  • Version 4 of the Node.js programming model. Learn more about the differences between v3 and v4 in the migration guide.
  • Version 4.3.0 or higher of the @azure/functions npm package.
  • If running in Azure, version 4.28 of the Azure Functions runtime.
  • If running locally, version 4.0.5530 of Azure Functions Core Tools.

 

Steps

 

  1. If you plan to stream large amounts of data, adjust the app setting FUNCTIONS_REQUEST_BODY_SIZE_LIMIT in Azure or in your local.settings.jsonfile. The default value is 104857600, i.e., limiting your request to 100mb maximum.
  2. Add the following code to your app in any file included by your main field.

    JavaScript

    const { app } = require('@azure/functions');
    
    app.setup({ enableHttpStream: true });

    TypeScript

    import { app } from '@azure/functions';
    
    app.setup({ enableHttpStream: true });
  3. That's it! The existing HttpRequestand HttpResponse types in programming model v4 already support many ways of handling the body, including as a stream. Use request.body to truly benefit from streams, but rest assured you can continue to use methods like request.text() which will always return the body as a string.

     

Example code

 

Below is an example of an HTTP triggered function that receives data via an HTTP POST request, and the function streams this data to a specified output file:

 

  • JavaScript

    const { app } = require('@azure/functions');
    const { createWriteStream } = require('fs');
    const { Writable } = require('stream');
    
    app.http('httpTriggerStreamRequest', {
        methods: ['POST'],
        authLevel: 'anonymous',
        handler: async (request, context) => {
            const writeStream = createWriteStream('<output file path>');
            await request.body.pipeTo(Writable.toWeb(writeStream));
    
            return { body: 'Done!' };
        },
    });
  • TypeScript

    import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions';
    import { createWriteStream } from 'fs';
    import { Writable } from 'stream';
    
    export async function httpTriggerStreamRequest(
        request: HttpRequest,
        context: InvocationContext
    ): Promise<HttpResponseInit> {
        const writeStream = createWriteStream('<output file path>');
        await request.body.pipeTo(Writable.toWeb(writeStream));
    
        return { body: 'Done!' };
    }
    
    app.http('httpTriggerStreamRequest', {
        methods: ['POST'],
        authLevel: 'anonymous',
        handler: httpTriggerStreamRequest,
    });

 

Below is an example of an HTTP triggered function that streams a file's content as the response to incoming HTTP GET requests:

 

  • JavaScript

    const { app } = require('@azure/functions');
    const { createReadStream } = require('fs');
    
    app.http('httpTriggerStreamResponse', {
        methods: ['GET'],
        authLevel: 'anonymous',
        handler: async (request, context) => {
            const body = createReadStream('<input file path>');
    
            return { body };
        },
    });
  • TypeScript

    import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions';
    import { createReadStream } from 'fs';
    
    export async function httpTriggerStreamResponse(
        request: HttpRequest,
        context: InvocationContext
    ): Promise<HttpResponseInit> {
        const body = createReadStream('<input file path>');
    
        return { body };
    }
    
    app.http('httpTriggerStreamResponse', {
        methods: ['GET'],
        authLevel: 'anonymous',
        handler: httpTriggerStreamResponse,
    });

 

Try it out!

 

For a ready-to-run sample app with more detailed code, check out this GitHub repo.

 

Check out this GitHub repo to discover the journey of building a generative AI application using LangChain.js and Azure. This demo explores the development process from idea to production, using a RAG-based approach for a Q&A system based on YouTube video transcripts.

 

Do try out this feature and share your valuable feedback with us on GitHub.

Version history
Last update:
‎May 23 2024 11:26 AM
Updated by: