Coder Social home page Coder Social logo

flask-sse's Introduction

Flask-Sse

A simple Flask extension for HTML5 server-sent events support, powered by Redis

The extension provides 2 things - a blueprint with a single route for streaming events, and a helper function to send messages to subscribers:

from flask import Flask, json
from flask.ext.sse import sse, send_event

app = Flask(__name__)
app.register_blueprint(sse, url_prefix='/stream')

@app.route('/send')
def send_message()
    send_event('myevent', json.dumps({"message": "Hello!"}))

You can then subscribe to these events in a supported browser:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
</head>
<body>
  <script>
    var source = new EventSource("{{ url_for('sse.stream') }}");
    source.addEventListener('myevent', function(e) {
        var data = JSON.parse(e.data);
        // handle event
    }, false);
  </script>
</body>
</html>

The source comes with a basic example

Clients can subscribe to different channels by setting 'channel' on the query string, which defaults to 'sse'. These correspond to redis channels.

def send_message()
    send_event('myevent', json.dumps({"line": "Something happened"}), channel='logs')

#######
    
var source = new EventSource("{{ url_for('sse.stream'), channel='logs' }}")    

Being a blueprint, you can attach a before_request handler to handle things like access control:

@sse.before_request
def check_access():
    if request.args.get('channel') == 'firehose' and not g.user.is_admin():
        abort(403)

Configuration

Redis connection details are read from the applications config using the following keys (defaults in [])

  • SSE_REDIS_HOST [localhost]
  • SSE_REDIS_PORT [6379]
  • SSE_REDIS_DB [0]

Caveats

Subscribers will connect and block for a long time, so you should seriously consider running under an asynchronous WSGI server, such as gunicorn+gevent (like the example)

Credits

Inspired by django-sse

flask-sse's People

Contributors

bubenkoff avatar dazworrall avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.