Skip to main content

py-sse-client

A simple asynchronous Server Sent Events (SSE) client for Python.

For more information about Server Sent Events, refer to MDN Web Docs.

Installation

Install the package using pip:

pip install py-sse-client

Usage

Quickstart

import asyncio
import pysse

def listener(event):
    print(event)

async def main():
    client = pysse.Client("https://example.com/sse", listener)
    await client.connect()

if __name__ == "__main__":
    asyncio.run(main())

Creating a Client

To create a client, use the Client class. The constructor accepts the following arguments:

  • url:
    • The URL of the SSE endpoint
    • Type: str
    • Required
  • callable:
    • A callback function that will be invoked whenever an event is received
    • The callback receives an Event object as its argument
    • Type: Callable
    • Required
  • param:
    • Query parameters to include in the request
    • Default is an empty dictionary
    • Type: dict
    • Optional
  • headers:
    • HTTP headers to include in the request
    • Default is an empty dictionary
    • Type: dict
    • Optional

Example:

from pysse import Client

def listener(event):
    print(f"Event Name: {event.name}")
    print(f"Event Data: {event.data}")

client = Client(
    url="https://example.com/sse",
    callable=listener,
    param={"token": "your_token"},
    headers={"Authorization": "Bearer your_token"}
)

Connecting to a Server Sent Events API

To connect to the SSE API, call the connect method of the Client instance. This method is asynchronous and should be awaited.

Example:

import asyncio

async def main():
    await client.connect()

asyncio.run(main())

Disconnecting from the Server Sent Events API

To disconnect from the SSE API, call the close method of the Client instance. This method is also asynchronous and should be awaited.

Example:

async def main():
    await client.close()

asyncio.run(main())

API Reference

class pysse.Client

Constructor

Client(url: str, callable: Callable, param: dict = {}, headers: dict = {})

Arguments:

  • url (str): The URL of the SSE endpoint
  • callable (Callable): A callback function to handle incoming events
  • param (dict, optional): Query parameters for the request
  • headers (dict, optional): HTTP headers for the request

Methods

  • async connect() -> None
    Connects to the SSE API and starts listening for events

  • async close() -> None
    Disconnects from the SSE API

  • is_closed() -> bool
    Returns True if the connection is closed, otherwise False

class pysse.Event

A data class representing an SSE event

Attributes:

  • name (str): The name of the event
  • data (dict): The data associated with the event

License

This project is licensed under the MIT License. See the LICENSE file for details.

Links

Metadata

Release files for py-sse-client 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-sse-client 1.0.1
File Size Uploaded Provenance Actions
py_sse_client-1.0.1.tar.gz 4.4 kB Without provenance Details for py_sse_client-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for py-sse-client 1.0.1
File Interpreter ABI Platform Actions
py_sse_client-1.0.1-py3-none-any.whl Python 3 none any Details for py_sse_client-1.0.1-py3-none-any.whl

Total release size: 9.3 kB

Release files / py_sse_client-1.0.1.tar.gz

Download URL py_sse_client-1.0.1.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
685712a71940fa6f4e2d7867c003d9187bf408f33e65d7899358d04cf00cc4f3
BLAKE2b-256 checksum
How to use checksums
4edade2bba42b75fc2125cdd92358dad80af6915107b1469adf0f5774a3ed88b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / py_sse_client-1.0.1-py3-none-any.whl

Download URL py_sse_client-1.0.1-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ffed0881cc47efcb42620e99b85d11d2e6e944b8e86dbf0a88f068585917ebe6
BLAKE2b-256 checksum
How to use checksums
864470b53e8af72cc6b419bbf4f8a6a3fe9e6a005fc33889efeaf52fff8df78b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Security for py-sse-client 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page