Coder Social home page Coder Social logo

pe-sky / flask-sse-no-deps Goto Github PK

View Code? Open in Web Editor NEW

This project forked from maxhalford/flask-sse-no-deps

0.0 0.0 0.0 3 KB

An example of server-sent events in Flask without extra dependencies

Home Page: https://maxhalford.github.io/blog/flask-sse-no-deps/

License: MIT License

Python 100.00%

flask-sse-no-deps's Introduction

Flask SSE example

Look ma, no dependencies!

This repository is an example of how to perform server-sent events (SSE) in Flask with no extra dependencies. Libraries such as flask-sse are great, but they require having to use Redis or some other sort of pubsub backend. While this can be fine, I wanted to show that you can do SSE by only using Flask. The following instructions explain how to run the example. For more information on how this works, please see the accompanying blog post.

You're going to need three terminal sessions to run this example in it's entirety. The first will run the server, the second will listen to events, and the third will trigger events. You may also open an additional terminal if you want to add another listener. In fact, having multiple listeners is a good test to check that a single message is correctly dispatched once to each listener.

In the first terminal, create a virtual environment, activate it, and install the necessary requirements.

python3 -m venv env
source env/bin/activate
pip install -r requirements.txt

Next, run the server.

export FLASK_APP=app.py
export FLASK_ENV=development
flask run

In the second terminal, listen for updates.

source env/bin/activate
python listen.py

Finally, in the third terminal, run the emit.py script.

source env/bin/activate
python emit.py

This will run an infinite loop which sends a GET request to the /ping route every second. This in turn will trigger an event which will be displayed in the listening terminal. You should see a new pong message being displayed every second or so.

flask-sse-no-deps's People

Contributors

maxhalford 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.