In the first part of this post, we saw how to use Twisted Klein to write Flask-like Web handlers that can run asynchronous code.
We implemented a HTTP/POST handler that in turn issued an outgoing request to a Web service before completing.

Using Web services to create apps which are assembled from decoupled, interacting services or components is one way to tame complexity. It allows you to break down your app into manageable parts. Additionally, you can mix and match services implemented in different languages.

However, Web services - being based on HTTP - come with their own limitations. They are strictly request-response.

We create a app application object (line 3). As with Flask/Klein, you can name you app object anything you like.

We define a function to square numbers (line 7). We also print something when being called.

We make this function callable from other components by using the @app.register() decorator (line 6). The function will now be remoted under the URI ("name") com.example.square for others to call via WAMP.

When you open this in your browser (while watching the JavaScript console), you should see the result of 23 squared being logged. Also, the Python part will log that is was indeed being called by someone.

In our Web request handler (line 11), we extract the form variable from the HTTP/POST (line 12) and then call our remote procedure com.example.square (line 13)

Since we use a co-routine / "synchronous looking" style, we need to use inlineCallbacks, yield and returnValue

This time, the Klein app is started slightly differenty (line 24). The reason is that Klein's app.run() method will never return, since it internally starts the Twisted reactor. But we want to enter wampapp.run() (which also start the Twisted reactor).

And we run our WAMP app connecting to the development router we started above (line 25). This time we use standalone = False, so no developement router will be started in this process.

To test, run the WAMP Part from above, run this Web Part, and then open the following in your browser:

When you submit the form, our Web code (the Klein based request handler for /square/submit) will receive HTML form data via a plain old HTTP/POST.

Then, upon receiving the HTTP/POST, the request handler will perform an asynchronous call to a WAMP procedure before returning.

This is just an example. Request handlers can:

call remote procedures, using app functionality provided in other application components (both local and remote)

publish events, received by other application components (running locally, remote or in the browser)

WAMP allows to create systems from loosely coupled application components talking to each other - in real-time and in any direction.

Using Twisted Klein we can use WAMP application components directly from within Web request handlers.

The combination allows us to create powerful applications with Web and WAMP parts. Since everything is running asynchronously, we won't run into performance problems due to blocking. It'll happily handle large numbers of concurrent requests and connections.

And most importantly: we can tame complexity by building apps from components - both Web and WAMP.

Search this Site

Stay Informed

Crossbar.io and Crossbar.io Fabric

Crossbar.io is an open-source project created and managed by the people behind Crossbar.io Fabric.
Crossbar.io provides the application message routing for real-time Web and IoT applications.
Crossbar.io Fabric will support you in managing your Crossbar.io instances.