Loss connection with WebSockets LPR events

Hello gouys, how are you?

I developed an integration based on the exempla EventsAndStateWebSocketApiPython. But Constantly I lost the connection with the server and I don’t connect again, it’s needed to reset the Events Server in the Milestone server application to connect again and continuing receiving the events.

Below are the part of my code where I connect with webscokets and mantaining the applicationg receiving the events. To you know the erros are always 401, 502 and 503.

async def main(ip_address, access_token, verify_ssl, controller, milestone_thread: IoMilestoneCommunication):
    session_id = ""
    last_event_id = ""
 
    while not milestone_thread.do_stop:
        try:
            escape_task = asyncio.create_task(check_for_escape(ip_address, access_token))
            connection_uri = f"wss://{ip_address}/api/ws/events/v1/" if verify_ssl else f"ws://{ip_address}/api/ws/events/v1/"
            receive_events_task = None
 
            async with connect(connection_uri, extra_headers={"Authorization": f"Bearer {access_token}"}) as web_socket:
                session = await start_session(web_socket, session_id, last_event_id)
                state_events = []
                if session["status"] == 201:
                    subscription = await create_subscription(web_socket, subscription_filters)
                    milestone_thread.trace(f"Created Subscription => {subscription}")
                    session_id = session["sessionId"]
 
                await process_events(state_events, ip_address, access_token, controller)
 
                while not escape_task.done():
                    receive_events_task = asyncio.create_task(receive_events(web_socket))
                    done, pending = await asyncio.wait([receive_events_task, escape_task], return_when=asyncio.FIRST_COMPLETED)
                    if escape_task.done():
                        await web_socket.close()
                        trace("Reconnection triggered by escape task.")
                        break
                    if receive_events_task in done:
                        events = await receive_events_task
                        last_event_id = events["events"][-1]["id"]
                        await process_events(events["events"], ip_address, access_token, controller)
 
            if receive_events_task is not None:
                await receive_events_task
 
        except asyncio.CancelledError:
            trace("Task was cancelled. Retrying...")
            await asyncio.sleep(1)
            continue
 
        except (ConnectionClosedOK, ConnectionClosedError):
            trace("[*] WebSocket connection closed. Reconnecting...")
            await asyncio.sleep(1)
 
        except websockets.exceptions.InvalidStatusCode as e:
            trace(f"InvalidStatusCode exception: {e}. HTTP {e.status_code}")
            if e.status_code == 401:
                trace("[*] Re-authenticating due to HTTP 401 error.")
                milestone_thread.token = milestone_thread.login()
                await asyncio.sleep(1)
            elif e.status_code in (502, 503):
                trace(f"[*] HTTP {e.status_code} error. Backing off before retrying...")
                await asyncio.sleep(10)
            else:
                await asyncio.sleep(5)
 
        except asyncio.exceptions.TimeoutError:
            trace("[!] Timeout error. Retrying connection...")
            await asyncio.sleep(2)
 
        except Exception as e:
            report_exception(e)
            trace(f"[!] General Exception: {e}")
            await asyncio.sleep(2)
 
        finally:
            if not escape_task.done():
                escape_task.cancel()
                try:
                    await escape_task
                except asyncio.CancelledError:
                    trace("[*] Escape task cancelled.")

Software Version: 2023 R3 (23.3a)

Can you guys already seen this problem?

Best Regards,

João Pedro

Hi João Pedro,

We can confirm there is an issue in the version 23.3a, which is very similar to what you describe. In short it is possible to connect to the ESS channel in the first 20 minutes after start of the EvS. And then it is not with error code 50X. It is normal sometimes you are disconnected, but you should be able to reconnect afterwards.

The bug is fixed in the 24.1 version. We are in the process of creating a hotfix for version 23.3a, but this would take some time. Meanwhile the only thing I could recommend till then is to migrate to 24.1, if that is possible.