← All notes

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

Sources

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

Permanent ID: iil:note:mp9w3jep

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.},
}

Markdown

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.

All artifact files (JSON index)

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.