heroku-python / flask-sockets

[DEPRECATED] Alternative: https://github.com/miguelgrinberg/flask-sock
MIT License
1.74k stars 164 forks source link

The example code gives me an error? #8

Closed kramer65 closed 8 years ago

kramer65 commented 10 years ago

I'm trying to use Flask-Sockets with the example code:

sockets = Sockets(app)

@sockets.route('/echo')
def echo_socket(ws):
    while True:
        message = ws.receive()
        ws.send(message)

Unfortunately it gives me an error saying:

    File "/Library/Python/2.7/site-packages/Flask-0.10-py2.7.egg/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
    File "/Library/Python/2.7/site-packages/flask_sockets.py", line 37, in __call__
    environment = environ['wsgi.websocket']
    KeyError: 'wsgi.websocket'

Anybody any ideas what I'm doing wrong? All tips are welcome!

mbildner commented 10 years ago

Do you have gevent installed and working on your system?

kramer65 commented 10 years ago

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.

mbildner commented 10 years ago

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!

d0ugal commented 10 years ago

This would make a great addition to the README.

mbildner commented 10 years ago

submitted a pull request.

eranimo commented 10 years ago

Is there any possible way to use websockets with app.run? gunicorn doesn't provide the same debugging features as flask, which is bad.

mbildner commented 10 years ago

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

eranimo commented 10 years ago

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.

aodj commented 10 years ago

Also, gunicorn is Unix only

adamlwgriffiths commented 10 years ago

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()
jasdevsidhu commented 9 years ago

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():

print "in the function"

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.run(debug=True, port=80, host='0.0.0.0')

app.debug=True
#app.run()
SocketIOServer(('0.0.0.0', 5000), app,resource="socket.io").serve_forever()
print "successfull listen to socket"
kennethreitz commented 8 years ago

@jasdevsidhu you must install the module in order to use it :)