Synchronous and asynchronous library for server-sent events.
Project description
aiosse
Description
Yet another library for server-sent events.
This library works with httpx to support
synchronous as well as asynchronous workflows.
Note
Although there are already some libraries on the subject
(aiohttp-sse-client,
aiosseclient), these are
unfortunately not entirely correct. In example, both asynchronously iterate
over the stream content via async for line in response.content
[^1][^2].
This internally calls aiohttp's readuntil
method with
the default seperator \n
, but the official specification says:
Lines must be separated by either a U+000D CARRIAGE RETURN U+000A LINE FEED (CRLF) character pair, a single U+000A LINE FEED (LF) character, or a single +000D CARRIAGE RETURN (CR) character.
Another point is the error handling, which is often not sufficient to analyze the error or is entirely skipped.
[^1]: Code Reference
[^2]: Code Reference
Example
sync
import logging
import ssec
def main() -> None:
logging.basicConfig(level=logging.INFO)
for event in ssec.stream(
"https://stream.wikimedia.org/v2/stream/recentchange"
):
print(event)
main()
async
import asyncio
import logging
import ssec
async def main() -> None:
logging.basicConfig(level=logging.INFO)
async for event in ssec.stream_async(
"https://stream.wikimedia.org/v2/stream/recentchange"
):
print(event)
asyncio.run(main())
Installation
ssec is written in Python and tries to keep track of the newest version available. Currently[^3], this is Python 3.12.3. On some operating systems, this version is pre-installed, but on many it is not. This guide will not go into details on the installation process, but there are tons of instructions out there to guide you. A good starting point is the beginners guide.
[^3]: 06. May 2024
Installation (User)
There is no user installation yet. Please refer to the developer installation.
Installation (Developer)
1. Clone this repository to a desired location on your maschine using ssh
:
git git@github.com:sharly-project/ssec.git
2. Change into the project directory:
cd ssec
3. Create a virtual environment:
..with venv
:
python3.12 -m venv .venv
..with uv
:
uv venv -p 3.12
4. Activate the virtual environment:
.. on Windows:
.\.venv\Scripts\activate
.. on Linux:
source \.venv\bin\activate
.. on MacOS:
-
If everything worked out, you should now see a (.venv) or (ssec) prompt in your terminal.
5. Install ssec's developer edition:
..with pip
:
pip install -e .[dev]
..via uv
:
uv pip install -e .[dev]
Miscellaneous
Documentation
Build the documentation by running the following command in the root directory of the project:
sphinx-build -b html docs/src docs/build
The command requires that the developers edition of
ssec
is installed and the virtual environment is running.
The documentation is then accessible via doc/build/index.html
.
Set up Visual Studio Code for Development
To edit the code base with Visual Studio Code, install the following extensions:
Necessary settings are already included in the .vscode
directory and should
be enabled per default.
Contributing
Contributing to ssec
is highly appreciated, but comes with some requirements:
-
Type Hints
Write modern python code using type annotations to enable static analysis and potential runtime type checking.
-
Documentation
Write quality documentation using numpydoc docstring conventions.
-
Linting
-
Style
Format your code using ruff.
-
Testing
Write tests for your code using pytest.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.