Requests Archives - Mangum ASGI Blog Thu, 04 Jul 2024 07:16:19 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://mangum.io/wp-content/uploads/2024/07/cropped-folder-5000783_640-32x32.png Requests Archives - Mangum 32 32 ASGI in Action: Building a Chat Application https://mangum.io/asgi-in-action-building-a-chat-application/ Mon, 26 Feb 2024 07:12:00 +0000 https://mangum.io/?p=101 ASGI (Asynchronous Server Gateway Interface) has transformed Python web development by enabling real-time applications with high concurrency and asynchronous capabilities.

The post ASGI in Action: Building a Chat Application appeared first on Mangum.

]]>
ASGI (Asynchronous Server Gateway Interface) has transformed Python web development by enabling real-time applications with high concurrency and asynchronous capabilities. In this article, we’ll explore how to leverage ASGI to build a chat application, demonstrating its features and functionality. We’ll also integrate the FastAPI framework and utilize its startup event for application initialization.

Understanding ASGI for Real-Time Applications

ASGI supports bidirectional communication channels like WebSockets, making it ideal for building interactive and real-time applications such as chat apps. Unlike traditional synchronous servers, ASGI handles multiple connections concurrently, ensuring responsiveness and scalability.

Key Components for a Chat Application:

  • WebSockets: Establish persistent connections between clients and servers for real-time messaging.
  • Async Framework: Utilize frameworks like FastAPI or Starlette to handle asynchronous operations and WebSocket communication.

Building a Chat Application with FastAPI and ASGI

Setting Up FastAPI with WebSocket Support

FastAPI provides WebSocket support through ASGI, allowing bidirectional communication for real-time applications like chat.

from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from starlette.middleware.cors import CORSMiddleware
from typing import List

app = FastAPI()

# CORS (Cross-Origin Resource Sharing) middleware for WebSocket connections
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# In-memory storage for WebSocket connections
class ConnectionManager:
    def __init__(self):
        self.active_connections: List[WebSocket] = []

    async def connect(self, websocket: WebSocket):
        await websocket.accept()
        self.active_connections.append(websocket)

    def disconnect(self, websocket: WebSocket):
        self.active_connections.remove(websocket)

    async def broadcast_message(self, message: str):
        for connection in self.active_connections:
            await connection.send_text(message)

manager = ConnectionManager()

# WebSocket route for handling connections
@app.websocket("/chat/{client_id}")
async def websocket_endpoint(websocket: WebSocket, client_id: str):
    await manager.connect(websocket)
    try:
        while True:
            message = await websocket.receive_text()
            await manager.broadcast_message(f"Client {client_id}: {message}")
    except WebSocketDisconnect:
        manager.disconnect(websocket)
        await manager.broadcast_message(f"Client {client_id} left the chat")

Explanation:

  • WebSocket Endpoint (websocket_endpoint): Defines a WebSocket route /chat/{client_id} using FastAPI’s websocket decorator.
  • Connection Manager (ConnectionManager): Manages WebSocket connections, handling connection, disconnection, and message broadcasting.
  • CORSMiddleware: Enables cross-origin requests for WebSocket connections, allowing communication from different domains.

Integrating FastAPI Startup Event

Use FastAPI’s startup event to perform initialization tasks, such as connecting to databases or initializing external services.

from fastapi import FastAPI

app = FastAPI()

# Example startup event
@app.on_event("startup")
async def startup_event():
print("Application is starting up...")
# Perform initialization tasks here, e.g., connect to database

@app.get("/")
async def read_root():
return {"message": "Hello, ASGI Chat Application!"}

Explanation:

  • Startup Event (startup_event): Executes initialization tasks asynchronously when the FastAPI application starts.
  • Initialization Tasks: Connect to databases, set up external services, or load configurations during application startup.

ASGI, combined with frameworks like FastAPI, empowers developers to build sophisticated real-time applications such as chat apps with ease. By leveraging WebSocket communication and asynchronous capabilities, ASGI ensures responsiveness, scalability, and enhanced user experience. Integrating FastAPI’s startup event allows for seamless initialization of application resources, ensuring smooth operation and reliability. As Python’s web development landscape evolves, ASGI continues to be a pivotal technology for building modern, interactive web applications that meet the demands of today’s users and businesses.

