sndev
Project CategoriesDeveloper LearnDeveloper Insights
LoginGet Started
sndevv1.0.0
AboutProject FeedInsightsYouTubePrivacy PolicyTerms of Service

Β© 2026 sndev β€’ v1.0.0

Home
Categories
Dev Learn
Insights
Sign In
Agentic AI
Nov 28, 2026
s
sndev

Building a Real-Time WebSocket Streaming Engine with FastAPI | Deep Research Agent – Part 12

GitHub RepositoryWatch on YouTube

β€œImplement a real-time WebSocket streaming engine with FastAPI, managing report-specific connections and broadcasting live LangGraph agent execution updates to the frontend”

Project SeriesPart 12 of 13

Deep Research Agent

Series Progress92%
Series Episodes (13)
Build an Autonomous Deep Research Agent | End-to-End Agentic AI Project | Part 1
Available
Setup & Installation for Autonomous Deep Research Agent | End-to-End Agentic AI Project | Part 2
Available
Building the Agentic AI Brain with LangGraph | Autonomous Deep Research Agent | Part 3
Available
Building AI Agent with LangGraph | Planner Agent Implementation | Deep Research Agent | Part 4
Available
Building AI Agent Tools | Rate Limiting & Source Credibility | Deep Research Agent | Part 5
Available
Building a Web Search Tool for AI Agents with Tavily API | Deep Research Agent Part 6
Available
Building arXiv Search, Cross-Referencing, and Citation Tools | Deep Research Agent Part 7
Available
Building the Researcher Agent with Web Search, arXiv & Source Ranking | Deep Research Agent Part 8
Available
Building the Critic Agent: Detecting Conflicting Research | Deep Research System Part 9
Available
Building the Writer Agent – Generating Structured AI Research Reports | Part 10
Available
Building a Multi-Agent Workflow with LangGraph | Deep Research Agent – Part 11
Available
P12
Building a Real-Time WebSocket Streaming Engine with FastAPI | Deep Research Agent – Part 12
Current Part
Building and Deploying a Full-Stack AI Deep Research Agent | Part 13 – Final Chapter
Available
Part 11Part 13

In this part of our Autonomous Deep Research Agent series, we implement a real-time WebSocket streaming engine using FastAPI.

Our LangGraph-based multi-agent workflow is already capable of executing the Planner, Researcher, Critic, and Writer agents. However, research tasks can take 30–60 seconds or even longer to complete. Without real-time updates, users would have no indication of what is happening in the background.

To solve this, we introduce WebSockets to stream agent execution statuses directly from the backend to the frontend.

1. Why WebSockets Instead of HTTP Polling?

One approach to tracking background execution is HTTP polling, where the frontend repeatedly sends requests to the backend asking whether the task has completed.

For example, the frontend might send a request every few seconds:

  • Is the research complete?

  • Is the researcher still running?

  • Has the report been generated?

This creates unnecessary requests and increases server overhead.

Instead, we implement WebSockets, which establish a persistent, two-way communication channel between the frontend and backend.

Whenever an agent completes a step, the backend can immediately push an update to the frontend without waiting for another client request.

For our application, the initial updates will include statuses such as:

  • Planning

  • Researching

  • Reviewing

  • Writing

This gives users visibility into the agent workflow while the research is running.

2. Implementing the WebSocket Connection Manager

We create a new router file:

router/websocket.py

The core component is a ConnectionManager class responsible for managing active WebSocket connections.

A critical requirement is supporting multiple users researching different topics simultaneously.

For example, if 100 users are running 100 different research tasks, each user should receive updates only for their own report.

To achieve this, we maintain a dictionary that maps each report ID to its active WebSocket connections.

The Connection Manager provides three important methods:

Connect: Accepts an incoming WebSocket connection and registers it against the corresponding report ID.

Disconnect: Removes a disconnected WebSocket from the active connection list. If no connections remain for a report, its entry is removed from the dictionary. This helps prevent stale connections from accumulating.

Broadcast: Sends JSON messages to all active connections associated with a particular report ID. If sending a message fails, the connection is handled through the disconnect mechanism.

This report-specific mapping ensures that updates from one research task are not accidentally delivered to another user's session.

3. Broadcasting Agent Execution Statuses

In the previous part, we implemented an update callback inside our LangGraph execution workflow.

That callback receives status changes from the agents.

Now, we connect this mechanism to our WebSocket Connection Manager.

The flow is straightforward:

  1. The LangGraph agent updates its execution status.

  2. The update callback receives the new status.

  3. The callback invokes the broadcast method.

  4. The Connection Manager identifies active connections for that report ID.

  5. The status is sent to the connected frontend as a JSON message.

This connects our existing multi-agent execution workflow with the real-time communication layer.

4. Creating the WebSocket API Endpoint

Next, we expose a WebSocket endpoint using FastAPI:

/reports/{report_id}/stream

The endpoint accepts two important parameters:

  • websocket: The incoming WebSocket connection.

  • report_id: The identifier used to associate the connection with a specific research report.

When a client connects, the endpoint registers the connection through the Connection Manager.

We then keep the connection alive using an asynchronous loop. In our current implementation, the client does not need to send application data; we use receive_text() to keep the connection open and handle incoming messages if needed.

When a WebSocket disconnection occurs, the endpoint catches WebSocketDisconnect and removes the connection from the manager.

With this, our real-time WebSocket streaming engine is ready.

5. Architecture Overview

Our communication flow now looks like this:

Generating Architecture Diagram…

What's Next?

In this part, we completed the WebSocket communication layer that will allow our frontend to receive live updates from the research workflow.

At this stage, we are streaming execution statuses rather than the internal reasoning of the agents. We will progressively enhance this experience in upcoming parts.

In Part 13, we will move forward with building the remaining API endpoints, connecting the routers to our main FastAPI application, and testing the APIs using Postman.

We are gradually transforming our LangGraph workflow into a complete, interactive Deep Research Agent application.

Keep Building. Keep Learning. πŸš€

#AIEngineering #AgenticAI #LangGraph #FastAPI #WebSockets #Python #DeepResearchAgent #SNDev

Series Navigation

Up Next: Part 13

Continue following "Deep Research Agent" with Building and Deploying a Full-Stack AI Deep Research Agent | Part 13 – Final Chapter.

Part 11Read Part 13

Community Discussion
0

Ask questions, discuss architecture, and share insights with other developers.

Sort:

Sign in to join the discussion and share your thoughts with other developers.

Sign In to Comment
Loading discussions…