You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- 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))
19
19
20
20
## What‘s Missing?
21
21
22
22
-[ ] 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
24
24
-[ ] Support for more obscure fields of AsyncAPI, such as `traits`, ...
25
25
26
26
## Usage Example
27
27
28
28
```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
31
32
32
-
purr_channel = sio_app.create_emitter(
33
+
classPurrModel(BaseModel):
34
+
detail: str
35
+
loudness: int
36
+
37
+
classBellyRubModel(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
+
asyncdefexample_fastapi_route():
52
+
return {"message": "Welcome to the FastAPI-SIO example!"}
53
+
54
+
purr_channel = sio.create_emitter(
33
55
"purrs",
34
56
model=PurrModel,
35
57
summary="Channel for purrs",
36
58
description="Receive any purrs here!",
37
59
)
38
60
39
-
@sio_app.on(
61
+
@sio.on(
40
62
"rubs",
41
63
model=BellyRubModel,
42
64
summary="Channel for belly rubs",
43
65
description="Send your belly rubs through here!",
44
66
)
45
67
asyncdefhandle_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"))
49
69
return"Ack to the one who rubbed"
50
70
```
51
71
52
72
👉 [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)
53
73
54
74
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`**
57
75
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).
59
80
60
81
## Documentation & Reference
61
82
62
83
Refer to the [/docs](./docs/index.md) directory to learn how to use this library in your project.
63
84
64
85
_TODO: This documentation will be hosted on Github Pages in the near future, hopefully._
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
+
asyncdefexample_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.
0 commit comments