Overview

Uvicorn 0.27, released on January 22, 2024, implements the ASGI WebSocket 2.4 specification with close reason support.

Main Features

ASGI WebSocket 2.4

The WebSocket 2.4 spec adds a reason field to close events, enabling more precise communication between client and server during disconnection.

python
# ASGI WebSocket application with close reason
async def app(scope, receive, send):
    if scope['type'] == 'websocket':
        await send({'type': 'websocket.accept'})
        msg = await receive()
        await send({
            'type': 'websocket.close',
            'code': 1000,
            'reason': 'Processing complete',  # new
        })

Sources