1

I am getting a stream from a source which I made so that it can be accessed at a particular websocket URL (Not sure if this ideal and would appreciate any other architectures as well). I need to now do object detection on this video stream, and thought of the architecture that I will connect to the websocket URL through a client websocket library like websocket in a server which is made through flask or fastapi, and then again stream the object detected video to multiple clients through another websocket URL.

The problem is I am unable to receive any images even after connecting to the websocket URL and am not sure how to handle asyncio in a server scenario as in where to put the line run_until_complete.

Any suggestions or help would be greatly appreciated

Server code

import uvicorn
from fastapi import FastAPI, WebSocket
# import websockets
# import asyncio


# init app
app = FastAPI()


async def clientwebsocketconnection():
    uri = "wss://<websocket URL here>"  
    async with websockets.connect(uri) as websocket:
        print("reached here")
        data = await websocket.recv()
        print(f"< {data}")


# Routes
@app.get('/')
async def index():

    return {"text": "Its working"}


@app.websocket('/websocketconnection')  # FIXME: change this to a websocket endpoint
async def websocketconnection():

    return {"text": "Its working"}


if __name__ == '__main__':
    uvicorn.run(app, host="127.0.0.1", port=8000)
    # asyncio.get_event_loop().run_until_complete(clientwebsocketconnection())
davidism
  • 121,510
  • 29
  • 395
  • 339
Vikranth
  • 1,538
  • 1
  • 14
  • 23
  • Please have a look at [this](https://stackoverflow.com/a/70626324/17865804) and [this](https://stackoverflow.com/a/72070914/17865804) answer. – Chris Jun 26 '22 at 05:42

1 Answers1

0

I assume you want to send a text via websocket. TO do this you need the following code:

@app.websocket('/websocketconnection')
async def websocketconnection(websocket: WebSocket) -> None:
    await websocket.accept()
    await websocket.send_text("It's working!")
    await websocket.close()

You may find more examples in the official FastAPI docs.

Max Voitko
  • 1,542
  • 1
  • 17
  • 32