Skip to main content

A simple server sent events client for python

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_sse_client-1.0.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_sse_client-1.0.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file py_sse_client-1.0.0.tar.gz.

File metadata

  • Download URL: py_sse_client-1.0.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for py_sse_client-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ab0a319634e36757afb771223b418bdfd275a76362e8f6fae96d8da1c7d150da
MD5 e1ff78c81fc57aecf22d3955a1d96ab1
BLAKE2b-256 78e669e78a24727246eededc4a6d95d6800850491fdbf49949abdcbffe165942

See more details on using hashes here.

File details

Details for the file py_sse_client-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: py_sse_client-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for py_sse_client-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 093cfbc49ac250a89e97724786a92bec46cb2e1df9978c01a9075ac0f5a22108
MD5 95902ef20db0adb95acaa4bbdbb90d1a
BLAKE2b-256 4749e6c9cbda25db3bfcfe90d15453cfec0685a6fe93a8646d67ff4132f8feba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page