The post ASGI in Action: Building a Chat Application appeared first on Mangum.

]]>
Monitoring and Logging in ASGI Applications https://mangum.io/monitoring-and-logging-in-asgi-applications/ Mon, 19 Feb 2024 07:02:00 +0000 https://mangum.io/?p=93 Monitoring and logging are essential practices for maintaining the health, performance, and security of ASGI (Asynchronous Server Gateway Interface) applications.

The post Monitoring and Logging in ASGI Applications appeared first on Mangum.

]]>
Monitoring and logging are essential practices for maintaining the health, performance, and security of ASGI (Asynchronous Server Gateway Interface) applications. ASGI, known for its asynchronous capabilities and support for real-time communication, requires specific monitoring and logging strategies to effectively manage and troubleshoot applications. This article explores best practices and tools for monitoring and logging in Python ASGI applications.

Why Monitor and Log ASGI Applications?

ASGI applications handle multiple concurrent connections and asynchronous tasks, making real-time monitoring crucial for detecting performance issues, errors, and security threats promptly. Effective logging provides visibility into application behavior, facilitating debugging and ensuring compliance with operational standards.

Key Objectives of Monitoring and Logging:

  • Performance Optimization: Identify bottlenecks and optimize application performance under varying loads.
  • Error Detection: Monitor for exceptions, failures, and unexpected behaviors to preemptively address issues.
  • Security Monitoring: Detect and respond to potential security threats, ensuring data integrity and user safety.

Monitoring ASGI Applications

Metrics to Monitor:

  1. Request Throughput: Measure the rate of incoming requests to gauge application load and scalability requirements.
  2. Response Times: Monitor latency, the time taken for requests to receive responses, to ensure optimal user experience.
  3. Resource Utilization: Track CPU usage, memory consumption, and disk I/O to optimize resource allocation and performance.

Tools for Monitoring:

  • Prometheus: Collect metrics and monitor ASGI applications using Prometheus exporters and Grafana dashboards for visualization.
  • AWS CloudWatch: Monitor AWS-hosted ASGI applications, capture logs, and set alarms for performance anomalies.

Logging in ASGI Applications

Types of Logs:

  1. Application Logs: Capture events such as requests, responses, and application-specific activities for troubleshooting and auditing.
  2. Error Logs: Record exceptions, stack traces, and error messages to diagnose and resolve issues promptly.

Logging Best Practices:

  • Structured Logging: Use structured formats (e.g., JSON) for logs to facilitate parsing, analysis, and integration with monitoring tools.
  • Log Levels: Define appropriate log levels (e.g., DEBUG, INFO, ERROR) to filter and prioritize log messages based on severity.

Tools for Logging:

  • Python Logging Module: Built-in module for flexible logging configurations, including log levels, handlers, and formatters.
  • ELK Stack (Elasticsearch, Logstash, Kibana): Centralized logging solution for collecting, analyzing, and visualizing logs from ASGI applications.

Example Implementation in Python ASGI

Setting Up Logging in a FastAPI Application:

from fastapi import FastAPI, Request
import logging

app = FastAPI()

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)

@app.middleware("http")
async def log_requests(request: Request, call_next):
    # Log incoming requests
    logging.info(f"Incoming request: {request.method} {request.url}")
    response = await call_next(request)
    # Log response status
    logging.info(f"Response status: {response.status_code}")
    return response

@app.get("/")
async def read_root():
    return {"message": "Hello, ASGI Monitoring and Logging!"}

Explanation:

  • Middleware (log_requests): Logs incoming requests and response statuses using Python’s logging module.
  • Configuration (basicConfig): Configures logging level, format, and output stream (e.g., console, files) for ASGI application.

Monitoring and logging are indispensable practices for maintaining the performance, reliability, and security of ASGI applications. By implementing effective monitoring tools and logging strategies, Python ASGI developers can gain insights into application behavior, optimize performance, and respond proactively to errors and security threats. Whether using built-in Python modules or advanced monitoring platforms, prioritizing monitoring and logging ensures ASGI applications operate seamlessly and meet operational objectives effectively.

