Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Does API gateway allow streaming?

I have a cloud run function n that streams data and the API gateway in front to protect the endpoint. If I call the function directly it streams the data fine, but if I call it through the gateway it buffers the whole stream until sending all at once. Does API gateway allow streaming? Do I need specific openAPI configuration?

The following function and config do not work

 

functions.http("simulateStream", async (req, res) => {
  res.setHeader("Content-Type", "text/event-stream");
  res.setHeader("Cache-Control", "no-cache");
  res.setHeader("Connection", "keep-alive");

  let counter = 0;
  const interval = setInterval(() => {
    counter++;
    res.write(`data: ${counter}\n\n`);
    res.flush = res.flush || (() => {});
    res.flush();
    if (counter > 50) {
      clearInterval(interval);
      res.end();
    }
  }, 1000);
});

 

and config:

 

swagger: "2.0"
info:
  title: test-api
  description: test-api
  version: 1.0.0
schemes:
  - https
produces:
  - application/json
paths:
  /simulateStream:
    get:
      summary: Simulate a stream
      operationId: simulateStream
      x-google-backend:
        address: https://tst.cloudfunctions.net/simulateStream
      responses:
        "200":
          description: A successful response
          schema:
            type: string

 

 

0 1 496
1 REPLY 1

Hi @artuntun,

Welcome to Google Cloud Community!

Currently, streaming is not yet supported for API Gateway. Our engineering team is still exploring solutions to get this supported for API Gateway in the future, but there’s no specific timeline yet as to when this may happen.

One workaround to consider here is to use Cloud Endpoint for OpenAPI and set it up for Cloud Run with ESPv2 which is the current recommendation to implement bi-directional streaming.

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.

Top Solution Authors