Background
I am trying to create a simple REST API using the Flask-RESTful extension. This API will be working primarily to manage the CRUD and authentication of users for a simple service.
I am also trying to create a few web sockets using the Flask-SocketIO extension that these users will be able to connect to and see real-time updates for some data related to other people using the service. As such, I need to know that these users are authenticated and authorized to connect to certain sockets.
Problem
However, I'm having a bit of trouble getting set up. It seems like I am not able to have these two components (the REST API and SocketIO server) work together on the same Flask instance. The reason I say this is because when I run the following, either the REST API or the SocketIO server will work, but not both:
from flask import Flask
from flask_restful import Api
from flask.ext.socketio import SocketIO
app = Flask(__name__)
api = Api(app)
socketio = SocketIO(app)
# some test resources for the API and
# a test emitter statement for the SocketIO server
# are added here
if __name__ == '__main__':
app.run(port=5000)
socketio.run(app, port=5005)
Question
Is the typical solution for this type of setup to have two distinct instances of Flask going at the same time? For instance, would my SocketIO server have to make requests to my REST API in order to check to see that a specific user is authenticated/authorized to connect to a specific socket?
You just want to run socketio.run(app, port=5005) and hit the REST API on port 5005.
The reason this works is because under the hood, Flask-SocketIO is running an evented webserver based on gevent (or with the 1.0 release, also eventlet) - this server handling the websocket requests directly (using the handlers you register via the socketio.on decorator) and is passing on the non-websocket requests to Flask.
The reason your code wasn't working is because both app.run and socketio.run are blocking operations. Whichever one ran first was looping, waiting for connections, never allowing the second to kick off. If you really needed to run your websocket connections on a different port you'd need to spawn either the socketio or the app run call on a different process.
Related
I've developed a Python Flask Back-end app which allows me to do some HTTP requests on a Jsonfile (a sort of database) such as GET (to see a list of items) or POST (to create a new item in the Json database). Until now, I used Postman to test my app and all worked well. However, I'd like to develop a Python Flask Front-end app in order to create a graphical interface (in a web browser with jinja templates) to do the same requests. The problem is, I don't know how to begin my new app, I've googled all my questions but found no answer...
How can we "link" front-end and back-end apps in order to get the information from the web brower, make the requests via the back-end and then send the response with the front-end?
Using RESTful API.
A infrastructure solution could be (a classic one):
Your app server listening on 5000. It uses the REST architectural.
Your frontend server listening on 80/443. It makes requests to your app server to get the data and fill your html template with this data. The requests can be made with a library like requests. The requests library is one of the simpliest but you can choose another one to make HTTP requests.
Workflow:
Client <-HTTP requests-> Frontend <-HTTP requests made with requests->
App Server <--> Database
Advantage:
One of the advantage of this architecture: you can easily duplicate your servers or having multiple app servers responsible of different tasks. They can be running on the same machine or separated machines.
Serving static files:
If you are talking about serving static files for the frontend, then you should use an existing Webserver like Nginx (html/css/js files).
I have a react application and a flask backend. I need a simple solution for the following case, without constantly polling the backend:
User triggers an action from React app (POST to backend)
Backend receives the request and triggers some other service (service X), which is probably a long-running process.
Second service gets back to backend when (if) it's done.
Backend pushes completion notification back to React frontend.
That is, React App -> Flask EP-A -> Service X (will call back Flask EP-B when it's done)
I've investigated WebSockets and SSE (server-sent events) options. I think I need a combination of SSE and some sort of webhooks (second service calling back to flask backend when it's done).
There are two things still blurry to me here:
In react app, I register to an endpoint on flask (e.g. /events/oncomplete) but it looks like it's continously receiving responses once I've opened a connection.
For second service to notify backend, I need another endpoint (service X calling flask), how do I connect these two? (Service X -> Flask EP-B ->Flask EP-A -> React App)
Or should I take a different approach?
Thanks,
I think you need to use websockets.
For Node.js npm package - https://www.npmjs.com/package/ws
I deployed my Falcon app in azure functions using bitbucket. But I cannot see any of the files in the function app. I also tried by pulling the repo to azure function folder but that didn't work either becuase my routes didn't work as expected. I use MVC architecture in my app. My run.py looks like
import falcon
from wsgiref import simple_server
from project.routes import *
if __name__ == "__main__":
host = '127.0.0.1'
port = 5000
httpd = simple_server.make_server(host, port, app)
print("Serving on %s:%s" % (host, port))
httpd.serve_forever()
Is there any way to deploy my app as it is or should I change the structure.
Current folder structure
Azure Functions is not an appropriate place to just place a web application and expect it to run as normal. It is a "serverless" framework, so having your app use a MVC architecture is a sign that your app is currently not a good fit for Azure Functions. In your application's current state, it is better suited for an Azure Web App.
Azure Functions apps should be built around small functions that are called in response to events. To make your application a better fit for Azure Functions, it would involve refactoring your application into individual functions, that are triggered by events such as HTTP requests, timers, and many others that can be found here.
I am building a server using Flask that will be called by a web client.
When a certain change occurs on the db my Flask app interacts with, I need the web client to be alerted so it can display the update.
Would someone be able to give me some direction as to what I should be looking into with regards to both the Flask and web client sides?
Thanks.
The terminology you are using is a little confusing. I'm going to assume the web client is someone visiting your Flask app over the internet.
Basically, if you want the ability for the server to push updates to the client you need to use websockets
http://en.wikipedia.org/wiki/WebSocket
Let's say I have a Flask app running. When someone goes to any page, or makes any sort of request on the page, I want that request to be copied to another Flask app. Is there an already existing Flask plugin that would allow me to do so?
By copy I mean this:
My app is test.com. I have another Flask app running on a private machine on a private IP. When I get a GET request on test.com, I want the same GET request to be sent to the Flask app on the private app.
As others have said in the comments, the best kind of proxy is that provided by your web server. However sometimes you actually need your web application to do the proxying, in that case see this answer: Proxying to another web service with Flask