The post Monitoring and Logging in ASGI Applications appeared first on Mangum.

]]>
Advanced ASGI: Custom Middleware and Extensions https://mangum.io/advanced-asgi-custom-middleware-and-extensions/ Sun, 18 Feb 2024 07:07:00 +0000 https://mangum.io/?p=97 One of the powerful features of ASGI is the ability to create custom middleware and extensions, enhancing application functionality and customization.

The post Advanced ASGI: Custom Middleware and Extensions appeared first on Mangum.

]]>
One of the powerful features of ASGI is the ability to create custom middleware and extensions, enhancing application functionality and customization. This article explores how to leverage custom middleware and extensions in ASGI applications, including their implementation and benefits, with a focus on the broader ecosystem of ASGI web servers.

Understanding Custom Middleware in ASGI

Middleware in ASGI intercepts requests and responses, allowing developers to inject custom logic or modify data during request processing. Custom middleware is essential for cross-cutting concerns such as authentication, logging, error handling, and performance monitoring.

Benefits of Custom Middleware:

  • Modular Architecture: Enhances code reusability and maintainability by separating concerns into independent middleware components.
  • Customization: Tailors application behavior to specific requirements without modifying core application logic.
  • Performance Optimization: Improves application performance by implementing asynchronous operations and non-blocking I/O in middleware functions.

Implementing Custom Middleware in ASGI

Example: Logging Middleware with Starlette

from starlette.applications import Starlette
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.middleware import Middleware
from starlette.middleware.trustedhost import TrustedHostMiddleware
import logging

logging.basicConfig(level=logging.INFO)

class LoggingMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request, call_next):
        logging.info(f"Incoming request: {request.method} {request.url}")
        response = await call_next(request)
        logging.info(f"Outgoing response: {response.status_code}")
        return response

app = Starlette(
    middleware=[
        Middleware(LoggingMiddleware),
        Middleware(TrustedHostMiddleware, allowed_hosts=['example.com'])
    ]
)

@app.route("/")
async def homepage(request):
    return {"message": "Hello, Custom Middleware in ASGI!"}

Explanation:

  • LoggingMiddleware: Custom middleware class that logs incoming requests and outgoing responses using Python’s logging module.
  • Middleware Registration: Registers LoggingMiddleware and TrustedHostMiddleware with Starlette application, demonstrating how to chain multiple middleware components.

Developing Extensions for ASGI Applications

Extensions in ASGI provide additional functionality or integration with external services, enhancing application capabilities beyond standard framework features. Examples include database connectors, authentication providers, and third-party service integrations.

Benefits of ASGI Extensions:

  • Enhanced Functionality: Extends ASGI framework capabilities with pre-built components for common tasks.
  • Community Support: Leverages community-developed extensions to streamline development and integration efforts.
  • Ecosystem Compatibility: Ensures compatibility with ASGI web servers and frameworks, facilitating seamless integration into existing projects.

Example of Using ASGI Extensions

Integrating Database Extension with FastAPI

from fastapi import FastAPI
from databases import Database

app = FastAPI()

# Initialize database extension
database = Database("sqlite:///./example.db")

@app.on_event("startup")
async def startup():
    # Connect to database on application startup
    await database.connect()

@app.on_event("shutdown")
async def shutdown():
    # Disconnect from database on application shutdown
    await database.disconnect()

@app.get("/")
async def read_root():
    # Query database example
    query = "SELECT * FROM users"
    results = await database.fetch_all(query)
    return {"message": "Hello, ASGI Extensions!", "data": results}

Explanation:

  • Database Extension (databases): Integrates asynchronous database operations with FastAPI using an ASGI-compatible database connector.
  • Startup and Shutdown Events: Utilizes FastAPI’s event handlers (on_event) to manage database connections during application lifecycle.

Custom middleware and extensions play a crucial role in extending the functionality and enhancing customization options for ASGI applications. By leveraging custom middleware, developers can implement cross-cutting concerns such as logging, authentication, and performance monitoring efficiently. Additionally, integrating extensions allows for seamless integration with external services and enhanced capabilities beyond standard framework features. As the ASGI ecosystem continues to evolve, custom middleware and extensions provide powerful tools for building scalable, performant, and feature-rich web applications in Python.

