Skip to content

Commit 4f233f3

Browse files
committed
docs: Update all examples and add basic written guide
Resolves: #6
1 parent 3b1a0c5 commit 4f233f3

File tree

3 files changed

+129
-24
lines changed

3 files changed

+129
-24
lines changed

README.md

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,62 +14,82 @@ The usage of the library is very familiar to the experience you‘re used to in
1414
- First-class generated specification & documentation
1515
- Uses [python_socketio](https://python-socketio.readthedocs.io/en/latest/) underneath
1616
- Fully typed using pydantic, including the [AsyncAPI spec](./fastapi_sio/schemas/asyncapi.py)
17-
- Streamlined emit to clients ([learn more in docs](./docs/emitting.md))
18-
- Forces strictly to emit correct data type ([see the example](./docs/example.md))
17+
- Streamlined emit to clients ([learn more in docs](./docs/index.md#using-emitters))
18+
- Forces strictly to emit correct data type ([see the example](./examples/from_readme.py))
1919

2020
## What‘s Missing?
2121

2222
- [ ] Serve AsyncAPI studio at /sio/docs
23-
- Unfortunately, AsyncAPI studio doesn‘t work the same way as Swagger UI, there is currently no way to use CDN hosted built package and supply only single html file and URL with spec JSON
23+
- Unfortunately, AsyncAPI studio doesn‘t work the same way as Swagger UI, there is currently no way to use CDN hosted built package and supply only single html file and URL with spec JSON
2424
- [ ] Support for more obscure fields of AsyncAPI, such as `traits`, ...
2525

2626
## Usage Example
2727

2828
```python
29-
fastapi_app = FastAPI()
30-
sio_app = FastAPISIO(app=fastapi_app)
29+
from fastapi import FastAPI
30+
from pydantic import BaseModel
31+
from fastapi_sio import FastAPISIO
3132

32-
purr_channel = sio_app.create_emitter(
33+
class PurrModel(BaseModel):
34+
detail: str
35+
loudness: int
36+
37+
class BellyRubModel(BaseModel):
38+
where_exactly: str
39+
scratches_num: int
40+
41+
fastapi = FastAPI()
42+
sio = FastAPISIO(app=fastapi)
43+
44+
# Mount this ASGI app. The FastAPI app is passed as the other_asgi_app
45+
# so you'll have access to the FastAPI routes as well.
46+
app = sio.asgi_app
47+
48+
# Run with `uvicorn examples.from_readme:app`
49+
50+
@fastapi.get("/")
51+
async def example_fastapi_route():
52+
return {"message": "Welcome to the FastAPI-SIO example!"}
53+
54+
purr_channel = sio.create_emitter(
3355
"purrs",
3456
model=PurrModel,
3557
summary="Channel for purrs",
3658
description="Receive any purrs here!",
3759
)
3860

39-
@sio_app.on(
61+
@sio.on(
4062
"rubs",
4163
model=BellyRubModel,
4264
summary="Channel for belly rubs",
4365
description="Send your belly rubs through here!",
4466
)
4567
async def handle_rub(sid, data):
46-
await purr_channel.emit(
47-
PurrModel(loudness=2, detail="Purr for all listeners")
48-
)
68+
await purr_channel.emit(PurrModel(loudness=2, detail="Purr for all listeners"))
4969
return "Ack to the one who rubbed"
5070
```
5171

5272
👉 [Check out the example AsyncAPI documentation output!](https://studio.asyncapi.com/?url=https://raw.githubusercontent.com/marianhlavac/fastapi-sio/master/examples/from_readme_asyncapi.json)
5373

5474
By default (you can change these values):
55-
- the Socket.io endpoint path is **`/sio/socket.io`** (the `socket.io` part is set automatically by some clients)
56-
- The AsyncAPI spec file is at **`/sio/docs/asyncapi.json`**
5775

58-
Find more in the [examples](/docs/examples.md).
76+
- the Socket.io endpoint path is **`/sio/socket.io`** (the `socket.io` part is set automatically by some clients)
77+
- The AsyncAPI spec file is at **`/sio/docs/asyncapi.json`**
78+
79+
Find more in the [example](./examples/from_readme.py).
5980

6081
## Documentation & Reference
6182

6283
Refer to the [/docs](./docs/index.md) directory to learn how to use this library in your project.
6384

6485
_TODO: This documentation will be hosted on Github Pages in the near future, hopefully._
6586

66-
6787
## Contribution
6888

6989
...
7090

7191
## Used by
7292

73-
<a href="https://dronetag.cz"><img src="https://dronetag.cz/assets/logo-full.svg" height="32" /></a>
93+
<a href="https://dronetag.com"><img src="https://dronetag.com/assets/logo.png" height="68" /></a>
7494

75-
[Feel free to open a PR](https://github.com/marianhlavac/fastapi-sio/pulls) to add your project or company to this list.
95+
[Feel free to open a PR](https://github.com/marianhlavac/fastapi-sio/pulls) to add your project or company to this list.

docs/index.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Using fastapi-sio
2+
3+
This doc page provides a short guide to the fastapi-sio library. It includes instructions for integrating fastapi-sio with FastAPI, details on using emitters for Socket.IO channels, and guidance on generating and using AsyncAPI documentation. Use this page as the main entry point for understanding and utilizing the features of fastapi-sio in your projects.
4+
5+
## Using with FastAPI
6+
7+
To get started, install the library and set up your FastAPI app as usual. Then, create a `FastAPISIO` instance and mount it to your FastAPI application.
8+
9+
**Example:**
10+
11+
```python
12+
fastapi = FastAPI()
13+
sio = FastAPISIO(app=fastapi)
14+
15+
# Mount the ASGI app
16+
app = sio.asgi_app
17+
18+
@fastapi.get("/")
19+
async def example_fastapi_route():
20+
return {"message": "Welcome to the FastAPI-SIO example!"}
21+
```
22+
23+
Run the ASGI app provided by FastAPISIO.asgi_app. Socket.io traffic will be handled by _socketio_, while the rest by _FastAPI_.
24+
25+
---
26+
27+
## Using Emitters
28+
29+
Emitters allow you to send data to clients on specific Socket.IO channels, with full type safety and schema validation.
30+
31+
**How to use:**
32+
33+
1. **Define a Pydantic model** for the data you want to emit.
34+
2. **Create an emitter** using `sio.create_emitter("event_name", model=YourModel)`.
35+
3. **Emit data** by calling `await emitter.emit(payload)` inside your handler.
36+
37+
**Example:**
38+
39+
```python
40+
class Notification(BaseModel):
41+
message: str
42+
43+
notifier = sio.create_emitter("notify", model=Notification)
44+
45+
@sio.on("ping")
46+
async def handle_ping(sid, data):
47+
await notifier.emit(Notification(message="pong!"))
48+
```
49+
50+
- The emitted data will be validated and serialized according to your Pydantic model.
51+
52+
---
53+
54+
## AsyncAPI Documentation
55+
56+
`fastapi-sio` automatically generates an [AsyncAPI](https://www.asyncapi.com/) specification for your Socket.IO events and data models.
57+
58+
### How to access the documentation
59+
60+
- By default, the AsyncAPI spec is available at:
61+
`GET /sio/docs/asyncapi.json`
62+
63+
### How to use the documentation
64+
65+
1. **Start your FastAPI app.**
66+
2. **Open the `/sio/docs/asyncapi.json` endpoint** in your browser or with a tool like `curl`.
67+
3. **Copy the JSON output.**
68+
4. **Paste it into the [AsyncAPI Studio](https://studio.asyncapi.com/)** online to view and interact with your API documentation.
69+
70+
> [!NOTE]
71+
> Currently, `fastapi-sio` does not self-host the AsyncAPI Studio UI. You must manually copy the JSON and use the AsyncAPI online tools.

examples/from_readme.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,46 @@
66
from pydantic import BaseModel
77
from fastapi_sio import FastAPISIO
88

9+
910
class PurrModel(BaseModel):
1011
detail: str
1112
loudness: int
1213

14+
1315
class BellyRubModel(BaseModel):
1416
where_exactly: str
1517
scratches_num: int
1618

17-
fastapi_app = FastAPI()
18-
sio_app = FastAPISIO(app=fastapi_app)
1919

20-
purr_channel = sio_app.create_emitter(
20+
fastapi = FastAPI()
21+
sio = FastAPISIO(app=fastapi)
22+
23+
# Mount this ASGI app. The FastAPI app is passed as the other_asgi_app
24+
# so you'll have access to the FastAPI routes as well.
25+
app = sio.asgi_app
26+
27+
# Run with `uvicorn examples.from_readme:app`
28+
29+
30+
@fastapi.get("/")
31+
async def example_fastapi_route():
32+
return {"message": "Welcome to the FastAPI-SIO example!"}
33+
34+
35+
purr_channel = sio.create_emitter(
2136
"purrs",
2237
model=PurrModel,
2338
summary="Channel for purrs",
2439
description="Receive any purrs here!",
2540
)
2641

27-
@sio_app.on(
42+
43+
@sio.on(
2844
"rubs",
2945
model=BellyRubModel,
3046
summary="Channel for belly rubs",
3147
description="Send your belly rubs through here!",
3248
)
3349
async def handle_rub(sid, data):
34-
await purr_channel.emit(
35-
PurrModel(loudness=2, detail="Purr for all listeners")
36-
)
37-
return "Ack to the one who rubbed"
50+
await purr_channel.emit(PurrModel(loudness=2, detail="Purr for all listeners"))
51+
return "Ack to the one who rubbed"

0 commit comments

Comments
 (0)