8

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, when simply going to the url /echo using my browser 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!

[EDIT] @jbub - Thanks for the tips! So to start off I now use gunicorn instead of the built-in dev-server. So I started it using gunicorn -k flask_sockets.worker -b 0.0.0.0:5000 main:app. I then inserted the code below in my views.py i nwhich the echo_test.html is the code you supplied. When I now visit /echo_test, I indeed get a prompt saying "socket closed".

sockets = Sockets(app)

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

@app.route('/echo_test', methods=['GET'])
def echo_test():
    return render_template('echo_test.html')

But let's say my aim is to have a word (randomly chosen from a list) on a page which gets updated with other values randomly chosen from the list. Would you have any tips on achieving that?

kramer65
  • 50,427
  • 120
  • 308
  • 488
  • 1
    Are you running behind any http server ? Do you have libevent, gevent and gevent-websocket installed ? – jbub Nov 20 '13 at 16:33
  • 1
    @jbub - I'm running both apache and the Flask dev-server. But as far as I know, they don't get in each others way. All the other things I run from the Flask server have no problems. Also, I just added to the question that I'm simply visiting the url with my browser, I suppose that is the way to start right? Any other tips are welcome since I'm pretty desperate.. – kramer65 Nov 20 '13 at 16:47
  • Note to other people running into this problem. Make sure that your port is correct. In most cases the port should be 8000 NOT 5000. – High schooler Jan 17 '14 at 04:06
  • You might find this [question](http://stackoverflow.com/questions/19899578/how-do-i-get-this-websocket-example-to-work-with-flask) useful. – John Jan 19 '14 at 21:56

3 Answers3

9

Ah, thats the problem, you cant just visit the websocket endpoint with regular GET request, that way the wsgi.websocket will not be set to environ.

Also use gunicorn not the dev server, it comes with preconfigured worker:

# install from pip
pip install gunicorn

# run app located in test.py module (in test.py directory)
gunicorn -k flask_sockets.worker test:app

I made quick example here, be sure to update the address and port to match your setup.

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript">
       var ws = new WebSocket("ws://localhost:8000/echo");
       ws.onopen = function() {
           ws.send("socket open");
       };
       ws.onclose = function(evt) {
           alert("socket closed");
       };
    </script>
  </head>
</html>

That way the browser sends a request to the server, indicating that it wants to switch protocols from HTTP to WebSocket.

Feel free to read some more about websockets here:

Community
  • 1
  • 1
jbub
  • 2,586
  • 23
  • 23
  • Thanks for the tips! I now see the alert. I just wonder; how do I update something (like a word) so that I replace it with new values? Any tips on that? – kramer65 Nov 21 '13 at 11:14
  • if you want to start sending messages, feel free to check this example out https://github.com/heroku-examples/python-websockets-chat/blob/master/chat.py, its based on flask-sockets :) – jbub Nov 21 '13 at 20:34
  • Is there a way to do it without Gunicorn and only flask? It seems like a hassle to run another process. – High schooler Dec 31 '13 at 01:18
  • Unfortunately werkzeug development server (which Flask uses) cannot provide the WSGI environ with a websocket interface, so you will have to use the provided one. – jbub Dec 31 '13 at 13:26
2

It seems that Flask-Sockets does not provided a socket server so you either have to set up nginx to proxy web sockets, run your app using gunicorn or create a socket server yourself.

I found this helpful https://gist.github.com/lrvick/1185629

rosterloh
  • 800
  • 5
  • 5
1

If using AWS, I found that sometimes need to edit the security group so that port 80 (and 443) are of type 'Custom TCP Rule' and not HTTP (and HTTPS)

vim
  • 845
  • 16
  • 13