The post Advanced ASGI: Custom Middleware and Extensions appeared first on Mangum.

]]>
Understanding the ASGI Lifespan Protocol https://mangum.io/understanding-the-asgi-lifespan-protocol/ Fri, 09 Feb 2024 06:59:00 +0000 https://mangum.io/?p=87 The ASGI (Asynchronous Server Gateway Interface) Lifespan Protocol defines a standard for managing the lifecycle of ASGI applications, handling events such as startup and shutdown.

The post Understanding the ASGI Lifespan Protocol appeared first on Mangum.

]]>
The ASGI (Asynchronous Server Gateway Interface) Lifespan Protocol defines a standard for managing the lifecycle of ASGI applications, handling events such as startup and shutdown. This protocol ensures consistent initialization and cleanup of resources across ASGI servers and frameworks. This article delves into the details of the ASGI Lifespan Protocol, its significance, and how it integrates with frameworks like Starlette.

What is the ASGI Lifespan Protocol?

ASGI introduced the Lifespan Protocol to manage application lifecycle events asynchronously. Unlike traditional synchronous servers handled by WSGI, ASGI’s asynchronous nature allows it to handle multiple connections simultaneously and efficiently manage resources like database connections or caches.

Key Components of ASGI Lifespan Protocol:

  • Startup Event: Triggered when the ASGI application starts, allowing initialization tasks such as connecting to databases or loading configurations.
  • Shutdown Event: Triggered when the ASGI application is shutting down, enabling cleanup tasks like closing database connections or releasing resources.

Why ASGI Lifespan Protocol Matters?

The ASGI Lifespan Protocol ensures that ASGI applications are properly initialized and terminated, maintaining application integrity and resource efficiency. It enables frameworks and servers to handle startup and shutdown events consistently, regardless of the underlying server implementation.

Implementing ASGI Lifespan Protocol with Starlette

Starlette is a lightweight ASGI framework for building asynchronous web applications in Python. It integrates seamlessly with the ASGI Lifespan Protocol to manage application lifecycle events.

Example of ASGI Lifespan Events with Starlette

from starlette.applications import Starlette
from starlette.routing import Route

app = Starlette()

async def startup():
    # Perform startup tasks such as database connection
    print("Starting up Starlette application...")

async def shutdown():
    # Perform shutdown tasks such as closing database connection
    print("Shutting down Starlette application...")

# Register startup and shutdown events
app.add_event_handler("startup", startup)
app.add_event_handler("shutdown", shutdown)

@app.route("/")
async def homepage(request):
    # Handle Starlette request
    return {"message": "Hello, ASGI Lifespan Protocol!"}

Explanation:

  • Startup Function (startup()): Executes initialization tasks when the ASGI application starts. This may include setting up database connections, loading configurations, or initializing caches.
  • Shutdown Function (shutdown()): Executes cleanup tasks when the ASGI application is shutting down. This ensures resources are released gracefully, preventing resource leaks or data corruption.
  • Integration with Starlette: Starlette’s add_event_handler method registers functions to handle startup and shutdown events, ensuring proper management of application lifecycle.

Advantages of ASGI Lifespan Protocol

  • Resource Management: Ensures efficient use of resources by managing their lifecycle appropriately.
  • Consistency: Provides a standardized way to handle startup and shutdown events across ASGI applications and frameworks.
  • Enhanced Reliability: Reduces the risk of resource leaks or data corruption by ensuring proper initialization and cleanup procedures.

The ASGI Lifespan Protocol plays a vital role in managing the lifecycle of ASGI applications, ensuring they start up and shut down gracefully while managing resources effectively. Frameworks like Starlette leverage this protocol to handle initialization and cleanup tasks asynchronously, enhancing application reliability and performance. Understanding and implementing the ASGI Lifespan Protocol allows developers to build robust, scalable, and efficient ASGI applications that meet modern web development demands effectively.

The post Understanding the ASGI Lifespan Protocol appeared first on Mangum.

]]>