FastAPI SSE working Locally but not in Azure Web App?

DAS Ujaan 5 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.
8,930 questions
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.