Combining WAMP and Web - Twisted Klein and Autobahn

#1

Hi,

the latest AutobahnPython comes with a new Flask-like API for writing WAMP applications:

https://github.com/tavendo/AutobahnPython/tree/master/examples/twisted/wamp/app/hello

The API is actually just a thin wrapper over the underlying wamp.ApplicationSession, but looks and feels very Flask-esque, which can lower the bar for new users.

The API is (obviously) useful for pure WAMP apps, but how would you actually mix above with Flask if you want to do both WAMP and Web stuff?

The core issue is that Flask (as Django and others) are written on top of a fundamentally blocking API: WSGI.

There are a couple of approaches to work around this like running Flask on a background worker thread pool or using a HTTP-WAMP bridge.

Here is another approach: use Twisted Klein, which essentially is Flask ported to Twisted Web.

This allows you to really use asynchronous code within Web routes. E.g. you can call a WAMP remote procedure upon receiving a HTTP form:

https://github.com/tavendo/AutobahnPython/tree/master/examples/twisted/wamp/app/klein

In a way, this is the cleanest approach of all. First-class asynchronous code for both Web and WAMP. However, you will need to write your Web code on top of Klein.

Cheers,
/Tobias

Note: The example runs WAMP and Web on different ports. This could be modified to run on one port also.

0 Likes

#2

This is a great example and actually you should blog about it.

···

On Saturday, June 28, 2014 7:37:20 PM UTC+7, Tobias Oberstein wrote:

Hi,

the latest AutobahnPython comes with a new Flask-like API for writing
WAMP applications:

https://github.com/tavendo/AutobahnPython/tree/master/examples/twisted/wamp/app/hello

The API is actually just a thin wrapper over the underlying
wamp.ApplicationSession, but looks and feels very Flask-esque, which can
lower the bar for new users.

The API is (obviously) useful for pure WAMP apps, but how would you
actually mix above with Flask if you want to do both WAMP and Web stuff?

The core issue is that Flask (as Django and others) are written on top
of a fundamentally blocking API: WSGI.

There are a couple of approaches to work around this like running Flask
on a background worker thread pool or using a HTTP-WAMP bridge.

Here is another approach: use Twisted Klein, which essentially is Flask
ported to Twisted Web.

This allows you to really use asynchronous code within Web routes. E.g.
you can call a WAMP remote procedure upon receiving a HTTP form:

https://github.com/tavendo/AutobahnPython/tree/master/examples/twisted/wamp/app/klein

In a way, this is the cleanest approach of all. First-class asynchronous
code for both Web and WAMP. However, you will need to write your Web
code on top of Klein.

Cheers,

/Tobias

Note: The example runs WAMP and Web on different ports. This could be
modified to run on one port also.

0 Likes

#3

Hello Tobias,

I have tried the ‘klein’ server_web.py, I got this error

2014-07-13 19:51:18+0200 [-] Starting factory <twisted.web.server.Site instance at 0x7f9bd26e0098>
2014-07-13 19:51:18+0200 [-] Starting factory <autobahn.twisted.websocket.WampWebSocketClientFactory instance at 0x7f9bd26e0d88>
2014-07-13 19:51:18+0200 [Uninitialized] Stopping factory <autobahn.twisted.websocket.WampWebSocketClientFactory instance at 0x7f9bd26e0d88>
2014-07-13 19:51:26+0200 [HTTPChannel,0,127.0.0.1] Unhandled Error
Traceback (most recent call last):
File “/home/salvatore/apps/flask/tutor/local/lib/python2.7/site-packages/klein/app.py”, line 87, in execute_endpoint
return endpoint_f(self._instance, *args, **kwargs)
File “/home/salvatore/apps/flask/tutor/local/lib/python2.7/site-packages/klein/app.py”, line 172, in _f
return _call(instance, f, request, *a, **kw)
File “/home/salvatore/apps/flask/tutor/local/lib/python2.7/site-packages/klein/app.py”, line 27, in _call
return f(*args, **kwargs)
File “/home/salvatore/apps/flask/tutor/local/lib/python2.7/site-packages/twisted/internet/defer.py”, line 1237, in unwindGenerator
return _inlineCallbacks(None, gen, Deferred())
— —
File “/home/salvatore/apps/flask/tutor/local/lib/python2.7/site-packages/twisted/internet/defer.py”, line 1099, in _inlineCallbacks
result = g.send(result)
File “server_web.py”, line 31, in square_submit
res = yield wampapp.session.call(‘com.example.square’, x)
exceptions.AttributeError: ‘NoneType’ object has no attribute ‘call’

···

Le samedi 28 juin 2014 14:37:20 UTC+2, Tobias Oberstein a écrit :

Hi,

the latest AutobahnPython comes with a new Flask-like API for writing
WAMP applications:

https://github.com/tavendo/AutobahnPython/tree/master/examples/twisted/wamp/app/hello

The API is actually just a thin wrapper over the underlying
wamp.ApplicationSession, but looks and feels very Flask-esque, which can
lower the bar for new users.

The API is (obviously) useful for pure WAMP apps, but how would you
actually mix above with Flask if you want to do both WAMP and Web stuff?

The core issue is that Flask (as Django and others) are written on top
of a fundamentally blocking API: WSGI.

There are a couple of approaches to work around this like running Flask
on a background worker thread pool or using a HTTP-WAMP bridge.

Here is another approach: use Twisted Klein, which essentially is Flask
ported to Twisted Web.

This allows you to really use asynchronous code within Web routes. E.g.
you can call a WAMP remote procedure upon receiving a HTTP form:

https://github.com/tavendo/AutobahnPython/tree/master/examples/twisted/wamp/app/klein

In a way, this is the cleanest approach of all. First-class asynchronous
code for both Web and WAMP. However, you will need to write your Web
code on top of Klein.

Cheers,

/Tobias

Note: The example runs WAMP and Web on different ports. This could be
modified to run on one port also.

0 Likes

#4

mmh, works for me .. see screenshot

/Tobias

···

Am 13.07.2014 19:55, schrieb Salvatore DI DIO:

Hello Tobias,

I have tried the 'klein' server_web.py, I got this error

0 Likes

#5

Thanks Tobias,

When I launch it all is ok.

The problem occurs when I make a request,.

Salvatore

···

Le mardi 15 juillet 2014 14:01:12 UTC+2, Tobias Oberstein a écrit :

Am 13.07.2014 19:55, schrieb Salvatore DI DIO:

Hello Tobias,

I have tried the ‘klein’ server_web.py, I got this error

mmh, works for me … see screenshot

/Tobias

0 Likes