Skip to content

Commit

Permalink
Document iter methods for WebSockets (encode#2011)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex authored and Viicos committed Feb 16, 2023
1 parent 251b53e commit 4c5a8d8
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/websockets.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,29 @@ May raise `starlette.websockets.WebSocketDisconnect()`.
JSON messages default to being received over text data frames, from version 0.10.0 onwards.
Use `websocket.receive_json(data, mode="binary")` to receive JSON over binary data frames.

### Iterating data

* `websocket.iter_text()`
* `websocket.iter_bytes()`
* `websocket.iter_json()`

Similar to `receive_text`, `receive_bytes`, and `receive_json` but returns an
async iterator.

```python hl_lines="7-8"
from starlette.websockets import WebSocket


async def app(scope, receive, send):
websocket = WebSocket(scope=scope, receive=receive, send=send)
await websocket.accept()
async for message in websocket.iter_text():
await websocket.send_text(f"Message text was: {message}")
await websocket.close()
```

When `starlette.websockets.WebSocketDisconnect` is raised, the iterator will exit.

### Closing the connection

* `await websocket.close(code=1000, reason=None)`
Expand Down

0 comments on commit 4c5a8d8

Please sign in to comment.