¡Hola Maria Natalia Benavides Sanabria!
Bienvenido a Microsoft Q&A.
The issue you're experiencing, where network requests remain in a "pending" state and the real-time connection is not established, could be caused by several factors. Below is a detailed analysis and troubleshooting guide tailored to your environment:
- WebSocket or Long-Polling Configuration
FastAPI and Uvicorn support WebSockets for real-time communication, but IIS may require additional configuration to handle WebSocket connections properly.
Steps to Check:
Enable WebSockets in IIS:
- Open IIS Manager.
- Select your server in the left-hand Connections pane.
- Double-click "WebSocket Protocol" in the Features pane.
- Ensure WebSocket Protocol is enabled for your site.
from fastapi import FastAPI, WebSocket
- Double-click "WebSocket Protocol" in the Features pane.
- Select your server in the left-hand Connections pane.
app = FastAPI()
@app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket): await websocket.accept() await websocket.send_text("Hello WebSocket!") await websocket.close() ```
**Check Uvicorn Version**: Ensure you're using a compatible version of Uvicorn (>=0.15.0). If you're using WebSockets, Uvicorn must be started with the `--ws` flag:
```yaml
uvicorn app:app --host 0.0.0.0 --port 8000 --ws websockets
```
- IIS Reverse Proxy Configuration
If IIS is acting as a reverse proxy for your FastAPI application, misconfigurations in the web.config
file could block WebSocket or HTTP/2 connections.
Steps to Check:
Update web.config
: Ensure your web.config
file allows WebSocket traffic and properly forwards requests to Uvicorn. Example configuration:
<configuration>
<system.webServer>
<webSocket enabled="true" />
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://127.0.0.1:8000/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Check Timeouts: IIS may terminate long-running connections prematurely. Increase the timeout settings for WebSocket connections:
<webSocket enabled="true" receiveTimeout="00:20:00" />
- FastAPI and Python Compatibility
You're using Python 3.13.2, which is a very recent version. Some libraries, including FastAPI or Uvicorn, might not yet be fully compatible with this version.
Steps to Check:
Test with Python 3.11 or 3.12: Downgrade to a stable Python version (e.g., 3.11.x) and test your application to rule out compatibility issues.
Update Dependencies: Ensure all dependencies are up to date:
pip install --upgrade fastapi uvicorn pydantic
- Frontend (Angular) Configuration
Angular's HTTP client or WebSocket implementation might not be correctly configured to handle real-time connections.
Steps to Check:
CORS Configuration: Ensure your FastAPI backend allows CORS requests from your Angular frontend:
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=["http://your-angular-app.com"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
WebSocket Implementation: Verify that your Angular application is correctly initiating WebSocket connections. Example:
const socket = new WebSocket('ws://your-backend-url/ws');
socket.onmessage = (event) => {
console.log(event.data);
};
- Network and Firewall Issues
Network configurations or firewalls might block WebSocket or long-polling connections.
Steps to Check:
Firewall Rules: Ensure that ports used by Uvicorn (e.g., 8000) are open and accessible.
Inspect Network Traffic: Use browser developer tools or tools like Wireshark to inspect network traffic and identify where the connection is being blocked.
- Debugging Tools
Browser Developer Tools: Check the "Network" tab in your browser's developer tools for more details about the "pending" requests. Look for:
- HTTP status codes (e.g., 403, 500).
- Errors in the WebSocket handshake.
- Uvicorn Logs: Run Uvicorn with verbose logging to capture detailed error messages:
uvicorn app:app --host 0.0.0.0 --port 8000 --log-level debug
Espero que estos consejos ayuden a resolver el problema. Si necesitas más asistencia, estoy a tu disposición.
Saludos,
Jonathan.
----------*
Tu opinión es muy importante para nosotros! Si esta respuesta resolvió tu consulta, por favor haz clic en 'SÍ'. Esto nos ayuda a mejorar continuamente la calidad y relevancia de nuestras soluciones.