Making applications real-time is a common problem, even in popular tech stacks. This is a solution to ramp up quickly using Flask-SocketIO in an app that leverages Python server-side with a React client. From flask import Flask, rendertemplate, request, redirect, urlfor from flasksocketio import SocketIO, send, emit import time app = Flask (name) app.config 'SECRETKEY' = 'secret!' Socketio = SocketIO (app) @socketio.on ('message') def task (msg): time.sleep (3) send (msg) # (Socketio) Sending message to client once the background task is done. In this blog post, I'll be explaining what NGINX is and how to configure it for serving a Flask web application. This blog post is part of a larger series on deploying Flask applications. Last Updated December 22nd, 2018 This tutorial was written using Python 3.6.Some of the code used is not compatible with version 2. In this tutorial we'll be exploring how one can create a socket.io based webserver in Python using the socketio module. I introduce Flask-SocketIO in this video by creating a simple chat app. Lookout of for other SocketIO videos where I create more complicated apps.To download.
I am working on a project in which I need to stream live data every millisecond into a websocket. The tool I chose to accomplish this task is Flask-SocketIO. It is a quick and simple way to turn on socket communication in a Flask application.
To start, I needed Flask-SocketIO. To install it run:
pip install flask-socketio
I am working on a project in which I need to stream live data every millisecond into a websocket. The tool I chose to accomplish this task is Flask-SocketIO. It is a quick and simple way to turn on socket communication in a Flask application.
To start, I needed Flask-SocketIO. To install it run:
pip install flask-socketio
Setting up a basic a Flask app is easy enough. I did this to get started:
Flask Socketio Chat App
Landscape pencil sketch. In order to get things going with a socket that handles streaming data I needed to add the following two imports and one constant. I use time.sleep()
to control the how quick data is written to the socket and I use Thread()
to stream data on a separate thread.
Next I used a class that handles the streaming of data on a separate thread. Starting from the top I have a class CountThread
that inherits from Thread
. Note in the constructor that super
is used to access the parent Thread
class. Next get_data()
is used to loop over the numbers 0-3 and emit each number to the SOCKETIO
constant. The call to sleep is used to avoid filling up the socket too quickly. Lastly, the run()
method is used by default to start a Thread
.
Next is the code that creates and starts the thread. The @SOCKETIO
decorator is used to define the action to act on, in this case 'connect'
, as well as the namespace to work within, '/flask-socketio-demo'
, in this example. Within the connect_socket()
method the THREAD
‘s status is checked with isAlive()
and then started.
The final piece is to build the front-end. All we need here is a basic template with some JavaScript. The JavaScript handles connecting to the socket and then waits for any ‘count' events. When it receives such an event it logs it.
Flask Socketio Emit To Specific Client
To test this out start the server and navigate to the index route in a browser. Open up developer tools in the browser and the console will log 0,1,2,3 in 1 second increments.