async def add_event_listener(
    request: Request, payload: ListenPayload
) -> t.AsyncIterator:
    """Add an event listener to the server

    Args:
        request: the event source request
        payload: the initialization payload

    Returns:
        A server sent event source
    """
    data = await initialize_listener(payload)
    try:
        if data.is_app:
            yield ServerSentEvent(
                event=StateUpdate.get_event_name(),
                data=json_util.dumps(
                    asdict(
                        StateUpdate(state=data.state.serialize()),
                        dict_factory=dict_factory,
                    )
                ),
            )

        while True:
            disconnected = await request.is_disconnected()
            if disconnected:
                await disconnect(
                    data.is_app,
                    data.request_listeners,
                )
                break

            events: t.List[t.Tuple[datetime, EventType]] = []
            for _, listener in data.request_listeners:
                if listener.queue.qsize():
                    events.append(listener.queue.get_nowait())

            events = sorted(events, key=lambda event: event[0])

            for _, event in events:
                if isinstance(event, StateUpdate):
                    # we copy here as this is a shared object
                    event = StateUpdate(state=event.state.serialize())

                yield ServerSentEvent(
                    event=event.get_event_name(),
                    data=json_util.dumps(
                        asdict(event, dict_factory=dict_factory)
                    ),
                )

            await asyncio.sleep(0.2)

    except asyncio.CancelledError as e:
        await disconnect(data.is_app, data.request_listeners)
        raise e
Add an event listener to the server

    Args:
        request: the event source request
        payload: the initialization payload

    Returns:
        A server sent event source
    
fiftyone/server/events/listener.py