Skip to main content

Server Sent Events (SSE) for Flask using the Mercure protocol

Project description

Flask-Mercure-SSE

Provide push capabilities using server-sent events to your Flask apps. Based on the Mercure protocol.

  • Built-in hub for development
  • Use any external Mercure hub
  • Use external hub like the Mercure.rocks hub for production and scaling

Installation

pip install flask-mercure-sse

Getting started

Enable the MercureSSE extension:

from flask import Flask
from flask_mercure_sse import MercureSSE

app = Flask(__name__)
mercure = MercureSSE(app)

Publish messages from anywhere in your app:

mercure.publish("topic", "message")

Generate subscription urls in your templates:

<script>
const es = new EventSource("{{ mercure_hub_url('topic') }})");
// ...
</script>

About the built-in hub

The built-in hub is for development only as it is not scalable at all.

It implements all the required part of the Mercure specification including authorization. Subscriptions are not implemented.

Configuration

Key Description Default
MERCURE_HUB_URL External hub url None
MERCURE_PUBLISHER_JWT The authorization JWT to publish on external hubs Required when hub url is provided
MERCURE_AUTHZ_COOKIE_NAME Authorization cookie name mercureAuthorization
MERCURE_TYPE_IS_TOPIC Whether to auto set type to topic name when no type is provided False
MERCURE_HUB_ALLOW_PUBLISH Whether to allow publishing via HTTP with the built-in hub False
MERCURE_HUB_ALLOW_ANONYMOUS Whether to allow anonymous subscribers to connect True
MERCURE_SUBSCRIBER_SECRET_KEY Secret key to generate subscriber JWTs app.config["SECRET_KEY"]
MERCURE_PUBLISHER_SECRET_KEY Secret key to generate publisher JWTs app.config["SECRET_KEY"]

Authorization

Publish privately using private=True in publish().

Using external hubs

Provide the authorization JWT to the frontend:

  • Use mercure_hub_url(topics, "SUBSCRIBER_JWT") to generate subscription urls with the authorization parameter.
  • Use mercure_authentified_hub_url(topics) to generate subscription urls using a subscriber jwt generated using mercure_subscriber_jwt().
  • Use MercureSSE.set_authz_cookie(response, jwt="SUBSCRIBER_JWT") to define the mercureAuthorization cookie.

Use mercure_subscriber_jwt(topics) in templates to generate a JWT.

Using the built-in hub

First, ensure that a secret key is defined in your app config.

By default, publishing is not possible via the HTTP api for security reason. You will only need to publish internally using MercureSSE.publish().

Create authorization JWT for subscribers using MercureSSE.create_subscription_jwt(topics).

To authorize subscribers:

  • Pass the JWT to mercure_hub_url() in templates like external hubs
  • Use MercureSSE.set_authz_cookie(response, topics) to define the mercureAuthorization cookie.

When publishing via HTTP is allowed, app.extensions["mercure"].publisher_jwt is used as the authorization JWT.

Using signals as event sources

Use MercureSSE.publish_signal(signal) to publish an event each time the signal is dispatched

my_event = signal('my-event')
mercure.publish_signal(my_event) # topic is the event name

Check out the parameters of publish_signal() for options when handling the event.

CLI

Some CLI commands are available.

Start with flask mercure --help.

Going to production

It is recommended to use the official Mercure.rocks hub in production environments.

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

flask_mercure_sse-0.2.2.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

flask_mercure_sse-0.2.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file flask_mercure_sse-0.2.2.tar.gz.

File metadata

  • Download URL: flask_mercure_sse-0.2.2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.17

File hashes

Hashes for flask_mercure_sse-0.2.2.tar.gz
Algorithm Hash digest
SHA256 f3e906f2da2a8bbdbb43503dd5114663addacd120145106606ea54a5f182d8bc
MD5 9813da426c6e00b1f7bde35b501ace6f
BLAKE2b-256 6ecd33294c7d5022fa0a277c1785b5e1175b9c50d7da1c25aac37e0bad8cca55

See more details on using hashes here.

File details

Details for the file flask_mercure_sse-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_mercure_sse-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 501ddb75ae267ab3d1193569e9e72d20e94cd6f1af8a2e2218138b647929a448
MD5 ea60cb4bca40f28244c03dfaaeab6c2d
BLAKE2b-256 6f29d56cb817103547fcd2bb4e793cbe7dadef12c9800d8ae4f59285f8889512

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