Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSockets and WSGI together through Gunicorn

Is it possible to use Gunicorn to handle WebSockets and regular WSGI views together in one (Flask) app?

I know how to get websockets to work using the Gevent WSGI server, and I can get a regular WSGI app running with Gunicorn, with gevent workers, but when I try to serve the two together from one app using Gunicorn I get an error:

ValueError: View function did not return a response

Is it possible to serve the two from one app, using gunicorn? I plan eventually to put this all behind nginx, and I'm not averse to splitting the socket into another app and having the two communicate, as long as that doesn't demand too many additional system resources. Until then, is there a way to do it this way?

EDIT:

I figured out how to get this working. The key is 1) change the logging function for gevent and 2) make sure to specify to gunicorn that I'm using geventWebSocketWorker class workers.

I found part of this answer on this site: http://d.hatena.ne.jp/Malan/20121007

For the record, I think it's probably a better idea to have one server running tornado/twisted/autobahn(thanks Jordan) and another running my WSGI stuff. But that's not what I wanted here :)

def log_request(self):
    log = self.server.log
    if log:
        if hasattr(log, "info"):
            log.info(self.format_request() + '\n')
        else:
            log.write(self.format_request() + '\n')

import gevent        
gevent.pywsgi.WSGIHandler.log_request = log_request
from geventwebsocket.handler import WebSocketHandler
from gevent.pywsgi import WSGIServer

sudo gunicorn -c gunicorn_config.py -k     "geventwebsocket.gunicorn.workers.GeventWebSocketWorker" router:app       
like image 966
Moshe Bildner Avatar asked Jul 13 '13 23:07

Moshe Bildner


People also ask

Does Gunicorn support WebSocket?

You can just keep running your Django code as a WSGI app if you like, behind something like uwsgi or gunicorn; this won't let you support WebSockets, though, so you'll need to run a separate interface server to terminate those connections and configure routing in front of your interface and WSGI servers to route ...

Does Wsgi support WebSockets?

As of version 0.9. 13, eventlet. websocket supports SSL websockets; all that's necessary is to use an SSL wsgi server. The web socket spec is still under development, and it will be necessary to change the way that this module works in response to spec changes.

Can WebSockets send and receive at the same time?

The key word in that definition is two-way: with WebSocket, both the client and the server can trigger communication with one another, and both can send messages, at the same time.

Does flask support WebSockets?

Flask, being a minimalist web framework, does not have WebSocket support built-in. The old Flask-Sockets extension, which has not been maintained in the last few years, provided support for this protocol.


1 Answers

Flask-Sockets might be helpful.

like image 123
Andrew Avatar answered Nov 06 '22 09:11

Andrew