API

class flask_sse.Message(data, type=None, id=None, retry=None)

Data that is published as a server-sent event.

__init__(data, type=None, id=None, retry=None)

Create a server-sent event.

Parameters:
  • data – The event data. If it is not a string, it will be serialized to JSON using the Flask application’s JSONEncoder.
  • type – An optional event type.
  • id – An optional event ID.
  • retry – An optional integer, to specify the reconnect time for disconnected clients of this stream.
__str__()

Serialize this object to a string, according to the server-sent events specification.

to_dict()

Serialize this object to a minimal dictionary, for storing in Redis.

class flask_sse.ServerSentEventsBlueprint(name, import_name, static_folder=None, static_url_path=None, template_folder=None, url_prefix=None, subdomain=None, url_defaults=None, root_path=None)

A flask.Blueprint subclass that knows how to publish, subscribe to, and stream server-sent events.

messages(channel='sse')

A generator of Message objects from the given channel.

publish(data, type=None, id=None, retry=None, channel='sse')

Publish data as a server-sent event.

Parameters:
  • data – The event data. If it is not a string, it will be serialized to JSON using the Flask application’s JSONEncoder.
  • type – An optional event type.
  • id – An optional event ID.
  • retry – An optional integer, to specify the reconnect time for disconnected clients of this stream.
  • channel – If you want to direct different events to different clients, you may specify a channel for this event to go to. Only clients listening to the same channel will receive this event. Defaults to “sse”.
redis

A redis.StrictRedis instance, configured to connect to the current application’s Redis server.

stream()

A view function that streams server-sent events. Ignores any Last-Event-ID headers in the HTTP request. Use a “channel” query parameter to stream events from a different channel than the default channel (which is “sse”).

flask_sse.sse = <flask_sse.ServerSentEventsBlueprint object>

An instance of ServerSentEventsBlueprint that hooks up the stream() method as a view function at the root of the blueprint. If you don’t want to customize this blueprint at all, you can simply import and use this instance in your application.