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(<span data-autorefs-optional="fastapi.WebSocket(scope)">scope</span>, <span data-autorefs-optional="fastapi.WebSocket(receive)">receive</span>, <span data-autorefs-optional="fastapi.WebSocket(send)">send</span>)
Bases: HTTPConnection
PARAMETER | DESCRIPTION |
---|---|
scope
|
TYPE:
|
receive
|
TYPE:
|
send
|
TYPE:
|
Source code in starlette/websockets.py
24 25 26 27 28 29 30 |
|
url_for
¶
url_for(<span data-autorefs-optional="fastapi.WebSocket.url_for(name)">name</span>, /, <span data-autorefs-optional="fastapi.WebSocket.url_for(**path_params)">**path_params</span>)
PARAMETER | DESCRIPTION |
---|---|
name
|
TYPE:
|
**path_params
|
TYPE:
|
Source code in starlette/requests.py
185 186 187 188 |
|
receive
async
¶
receive()
Receive ASGI websocket messages, ensuring valid state transitions.
Source code in starlette/websockets.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
send
async
¶
send(<span data-autorefs-optional="fastapi.WebSocket.send(message)">message</span>)
Send ASGI websocket messages, ensuring valid state transitions.
PARAMETER | DESCRIPTION |
---|---|
message
|
TYPE:
|
Source code in starlette/websockets.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
accept
async
¶
accept(<span data-autorefs-optional="fastapi.WebSocket.accept(subprotocol)">subprotocol</span>=None, <span data-autorefs-optional="fastapi.WebSocket.accept(headers)">headers</span>=None)
PARAMETER | DESCRIPTION |
---|---|
subprotocol
|
TYPE:
|
headers
|
TYPE:
|
Source code in starlette/websockets.py
95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
receive_text
async
¶
receive_text()
Source code in starlette/websockets.py
113 114 115 116 117 118 119 120 |
|
receive_bytes
async
¶
receive_bytes()
Source code in starlette/websockets.py
122 123 124 125 126 127 128 129 |
|
receive_json
async
¶
receive_json(<span data-autorefs-optional="fastapi.WebSocket.receive_json(mode)">mode</span>='text')
PARAMETER | DESCRIPTION |
---|---|
mode
|
TYPE:
|
Source code in starlette/websockets.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
iter_text
async
¶
iter_text()
Source code in starlette/websockets.py
147 148 149 150 151 152 |
|
iter_bytes
async
¶
iter_bytes()
Source code in starlette/websockets.py
154 155 156 157 158 159 |
|
iter_json
async
¶
iter_json()
Source code in starlette/websockets.py
161 162 163 164 165 166 |
|
send_text
async
¶
send_text(<span data-autorefs-optional="fastapi.WebSocket.send_text(data)">data</span>)
PARAMETER | DESCRIPTION |
---|---|
data
|
TYPE:
|
Source code in starlette/websockets.py
168 169 |
|
send_bytes
async
¶
send_bytes(<span data-autorefs-optional="fastapi.WebSocket.send_bytes(data)">data</span>)
PARAMETER | DESCRIPTION |
---|---|
data
|
TYPE:
|
Source code in starlette/websockets.py
171 172 |
|
send_json
async
¶
send_json(<span data-autorefs-optional="fastapi.WebSocket.send_json(data)">data</span>, <span data-autorefs-optional="fastapi.WebSocket.send_json(mode)">mode</span>='text')
PARAMETER | DESCRIPTION |
---|---|
data
|
TYPE:
|
mode
|
TYPE:
|
Source code in starlette/websockets.py
174 175 176 177 178 179 180 181 |
|
close
async
¶
close(<span data-autorefs-optional="fastapi.WebSocket.close(code)">code</span>=1000, <span data-autorefs-optional="fastapi.WebSocket.close(reason)">reason</span>=None)
PARAMETER | DESCRIPTION |
---|---|
code
|
TYPE:
|
reason
|
TYPE:
|
Source code in starlette/websockets.py
183 184 185 186 |
|
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(<span data-autorefs-optional="fastapi.WebSocketDisconnect(code)">code</span>=1000, <span data-autorefs-optional="fastapi.WebSocketDisconnect(reason)">reason</span>=None)
Bases: Exception
PARAMETER | DESCRIPTION |
---|---|
code
|
TYPE:
|
reason
|
TYPE:
|
Source code in starlette/websockets.py
18 19 20 |
|
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(<span data-autorefs-optional="fastapi.websockets.WebSocketDisconnect(code)">code</span>=1000, <span data-autorefs-optional="fastapi.websockets.WebSocketDisconnect(reason)">reason</span>=None)
Bases: Exception
PARAMETER | DESCRIPTION |
---|---|
code
|
TYPE:
|
reason
|
TYPE:
|
Source code in starlette/websockets.py
18 19 20 |
|