Skip to main content

A Flask package for managing SSE streams.

Project description

Flask Streamy

flask-streamy is a lightweight Flask package that provides an abstraction for managing Server-Sent Events (SSE) streams. It allows you to easily create, manage, and terminate event streams in your Flask applications.

Installation

pip install flask-streamy

Basic Usage

from flask import Flask
from flask_streamy import StreamManager

app = Flask(__name__)
sse = StreamManager()

@app.route('/send_event/<stream_id>')
def send_event(stream_id):
    sse.send_event(stream_id, data="Hello, World!", event_name="greeting")
    return f"Event sent to stream {stream_id}"

@app.route('/events/<stream_id>')
def events(stream_id):
    return sse.get_stream(stream_id)

@app.route('/end_stream/<stream_id>')
def end_stream(stream_id):
    sse.end_stream(stream_id)
    return f"Stream {stream_id} ended"

if __name__ == "__main__":
    app.run(debug=True)

API

StreamManager

  • get_stream(stream_id, event_name="message"): Retrieves or creates a stream for the given ID.

  • send_event(stream_id, data, event_name=None): Sends an event to the specified stream.

  • end_stream(stream_id): Ends the stream for the given ID.

SSE

  • add_message(data, event_name=None): Adds a message to the stream.
  • end_stream(): Ends the stream.

Testing

Here's an updated section for the README.md to include instructions on how to test the flask-streamy package:

Running Tests

Tests for flask-streamy are located in the tests directory. To run the tests, you'll need to have unittest available, which is included in the Python standard library.

You can run the tests by executing the following command in the root of the package:

python -m unittest discover tests

Writing Tests

The tests/test_flask_streamy.py file contains example unit tests that check the core functionality of flask-streamy. Here’s a brief overview of what the tests cover:

  • Stream Creation and Retrieval: Ensures that streams are correctly created and retrieved by get_stream.
  • Sending Events: Validates that events are correctly added to the stream's message queue.
  • Ending Streams: Confirms that streams can be properly terminated and are no longer accessible after being ended.

You can add your own tests to the tests directory to cover additional use cases or to test custom functionality you may add to the package.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

flask_streamy-0.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file flask_streamy-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_streamy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1fba19570323cf2dd9dec7e0bb2d82e7336aba0ca64dc09307292ffebb8c4072
MD5 9c55ec4a588bb9f656f014451fd0c9fb
BLAKE2b-256 d8a405ab13e3b99a1373956d86a6e987a12cc64017eae22a18d9f1d44d6bf83f

See more details on using hashes here.

Supported by

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