Fastapi: Hi everybody, I am using the websocket of this API and I need to use a way to send a message for clients outside of the websocket decorator. Somebody can help me?

Created on 3 Sep 2020  路  4Comments  路  Source: tiangolo/fastapi

question

Most helpful comment

Someone can probably help you, but I'm not sure if they will have the willingness to do that... Please follow our template: https://github.com/tiangolo/fastapi/blob/master/.github/ISSUE_TEMPLATE/question.md

It really helps us to help you :tada:

All 4 comments

Someone can probably help you, but I'm not sure if they will have the willingness to do that... Please follow our template: https://github.com/tiangolo/fastapi/blob/master/.github/ISSUE_TEMPLATE/question.md

It really helps us to help you :tada:

from fastapi import FastAPI, WebSocket, WebSocketDisconnect
import uvicorn

app = FastAPI()

@app.websocket(path='/ws')
async def websocket_endpoint(websocket: WebSocket):
try:
await websocket.accept()
print(websocket.client_state)
while True:
data = await websocket.receive_text()
print(data)
await websocket.send_json({'msg':data})
except WebSocketDisconnect:
print(websocket.client_state)

uvicorn.run(app)

This is my code. I just wanna know how to send datas to client outside it. At now I am connecting only one client...

https://github.com/lsm1103/fastapi_websocket.git

You can take a look at this little demo of mine
鍙互鐪嬬湅鎴戠殑杩欎釜灏廳emo

Thanks for the help here everyone! :clap: :bow:

@jorgebg2016 if you are still having problems, please create a new issue following the template step by step. That will guide you to write the question in a way that is easier to help you or even better, will guide you to find the answer yourself. Given that, I'm gonna close this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DrPyser picture DrPyser  路  3Comments

mr-bjerre picture mr-bjerre  路  3Comments

kkinder picture kkinder  路  3Comments

zero0nee picture zero0nee  路  3Comments

devtud picture devtud  路  3Comments