How do I serve Realtime Database updates through a websocket connecton on FastAPI backend

Viewed 415

I am using Firebase Realtime Database for a project I am developing, I want the client to be able to get updates in realtime using a WebSocket connection with the server. I have a simple backend in FastAPI.

I know how to create a websocket on my FastAPI server:

@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    while True:
        data = await websocket.receive_text()
        await websocket.send_text(f"Message text was: {data}")

And I know how to get the updates on my database (https://stackoverflow.com/a/53020497/1339231):

def listener(event):
    print(event.event_type)  # can be 'put' or 'patch'
    print(event.path)  # relative to the reference, it seems
    print(event.data)  # new data at /reference/event.path. None if deleted

firebase_admin.db.reference('my/data/path').listen(listener)

But I do not know how to connect these two things. Any help would be appreciated

0 Answers
Related