Closed kramer65 closed 8 years ago
Do you have gevent installed and working on your system?
Yes:
kramer65s-laptop:~ kramer65$ sudo pip install --upgrade gevent
Password:
Requirement already up-to-date: gevent in /Library/Python/2.7/site-packages
Requirement already up-to-date: greenlet in /Library/Python/2.7/site-packages (from gevent)
Cleaning up...
Any other ideas maybe? I would really like to use this.
How are you serving your app?
My guess is that you're running it on the dev server included in Flask:
app.run(host="localhost", port=8000)
If so, that's probably your problem. The server included in Werkzeug/Flask won't handle giving your app a websocket interface, but it will still try to serve on the route you've given it, meaning that when you try to access the websocket object in Python, you'll get an error saying that it doesn't exist (ie you'll get a KeyError when Flask tries to find it in the environment dictionary it sets up to handle the client's request).
What you want to do instead is to serve the app using some server that is capable of handling this insertion. To do this, the module includes a Gunicorn worker class, which is awesome.
Here's how to do that:
First, make sure you have Gunicorn installed:
pip install gunicorn
Second, from the directory with your app, run this in the command line:
gunicorn -k flask_sockets.worker -b 127.0.0.1:8000 my_super_cool_app:app
sub in whatever your app is named without the .py
extension.
After that, as long as you can handle the client side of setting up a websocket, you should have delicious websockets serving out of whatever route you specified inside the app.
Hope that helps!
This would make a great addition to the README.
submitted a pull request.
Is there any possible way to use websockets with app.run? gunicorn doesn't provide the same debugging features as flask, which is bad.
What exactly are you trying to do that doesn't work / do you have the code up on GitHub?
On Saturday, January 11, 2014, Kaelan Cooter wrote:
Is there any possible way to use websockets with app.run? gunicorn doesn't provide the same debugging features as flask, which is bad.
— Reply to this email directly or view it on GitHubhttps://github.com/kennethreitz/flask-sockets/issues/8#issuecomment-32115116 .
Moshe Bildner Moshe.Bildner@gmail.com
I'm using gunicorn, as described in the code above above. It doesn't call the flask debugging pages or offer the console reporting that the build-in flask app.run() method provides.
Also, gunicorn is Unix only
flask-sockets uses gevent-websockets which includes its own WSGI server.
The following code is a near drop in replacement for app.run(host='0.0.0.0')
The only problem is you don't get the werkzeug exception helpers and what not.
No extra dependencies though =)
from gevent.pywsgi import WSGIServer
from geventwebsocket.handler import WebSocketHandler
server = WSGIServer(("0.0.0.0", 5000), app, handler_class=WebSocketHandler)
server.serve_forever()
Guys I am getting this error , help me out
/home/www/flask_project# gunicorn -k flask_sockets.worker main2:app
Error: class uri 'flask_sockets.worker' invalid or not found:
[Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/gunicorn/util.py", line 140, in load_class mod = import_module('.'.join(components)) File "/usr/lib/python2.7/importlib/init.py", line 37, in import_module import(name) ImportError: No module named flask_sockets ]
This is my main2.py
from gevent import monkey;monkey.patch_all() from flask import Flask,render_template, url_for, request, redirect, flash,jsonify,session,Markup from socketio import socketio_manage from socketio.namespace import BaseNamespace from socketio.server import SocketIOServer
app=Flask(name) app.config['SECRET_KEY'] = 'secret!'
class ChatNamespace(BaseNamespace): def recv_connect(self): print "successfully connected" self.emit('show_result','successfully connect') def on_receive_message(self,msg): print "message is "+msg["data"] self.emit('show_result2',msg["data"]) @app.route('/') def index():
return render_template('index.html')
@app.route("/socket.io/path:path") def run_socketio(path): socketio_manage(request.environ, {'/test': ChatNamespace}) return 'ok'
if name=='main':
app.debug=True
#app.run()
SocketIOServer(('0.0.0.0', 5000), app,resource="socket.io").serve_forever()
print "successfull listen to socket"
@jasdevsidhu you must install the module in order to use it :)
I'm trying to use Flask-Sockets with the example code:
Unfortunately it gives me an error saying:
Anybody any ideas what I'm doing wrong? All tips are welcome!