-
First Check
Commit to Help
Example Code# What I'm doing
app = FastAPI()
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await manager.connect(websocket)
try:
while True:
json = await websocket.receive_json()
response = await websocket_parser(json)
if(response):
await manager.send_response(response)
else:
manager.disconnect(websocket)
except WebSocketDisconnect:
manager.disconnect(websocket)
# VS what I'm looking for, I know this is NodeJs, but I'm looking for a python way to do it
io.on("connection", socket => {
socket.on("phone.calling", (sessionDescription) => {
console.log('Someone is calling');
socket.broadcast.emit('phone.call', sessionDescription);
});
socket.on("phone.answer", (answer) => {
console.log('Annswer to phone');
socket.broadcast.emit('phone.answer', answer);
});
});DescriptionI'm looking for a way to create an instance of my WS, and define multiple "routes" / "endpoints" that could be used in that specific WS. Maybe something like ws = websocket.define("/ws")
@ws.add_websocket_route("connect")
def ws_connect(websocket: WebSocket):
...
@ws.add_websocket_route("phone.calling")
def ws_phone_calling(websocket: WebSocket):
...I found many functions in fastAPI that could do the tricks, but I don't really know if they were built for it : @websocket.add_api_websocket_route
@websocket.add_websocket_route
@websocket.add_routeIf you have any Idea 🙏 Operating SystemLinux Operating System DetailsZorinOS 16 Pro FastAPI Version0.70.0 Python Version3.8.10 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
I don't have an exact solution for you, but here's a couple of pointers that might help: Socket.io vs WebSocketsBased on your JS example, it looks like you may be confusing socket.io with WebSockets. socket.io uses WebSockets and other technologies under the hood to create a robust realtime connection. Fastapi + Socket.ioIf your tech stack includes socket.io, consider fastapi-socketio to integrate with FastAPI. This should let you define 'routes' like so (untested): from fastapi import FastAPI
from fastapi_socketio import SocketManager
app = FastAPI()
socket_manager = SocketManager(app=app)
@sm.on("phone.calling")
async def handle_join(sid):
await app.sio.emit('lobby', "phone.call")For greater flexibility, you could also consider using the socketio package for Python directly in your FastAPI app. |
Beta Was this translation helpful? Give feedback.
-
|
Ok thx for those explanation 😁, I was looking for a native ways to do it with Fast API, I guess I have no other choice then doing an if Elif else, to choose the correct route depending on the type I received. (But your fastapi-socketio was a great solution too 😉) I was just trying to do the must esthetics FastAPI native WS "router" |
Beta Was this translation helpful? Give feedback.
I don't have an exact solution for you, but here's a couple of pointers that might help:
Socket.io vs WebSockets
Based on your JS example, it looks like you may be confusing socket.io with WebSockets. socket.io uses WebSockets and other technologies under the hood to create a robust realtime connection.
Fastapi + Socket.io
If your tech stack includes socket.io, consider fastapi-socketio to integrate with FastAPI.
This should let you define 'routes' like so (untested):