FastAPI SSE working Locally but not in Azure Web App?

DAS Ujaan 0 Reputation points
2024-06-07T13:58:29.56+00:00

We set up a basic notification system using SSE from our FastAPI server which seems to work fine locally, but doesn't work when we push it to our Azure Web App? I have server logs where I can see the notification is sent and consumed, but we can't see it being recieved on the frontend? I've looked around and seen discussion about turning off response buffering, but can't seem to figure out WHERE to do that (where can I find web.config's equivalent for our proj? We use containers if that helps?).

Here's a brief example of how the route looks:

@router.get(
    "/stream-notifs",
    response_description="User task notifications",
    response_model=Response,
    tags=["users"],
)
async def notif_update_stream(request: Request, user_id: str):
    # print(request.state.q)

    MESSAGE_STREAM_DELAY = 1  # second
    MESSAGE_STREAM_RETRY_TIMEOUT = 15000  # milisecond

    client_host = request.client.host
    print(f"Client {client_host} connected.")

    async def event_generator():
        notif_q: asyncio.Queue = request.state.notif_q
        this_user_notifs = []

        while True:
            notif_data: NotificationData = await notif_q.get()

            if notif_data.creator_id == UUID(user_id):
                # TODO: Add validation
                this_user_notifs.append(notif_data)
                print(f"Sending to: {request.client}")
            else:
                notif_q.put_nowait(notif_data)

            # If client was closed the connection
            if await request.is_disconnected():
                break

            # Checks for new messages and return them to client if any
            for curr_notif in this_user_notifs:  # Whenever a task is completed
                notif = Notification(
                    id="message_id",
                    event="completed_task",
                    retry=MESSAGE_STREAM_RETRY_TIMEOUT,
                    data=curr_notif,
                ).model_dump_json()
                yield notif
                this_user_notifs.pop()
                notif_q.task_done()
                print(f"Finished notif {notif}")

            await asyncio.sleep(MESSAGE_STREAM_DELAY)

        await notif_q.join()

    return EventSourceResponse(
        event_generator(),
        media_type="text/event-stream",
        headers={
            "Content-Type": "text/event-stream",
            "Cache-Control": "no-cache",
            "Transfer-Encoding": "chunked",
            "Connection": "keep-alive",
            "X-Accel-Buffering": "no",
        },
    )


The team is pretty new to Azure, so there's probably something I missed here. The backend is hosted on a container on a web app like I mentioned, and the frontend is the same (Vue). Any help/advice would be greatly appreciated!

As mentioned, I tried finding the settings to disable response buffering, looked for web.config, added the X-Accel-Buffering and Cache-Control headers but to no avail.

Any help at all would be greatly appreciated!

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,270 questions
{count} votes

1 answer

Sort by: Most helpful
  1. ajkuma 24,396 Reputation points Microsoft Employee
    2024-06-12T18:32:18.87+00:00

    Apologies for the delayed response here.

    I see that you have posted the question on SO and an active discussion is ongoing.
    If you haven't done, kindly take a look the steps outlined in the guide on deploying a Flask or FastAPI web app as a container in Azure App Service, which includes configuring Gunicorn, a production-level web server that forwards web requests to the FastAPI framework.

    Added pointers shared by Vivek on your SO thread - if web app is not working sometimes in normal app service and you have to send it repeatedly.

    • Make sure App service computation is not reaching threshold.
    • Upgrade your app service plan which can handle computation.

    For additional info on the deployment: Deploy a containerized Flask or FastAPI web app on Azure App Service

    If it's feasible/you are fine, could you please share your webapp name/URL here.

    --If the answer helped (pointed, you in the right direction) > please click Accept Answer - it benefits the community to find the answers quicker.

    0 comments No comments