Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottle + WebSocket

is it possible to host a normal Bottle application and a WebSocket one (example: https://github.com/defnull/bottle/blob/master/docs/async.rst) in the same application (same port)? So that /ws will go to WebSocket handler and all other will be normally routed to other bottle handlers.

like image 900
redman Avatar asked Apr 25 '12 13:04

redman


2 Answers

It sure is.

The server:

#!/usr/bin/python

import json
from bottle import route, run, request, abort, Bottle ,static_file
from pymongo import Connection
from gevent import monkey; monkey.patch_all()
from time import sleep

app = Bottle()

@app.route('/websocket')
def handle_websocket():
    wsock = request.environ.get('wsgi.websocket')
    if not wsock:
        abort(400, 'Expected WebSocket request.')
    while True:
        try:
            message = wsock.receive()
            wsock.send("Your message was: %r" % message)
            sleep(3)
            wsock.send("Your message was: %r" % message)
        except WebSocketError:
            break

@app.route('/<filename:path>')
def send_html(filename):
    return static_file(filename, root='./static', mimetype='text/html')


from gevent.pywsgi import WSGIServer
from geventwebsocket import WebSocketHandler, WebSocketError

host = "127.0.0.1"
port = 8080

server = WSGIServer((host, port), app,
                    handler_class=WebSocketHandler)
print "access @ http://%s:%s/websocket.html" % (host,port)
server.serve_forever()

The html page that holds the javascript:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <script type="text/javascript">
    var ws = new WebSocket("ws://localhost:8080/websocket");
    ws.onopen = function() {
        ws.send("Hello, world");
    };
    ws.onmessage = function (evt) {
        alert(evt.data);
    };
  </script>
</head>
<body>
</body>
</html>

A client:

#!/usr/bin/python

from websocket import create_connection
ws = create_connection("ws://localhost:8080/websocket")
print "Sending 'Hello, World'..."
ws.send("Hello, World")
print "Sent"
print "Reeiving..."
result =  ws.recv()
print "Received '%s'" % result
ws.close()
like image 97
Bryan Hunt Avatar answered Nov 17 '22 13:11

Bryan Hunt


This most likely isn't the easiest answer but I just got done setting up a test environment that works using Nginx and uWSGI over Pyramid and once you've got it setup you can extend it very very easily, for instance if your /ws is pulling too many resources, it's trivial with Nginx+uWSGI to relocate /ws to different hardware. My background is Pyramid and my uWSGI experience has only been with testing but in my reading it seems a solution that should work well. The bottle instructions were the result of a quick google search.

The Concept:

The concept is to take your app, ie your app = make_wsgi_app.from_config(config) before the app.serve_forever() call and instead use uwsgi to 'serve' your app to a socket app1.sock. There are many ways to configure uWSGI. The uWSGI site has documentation for more ways to configure uWSGI to talk to your app. Nginx comes with configuration to use uWSGI sockets natively at least in the current version. You just pass the uwsgi_pass unix:///tmp/app1.sock; path to the sites configuration along with the params. Do this twice in the same site conf file, once for location / { and once for location /ws { pointing to their respective app sock files and you should be good to go.

The concept of serving an app to a socket as file was new to me when I was setting up the testing environment, I hope this helps.

What to get:

nginx
uWSGI

HowTo:

Pull the nginx and uwsgi setup information out of this tutorial and cater it to your app, here for bottle specific setup or head over to the uwsgi site and checkout their configuration instructions. The documentation for each specific tech is pretty good so even with the lack of combined examples it wasn't difficult to get them working together. With both Nginx and uWSGI there are a huge number of configuration settings so be sure to take a look at those as well.
like image 39
Melignus Avatar answered Nov 17 '22 13:11

Melignus