What is ASGI?
ASGI stands for Asynchronous Server Gateway Interface. It is the spiritual successor to WSGI (Web Server Gateway Interface) and is designed to support async Python web apps, including WebSockets, HTTP/2, and more.
If you’ve worked with WSGI-based frameworks like Django or Flask, you're used to synchronous request-response cycles. ASGI takes this further by enabling asynchronous, long-lived connections, such as WebSockets and background tasks.
WSGI vs ASGI – A Quick Analogy
Let’s start with a conceptual difference:
WSGI
Client --> HTTP Request --> WSGI App --> HTTP Response --> Client
- One request at a time
- Blocking
- No native WebSocket support
ASGI
Client --> HTTP or WebSocket --> ASGI App --> Streamed Response or Event Handler
- Supports both sync & async
- Handles long-lived connections
- Enables concurrent tasks (with
async def
)
ASGI Anatomy
ASGI applications are callables, like this:
async def app(scope, receive, send):
...
Each ASGI app receives:
scope
: metadata about the connection (type, headers, etc.)receive
: a coroutine to get events (like incoming HTTP body chunks or WebSocket messages)send
: a coroutine to send responses or events
ASCII Diagram of ASGI Flow
+------------+ +----------------+ +-------------------+
| Browser | <---> | ASGI Web Server| <---> | Your ASGI App |
| (Client) | | (Uvicorn, etc) | | (Nexios, FastAPI) |
+------------+ +----------------+ +-------------------+
| |
| |
+---------+ +-------------+
| |
+-----v-----+ +-----v-----+
| HTTP | | WebSocket |
| Request | | Event |
+-----------+ +-----------+
Explanation:
- The ASGI server (like Uvicorn) is responsible for translating raw sockets (TCP/HTTP/WebSocket) into Python callables.
- Your ASGI app (like Nexios) just needs to handle the
scope
,receive
, andsend
interfaces. - ASGI enables streaming responses, background jobs, and real-time features easily.
Why ASGI Matters for Nexios
Nexios is an ASGI-native framework. This means:
- You can handle WebSocket connections out of the box
- You can build highly performant, async applications
- You’re future-proofed for modern Python server tech (like HTTP/3, SSE, etc.)
Use Cases Where ASGI Shines
Use Case | WSGI Compatible | ASGI Compatible |
---|---|---|
Traditional HTTP APIs | ✅ | ✅ |
WebSockets | ❌ | ✅ |
Background Tasks | ❌ | ✅ |
Server-Sent Events (SSE) | ❌ | ✅ |
Long Polling | ⚠️ Limited | ✅ |
Summary
ASGI lets Python web apps speak multiple protocols and scale efficiently using async programming.
If WSGI is a landline phone, ASGI is a smartphone – it still makes calls (HTTP), but also streams video, chats, and pushes updates (WebSockets, tasks, etc.).