ChilliCream / graphql-platform

Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Banana Cake Pop the awesome Monaco based GraphQL IDE.
https://chillicream.com
MIT License
4.96k stars 722 forks source link

Add "X-Accel-Buffering: no" header to improve subscriptions with reverse proxy #7084

Open zshall opened 2 weeks ago

zshall commented 2 weeks ago

Product

Hot Chocolate

Is your feature request related to a problem?

I've personally encountered this issue but I also saw others are experiencing it so I'm opening an issue.

The issue is that when you use Server-Sent Events in Subscriptions in Hot Chocolate and your server is running behind a reverse proxy such as Azure Application Gateway that uses response buffering, you either never receive subscription events or the events are delayed. The reason for this is because Hot Chocolate's endpoints don't add a specific header: X-Accel-Buffering set to no, which configures the reverse proxy to send data as its received rather than waiting for the entire (potentially endless) response.

I figured this out based off of this StackOverflow question but there isn't a lot of information about it that I've been able to find.

The solution you'd like

The solution I used to fix this problem was to add a middleware to detect whether we're serving GraphQL requests and to add this header:

app.Use((ctx, next) => {
    if (ctx.Request.Path.ToString().Contains("/graphql")) {
        var headers = ctx.Response.Headers;
        headers.Append("X-Accel-Buffering", "no");
    }
    return next();
});

This works, but I had to do it this way because I couldn't find a way to configure the headers that Hot Chocolate sends to include the header. It'd be advantageous if this was an option built into Hot Chocolate and documented so that anyone else using a setup like this can benefit.