Accept a WebSocket in Starlette or FastAPI
Answer: Starlette includes a WebSocket class that allows sending and receiving data on a websocket. You can use WebSockets with FastAPI.
Starlette's WebSocket class
The class fulfils a similar role to the HTTP request, but for a websocket. The signature is WebSocket(scope, receive, send). The page covers accepting the connection, sending data, receiving data, iterating data, closing the connection, and a send denial response.
FastAPI
You can use WebSockets with FastAPI. The FastAPI page tells you to add the websockets package, a Python library that makes it easy to use the "WebSocket" protocol. In production, a frontend such as React, Vue.js, or Angular often talks to the WebSocket backend, or a native mobile application may communicate with it directly.
| Starlette | FastAPI | |---|---| | WebSocket | Class for sending and receiving data | | WebSocket(scope, receive, send) | Signature in Starlette | | accept the connection | Step before sending or receiving data | | websockets package | Python library used with FastAPI |
See also
Claims
- id: C1 claim: Starlette's WebSocket class allows sending and receiving data on a websocket. source: S1 evidence: "allows sending and receiving data on a websocket"
- id: C2 claim: The WebSocket constructor takes scope, receive, and send. source: S1 evidence: "WebSocket(scope, receive, send)"
- id: C3 claim: FastAPI can use the WebSocket protocol in an application. source: S2 evidence: "You can use WebSockets with FastAPI"
Sources
- [S1] WebSockets. Starlette. https://starlette.dev/websockets/ . License: BSD-3-Clause. Accessed 2026-11-19.
- [S2] WebSockets. FastAPI. https://fastapi.tiangolo.com/advanced/websockets/ . License: MIT. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Starlette, WebSockets; FastAPI, WebSockets. BSD-3-Clause; MIT. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Starlette, WebSockets; FastAPI, WebSockets. BSD-3-Clause; MIT.
Cite this
iinventedlight. 2026. "Accept a WebSocket in Starlette or FastAPI." iinventedlight Library, iil:note:mp9w3jep, version of 2026-11-19. https://iinventedlight.com/library/notes/starlette-websockets/. License: CC-BY-4.0.
@misc{iil_note_mp9w3jep,
author = {{iinventedlight}},
title = {{Accept a WebSocket in Starlette or FastAPI}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/starlette-websockets/},
note = {Permanent ID iil:note:mp9w3jep. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- starlette-websockets.table.json (table; CC-BY-4.0)
- starlette-websockets.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/starlette-websockets.md.