Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate flask and flask_sockets into a single app running under uwsgi

I have an existing app running uwsgi/flask behind nginx. I want to integrate websockets into the same app. Flask sockets looks promising, so I'm trying that.

Flask sockets briefly mentions running gevent-websocket, but no sample code is provided to integrate an existing app into this setup. I've also tried several combinations of gevent-websocket with my existing flask app, but I still keep getting 500s. Additionally, it would be nice if I could find out the nature of the 500s, but the errors don't show up in the uwsgi log, as they do for the existing flask app. So, if you have any debugging dips to show a tracedump, that'd be great.

Can anyone help me get a simple echo route working with this setup? Thanks in advance.

FYI, I'm currently using:

  • uWSGI 1.0.3-debian
  • python 2.7.3
  • Flask 0.10.1
  • nginx 1.4.3
  • Flask Sockets 0.1
like image 751
Homer6 Avatar asked Jan 03 '14 18:01

Homer6


People also ask

What is the name of the application in flask?

In flask the conventional name for the application is app but uWSGI looks for application by default. That's why we create an alias for our app in the last line.

How to implement WebSocket in flask?

The WebSocket’s can be implemented with all server-side technologies, I am using Flask and Socket-IO modules from Python. First of all, will start by setting up the virtual environment for our sample project. Create a project folder. Create a requirments.txt file into the project folder. Add below code into requirments.txt.

Can I use uWSGI with gevent or Eventlet?

If gevent or eventlet is used in conjunction with uwsgi, the native websocket implementation from uwsgi is used. In my case, I used socket.io on the client, which uses http polling, therefore when I tried to use uwsgi, the server expected a native websocket connection and did not have anything that handled the http polling.

What is the difference between uWSGI and native WebSockets?

If UWSGI is used, native websockets are used (ws). If gevent or eventlet is used in conjunction with uwsgi, the native websocket implementation from uwsgi is used.


1 Answers

If you upgrade to uWSGI 2.0 you get websockets support for free without the need of additional frameworks and with gevent compatibility. More important you get a really fast implementation (suitable for gaming):

http://uwsgi-docs.readthedocs.org/en/latest/WebSockets.html

like image 67
roberto Avatar answered Sep 24 '22 16:09

roberto