diff --git a/docs/en/docs/advanced/websockets.md b/docs/en/docs/advanced/websockets.md index d473cef07..6cacc2e6f 100644 --- a/docs/en/docs/advanced/websockets.md +++ b/docs/en/docs/advanced/websockets.md @@ -51,38 +51,7 @@ In your WebSocket route you can `await` for messages and send messages. You can receive and send binary, text, and JSON data. -## Using `Depends` and others - -In WebSocket endpoints you can import from `fastapi` and use: - -* `Depends` -* `Security` -* `Cookie` -* `Header` -* `Path` -* `Query` - -They work the same way as for other FastAPI endpoints/*path operations*: - -```Python hl_lines="53 54 55 56 57 58 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76" -{!../../../docs_src/websockets/tutorial002.py!} -``` - -!!! info - In a WebSocket it doesn't really make sense to raise an `HTTPException`. So it's better to close the WebSocket connection directly. - - You can use a closing code from the valid codes defined in the specification. - - In the future, there will be a `WebSocketException` that you will be able to `raise` from anywhere, and add exception handlers for it. It depends on the PR #527 in Starlette. - -## More info - -To learn more about the options, check Starlette's documentation for: - -* The `WebSocket` class. -* Class-based WebSocket handling. - -## Test it +## Try it If your file is named `main.py`, run your application with: @@ -115,3 +84,62 @@ You can send (and receive) many messages: And all of them will use the same WebSocket connection. + +## Using `Depends` and others + +In WebSocket endpoints you can import from `fastapi` and use: + +* `Depends` +* `Security` +* `Cookie` +* `Header` +* `Path` +* `Query` + +They work the same way as for other FastAPI endpoints/*path operations*: + +```Python hl_lines="56 57 58 59 60 61 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79" +{!../../../docs_src/websockets/tutorial002.py!} +``` + +!!! info + In a WebSocket it doesn't really make sense to raise an `HTTPException`. So it's better to close the WebSocket connection directly. + + You can use a closing code from the valid codes defined in the specification. + + In the future, there will be a `WebSocketException` that you will be able to `raise` from anywhere, and add exception handlers for it. It depends on the PR #527 in Starlette. + +### Try the WebSockets with dependencies + +If your file is named `main.py`, run your application with: + +
+ +```console +$ uvicorn main:app --reload + +INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) +``` + +
+ +Open your browser at http://127.0.0.1:8000. + +There you can set: + +* The "Item ID", used in the path. +* The "Token" used as a query parameter. + +!!! tip + Notice that the query `token` will be handled by a dependency. + +With that you can connect the WebSocket and then send and receive messages: + + + +## More info + +To learn more about the options, check Starlette's documentation for: + +* The `WebSocket` class. +* Class-based WebSocket handling. diff --git a/docs/en/docs/img/tutorial/websockets/image05.png b/docs/en/docs/img/tutorial/websockets/image05.png new file mode 100644 index 000000000..00b0c5220 Binary files /dev/null and b/docs/en/docs/img/tutorial/websockets/image05.png differ diff --git a/docs_src/websockets/tutorial002.py b/docs_src/websockets/tutorial002.py index 43005e54c..5be199cd6 100644 --- a/docs_src/websockets/tutorial002.py +++ b/docs_src/websockets/tutorial002.py @@ -1,4 +1,4 @@ -from fastapi import Cookie, Depends, FastAPI, Header, WebSocket, status +from fastapi import Cookie, Depends, FastAPI, Query, WebSocket, status from fastapi.responses import HTMLResponse app = FastAPI() @@ -13,8 +13,9 @@ html = """

WebSocket Chat

+ -
+
@@ -23,8 +24,9 @@ html = """