- WebSockets
- fastapi.WebSocket
- scope
instance-attribute
- app
property
- url
property
- base_url
property
- headers
property
- query_params
property
- path_params
property
- cookies
property
- client
property
- state
property
- client_state
instance-attribute
- application_state
instance-attribute
- url_for
- receive
async
- send
async
- accept
async
- receive_text
async
- receive_bytes
async
- receive_json
async
- iter_text
async
- iter_bytes
async
- iter_json
async
- send_text
async
- send_bytes
async
- send_json
async
- close
async
- scope
- fastapi.WebSocketDisconnect
- WebSockets - additional classes
- fastapi.websockets.WebSocketDisconnect
- fastapi.websockets.WebSocketState
- fastapi.WebSocket
WebSockets
When defining WebSockets, you normally declare a parameter of type WebSocket
and with it you can read data from the client and send data to it.
It is provided directly by Starlette, but you can import it from fastapi
:
from fastapi import WebSocket
Tip
When you want to define dependencies that should be compatible with both HTTP and WebSockets, you can define a parameter that takes an HTTPConnection
instead of a Request
or a WebSocket
.
fastapi.WebSocket
WebSocket(scope, receive, send)
Bases: [HTTPConnection](https:/fastapi.tiangolo.com/zh/reference/httpconnection/#fastapi.requests.HTTPConnection "starlette.requests.HTTPConnection")
PARAMETER | DESCRIPTION |
---|---|
scope | TYPE: |
receive | TYPE: |
send | TYPE: |
Source code in starlette/websockets.py
|
|
scope instance-attribute
scope = scope
app property
app
url property
url
base_url property
base_url
headers property
headers
query_params property
query_params
path_params property
path_params
cookies property
cookies
client property
client
state property
state
client_state instance-attribute
client_state = CONNECTING
application_state instance-attribute
application_state = CONNECTING
url_for
url_for(__name, **path_params)
PARAMETER | DESCRIPTION |
---|---|
__name | TYPE: |
**path_params | TYPE: |
Source code in starlette/requests.py
|
|
receive async
receive()
Receive ASGI websocket messages, ensuring valid state transitions.
Source code in starlette/websockets.py
|
|
send async
send(message)
Send ASGI websocket messages, ensuring valid state transitions.
PARAMETER | DESCRIPTION |
---|---|
message | TYPE: |
Source code in starlette/websockets.py
|
|
accept async
accept(subprotocol=None, headers=None)
PARAMETER | DESCRIPTION |
---|---|
subprotocol | TYPE: |
headers | TYPE: |
Source code in starlette/websockets.py
|
|
receive_text async
receive_text()
Source code in starlette/websockets.py
|
|
receive_bytes async
receive_bytes()
Source code in starlette/websockets.py
|
|
receive_json async
receive_json(mode='text')
PARAMETER | DESCRIPTION |
---|---|
mode | TYPE: |
Source code in starlette/websockets.py
|
|
iter_text async
iter_text()
Source code in starlette/websockets.py
|
|
iter_bytes async
iter_bytes()
Source code in starlette/websockets.py
|
|
iter_json async
iter_json()
Source code in starlette/websockets.py
|
|
send_text async
send_text(data)
PARAMETER | DESCRIPTION |
---|---|
data | TYPE: |
Source code in starlette/websockets.py
|
|
send_bytes async
send_bytes(data)
PARAMETER | DESCRIPTION |
---|---|
data | TYPE: |
Source code in starlette/websockets.py
|
|
send_json async
send_json(data, mode='text')
PARAMETER | DESCRIPTION |
---|---|
data | TYPE: |
mode | TYPE: |
Source code in starlette/websockets.py
|
|
close async
close(code=1000, reason=None)
PARAMETER | DESCRIPTION |
---|---|
code | TYPE: |
reason | TYPE: |
Source code in starlette/websockets.py
|
|
When a client disconnects, a WebSocketDisconnect
exception is raised, you can catch it.
You can import it directly form fastapi
:
from fastapi import WebSocketDisconnect
fastapi.WebSocketDisconnect
WebSocketDisconnect(code=1000, reason=None)
Bases: Exception
PARAMETER | DESCRIPTION |
---|---|
code | TYPE: |
reason | TYPE: |
Source code in starlette/websockets.py
|
|
code instance-attribute
code = code
reason instance-attribute
reason = reason or ''
WebSockets - additional classes
Additional classes for handling WebSockets.
Provided directly by Starlette, but you can import it from fastapi
:
from fastapi.websockets import WebSocketDisconnect, WebSocketState
fastapi.websockets.WebSocketDisconnect
WebSocketDisconnect(code=1000, reason=None)
Bases: Exception
PARAMETER | DESCRIPTION |
---|---|
code | TYPE: |
reason | TYPE: |
Source code in starlette/websockets.py
|
|
code instance-attribute
code = code
reason instance-attribute
reason = reason or ''
fastapi.websockets.WebSocketState
Bases: Enum
CONNECTING class-attribute
instance-attribute
CONNECTING = 0
CONNECTED class-attribute
instance-attribute
CONNECTED = 1
DISCONNECTED class-attribute
instance-attribute
DISCONNECTED = 2