Skip to content

WebSocket scope and messages

Use these mappings for a WebSocket scope and the events exchanged during its connection, data transfer, close, or HTTP response phase.

WebSocketScope

Bases: TypedDict

A WebSocket scope in Scope.

typeinstance-attribute

type: Literal['websocket']

asgiinstance-attribute

http_versioninstance-attribute

http_version: Literal['1.1', '2']

schemeinstance-attribute

scheme: str

pathinstance-attribute

path: str

raw_pathinstance-attribute

raw_path: bytes

query_stringinstance-attribute

query_string: bytes

root_pathinstance-attribute

root_path: NotRequired[str]

headersinstance-attribute

headers: ScopeHeaders

clientinstance-attribute

client: NotRequired[tuple[str, int]]

serverinstance-attribute

server: tuple[str, int | None]

subprotocolsinstance-attribute

subprotocols: list[str]

stateinstance-attribute

state: NotRequired[State]

extensionsinstance-attribute

extensions: WebSocketExtensions

WebSocketConnect

Bases: TypedDict

A WebSocket connect event in ReceiveMessage.

typeinstance-attribute

type: Literal['websocket.connect']

WebSocketReceiveBytes

Bases: TypedDict

A binary WebSocket event in ReceiveMessage.

typeinstance-attribute

type: Literal['websocket.receive']

bytesinstance-attribute

bytes: bytes

textinstance-attribute

text: NotRequired[None]

WebSocketReceiveText

Bases: TypedDict

A text WebSocket event in ReceiveMessage.

typeinstance-attribute

type: Literal['websocket.receive']

textinstance-attribute

text: str

bytesinstance-attribute

bytes: NotRequired[None]

WebSocketDisconnect

Bases: TypedDict

A WebSocket disconnect event in ReceiveMessage.

typeinstance-attribute

type: Literal['websocket.disconnect']

codeinstance-attribute

code: int

reasoninstance-attribute

reason: NotRequired[str]

WebSocketAccept

Bases: TypedDict

A WebSocket accept event in SendMessage.

typeinstance-attribute

type: Literal['websocket.accept']

subprotocolinstance-attribute

subprotocol: NotRequired[str | None]

headersinstance-attribute

headers: NotRequired[Headers]

WebSocketSendBytes

Bases: TypedDict

A binary WebSocket event in SendMessage.

typeinstance-attribute

type: Literal['websocket.send']

bytesinstance-attribute

bytes: bytes

textinstance-attribute

text: NotRequired[None]

WebSocketSendText

Bases: TypedDict

A text WebSocket event in SendMessage.

typeinstance-attribute

type: Literal['websocket.send']

textinstance-attribute

text: str

bytesinstance-attribute

bytes: NotRequired[None]

WebSocketClose

Bases: TypedDict

A WebSocket close event in SendMessage.

typeinstance-attribute

type: Literal['websocket.close']

codeinstance-attribute

code: NotRequired[int]

reasoninstance-attribute

reason: NotRequired[str | None]

WebSocketHTTPResponseStart

Bases: TypedDict

A WebSocket HTTP response-start event in SendMessage.

typeinstance-attribute

type: Literal['websocket.http.response.start']

statusinstance-attribute

status: int

headersinstance-attribute

headers: NotRequired[Headers]

WebSocketHTTPResponseBody

Bases: TypedDict

A WebSocket HTTP response-body event in SendMessage.

typeinstance-attribute

type: Literal['websocket.http.response.body']

bodyinstance-attribute

body: NotRequired[bytes]

more_bodyinstance-attribute

more_body: NotRequired[bool]