Basiclly, my LiveQuery listening loop is something like this:
# Login and and get the session_token as described in above posts
async with websockets.connect(LIVEQUERY_URL) as websocket:
# Connect
# Subscribe
while(1)
recv_event = await websocket.recv()
# handle execption in recv()
# handle zero length answer (just in case we are using an older websockets version)
# handle 'op':'error'
# handle 'op':'unsubscribed'
# handle regular operations needed for the business logic.
What happens is that after a while (like 8-12hrs) the Back4App server does not send any new updates on the websocket. The websocket remains connected (no exceptions, no zero-length data received). From my side, the await recv() never receives anything, regardless of what is happening on the back4app tables.
While the frequency of updates is rather small (few/hr at most) I can't pinpoint the exact moment when the connection is silently dropped.
For now, my solution to this is to send an unsubscribe message after some hours. Thanks to the async, I do it like this:
Prepare a coroutine that waits x seconds then sends the message. This coroutine has access to the websocket object above
async def cancel_connection():
await asyncio.sleep(CONNECTION_TIMEOUT)
await trigger_unsubscribe() # sends the "op": "unsubscribe"
await asyncio.sleep(3)
await websocket.close()
and before while(1) listening loop:
asyncio.ensure_future(cancel_connection())
[Now, CONNECTION_TIMEOUT is set to 8hrs.]
It's been 2 days and for now, things are working (eg. I don't wake up in the morning and I find the connection silently dropped)
Any of you guys have any idea why the connection is silently dropped?
Thank you!
p.s. Maybe is relevant, maybe it is not. I think this behavior started 2-3 weeks ago after my back4app instance froze somehow and it was rebooted.