Skip to content

events

Yield pushed event envelopes asynchronously until the server closes the socket. The subscription must already be open, normally through an async with block.

Yield pushed event payloads until the server closes the socket.

Raises:

Type Description
RuntimeError

If the subscription has not been opened with async with.

Example
async with client.subscribe([{"event": "pane.output"}]) as stream:
    async for event in stream.events():
        print(event["event"])
Source code in src/herdr_client/async_client/client.py
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
async def events(self) -> AsyncIterator[EventEnvelope]:
    """Yield pushed event payloads until the server closes the socket.

    Raises:
        RuntimeError: If the subscription has not been opened with ``async with``.

    Example:
        ```python
        async with client.subscribe([{"event": "pane.output"}]) as stream:
            async for event in stream.events():
                print(event["event"])
        ```
    """
    reader = self._reader
    if reader is None or self._writer is None:
        raise RuntimeError("subscription has not been opened")

    while True:
        line = await read_line(reader, self._timeout)
        if line == b"":
            return
        if line.strip() == b"":
            continue
        yield event_envelope(decode_json(line))