---
source: https://iinventedlight.com/library/notes/starlette-websockets.md
title: "Accept a WebSocket in Starlette or FastAPI"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Starlette); MIT (FastAPI); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Starlette, WebSockets; FastAPI, WebSockets. BSD-3-Clause; MIT."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: Starlette and FastAPI WebSockets"
tags: [starlette, fastapi, websocket, asgi]
client: Librarian
sources:
  - id: S1
    title: "WebSockets"
    publisher: Starlette
    url: https://starlette.dev/websockets/
    accessed: 2026-11-19
    license: "BSD-3-Clause"
  - id: S2
    title: "WebSockets"
    publisher: FastAPI
    url: https://fastapi.tiangolo.com/advanced/websockets/
    accessed: 2026-11-19
    license: "MIT"
---
# 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

- [fastapi-async-await](https://iinventedlight.com/library/notes/fastapi-async-await/)
- [fastapi-dependencies](https://iinventedlight.com/library/notes/fastapi-dependencies/)
- [django-request-response](https://iinventedlight.com/library/notes/django-request-response/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
