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.
WebSocket(scope, receive, send)
              Bases: HTTPConnection
| PARAMETER | DESCRIPTION | 
|---|---|
| scope | 
                  
                    TYPE:
                       | 
| receive | 
                  
                    TYPE:
                       | 
| send | 
                  
                    TYPE:
                       | 
Source code in starlette/websockets.py
                    | 26 27 28 29 30 31 32 |  | 
url_for(name, /, **path_params)
| PARAMETER | DESCRIPTION | 
|---|---|
| name | 
                  
                    TYPE:
                       | 
| **path_params | 
                  
                    TYPE:
                       | 
Source code in starlette/requests.py
              | 185 186 187 188 |  | 
async
  
¶
receive()
Receive ASGI websocket messages, ensuring valid state transitions.
Source code in starlette/websockets.py
              | 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 61 62 |  | 
async
  
¶
send(message)
Send ASGI websocket messages, ensuring valid state transitions.
| PARAMETER | DESCRIPTION | 
|---|---|
| message | 
                  
                    TYPE:
                       | 
Source code in starlette/websockets.py
              | 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |  | 
async
  
¶
accept(subprotocol=None, headers=None)
| PARAMETER | DESCRIPTION | 
|---|---|
| subprotocol | 
                  
                    TYPE:
                       | 
| headers | 
                  
                    TYPE:
                       | 
Source code in starlette/websockets.py
              | 114 115 116 117 118 119 120 121 122 123 124 125 126 |  | 
async
  
¶
receive_text()
Source code in starlette/websockets.py
              | 132 133 134 135 136 137 138 139 |  | 
async
  
¶
receive_bytes()
Source code in starlette/websockets.py
              | 141 142 143 144 145 146 147 148 |  | 
async
  
¶
receive_json(mode='text')
| PARAMETER | DESCRIPTION | 
|---|---|
| mode | 
                  
                    TYPE:
                       | 
Source code in starlette/websockets.py
              | 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |  | 
async
  
¶
iter_text()
Source code in starlette/websockets.py
              | 166 167 168 169 170 171 |  | 
async
  
¶
iter_bytes()
Source code in starlette/websockets.py
              | 173 174 175 176 177 178 |  | 
async
  
¶
iter_json()
Source code in starlette/websockets.py
              | 180 181 182 183 184 185 |  | 
async
  
¶
send_text(data)
| PARAMETER | DESCRIPTION | 
|---|---|
| data | 
                  
                    TYPE:
                       | 
Source code in starlette/websockets.py
              | 187 188 |  | 
async
  
¶
send_bytes(data)
| PARAMETER | DESCRIPTION | 
|---|---|
| data | 
                  
                    TYPE:
                       | 
Source code in starlette/websockets.py
              | 190 191 |  | 
async
  
¶
send_json(data, mode='text')
| PARAMETER | DESCRIPTION | 
|---|---|
| data | 
                  
                    TYPE:
                       | 
| mode | 
                  
                    TYPE:
                       | 
Source code in starlette/websockets.py
              | 193 194 195 196 197 198 199 200 |  | 
async
  
¶
close(code=1000, reason=None)
| PARAMETER | DESCRIPTION | 
|---|---|
| code | 
                  
                    TYPE:
                       | 
| reason | 
                  
                    TYPE:
                       | 
Source code in starlette/websockets.py
              | 202 203 204 205 |  | 
When a client disconnects, a WebSocketDisconnect exception is raised, you can catch it.
You can import it directly form fastapi:
from fastapi import WebSocketDisconnect
WebSocketDisconnect(code=1000, reason=None)
              Bases: Exception
| PARAMETER | DESCRIPTION | 
|---|---|
| code | 
                  
                    TYPE:
                       | 
| reason | 
                  
                    TYPE:
                       | 
Source code in starlette/websockets.py
                    | 20 21 22 |  | 
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
WebSocketDisconnect(code=1000, reason=None)
              Bases: Exception
| PARAMETER | DESCRIPTION | 
|---|---|
| code | 
                  
                    TYPE:
                       | 
| reason | 
                  
                    TYPE:
                       | 
Source code in starlette/websockets.py
                    | 20 21 22 |  |