This guide gives an introduction to programming with WAMP in Python using Autobahn.
WAMP provides two communication patterns for application components to talk to each other
and we will cover all four interactions involved in above patterns
Tip
If you are new to WAMP or want to learn more about the design principles behind WAMP, we have a longer text here.
WAMP is all about creating systems from loosely coupled application components. It’s application components where your application specific code runs.
A WAMP based system consists of potentially many application components, which all connect to a WAMP router. The router is generic, which means, it does not run any application code, but only provides routing of events and calls.
Hence, to create a WAMP application, you
You create an application component by deriving from a base class provided by Autobahn.
When using Twisted, you derive from autobahn.twisted.wamp.ApplicationSession
from autobahn.twisted.wamp import ApplicationSession
class MyComponent(ApplicationSession):
def onJoin(self, details):
print("session ready")
whereas when you are using asyncio, you derive from autobahn.asyncio.wamp.ApplicationSession
from autobahn.asyncio.wamp import ApplicationSession
class MyComponent(ApplicationSession):
def onJoin(self, details):
print("session ready")
As can be seen, the only difference between Twisted and asyncio is the import (line 1). The rest of the code is identical.
Also, Autobahn will invoke callbacks on your application component when certain events happen. For example, autobahn.wamp.interfaces.ISession.onJoin()
is triggered when the WAMP session has connected to a router and joined a realm. We’ll come back to this topic later.
To actually make use of an application components, the component needs to connect to a WAMP router. Autobahn includes a runner that does the heavy lifting for you.
Here is how you use autobahn.twisted.wamp.ApplicationRunner
with Twisted
from autobahn.twisted.wamp import ApplicationRunner
runner = ApplicationRunner(url = u"ws://localhost:8080/ws", realm = u"realm1")
runner.run(MyComponent)
and here is how you use autobahn.asyncio.wamp.ApplicationRunner
with asyncio
from autobahn.asyncio.wamp import ApplicationRunner
runner = ApplicationRunner(url = u"ws://localhost:8080/ws", realm = u"realm1")
runner.run(MyComponent)
As can be seen, the only difference between Twisted and asyncio is the import (line 1). The rest of the code is identical.
There are two mandatory arguments to ApplicationRunner
:
url
: the WebSocket URL of the WAMP router (for WAMP-over-WebSocket)realm
: the Realm the component should join on that routerTip
A Realm is a routing namespace and an administrative domain for WAMP. For example, a single WAMP router can manage multiple Realms, and those realms are completely separate: an event published to topic T on a Realm R1 is NOT received by a subscribe to T on Realm R2.
Here are quick templates for you to copy/paste for creating and running a WAMP component.
Twisted:
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner
class MyComponent(ApplicationSession):
def onJoin(self, details):
print("session joined")
if __name__ == '__main__':
runner = ApplicationRunner(url = u"ws://localhost:8080/ws", realm = u"realm1")
runner.run(MyComponent)
asyncio:
from autobahn.asyncio.wamp import ApplicationSession, ApplicationRunner
class MyComponent(ApplicationSession):
def onJoin(self, details):
print("session joined")
if __name__ == '__main__':
runner = ApplicationRunner(url = u"ws://localhost:8080/ws", realm = u"realm1")
runner.run(MyComponent)
The component we’ve created attempts to connect to a WAMP router running locally which accepts connections on port 8080
, and for a realm realm1
.
Our suggested way is to use Crossbar.io as your WAMP router.
Tip
There are other WAMP routers besides Crossbar.io as well. Please see this list.
Once you’ve installed Crossbar.io, initialize an instance of it with the default settings, which will accept WAMP (over WebSocket) connections on ws://<hostname>:8080/ws
and has a realm1
pre-configured.
To do this, do
crossbar init
This will create the default Crossbar.io node configuration .crossbar/config.json
. You can then start Crossbar.io by doing
crossbar start
Remote Procedure Call (RPC) is a messaging pattern involving peers of three roles:
A Caller issues calls to remote procedures by providing the procedure URI and any arguments for the call. The Callee will execute the procedure using the supplied arguments to the call and return the result of the call to the Caller.
Callees register procedures they provide with Dealers. Callers initiate procedure calls first to Dealers. Dealers route calls incoming from Callers to Callees implementing the procedure called, and route call results back from Callees to Callers.
The Caller and Callee will usually run application code, while the Dealer works as a generic router for remote procedure calls decoupling Callers and Callees.
To make a procedure available for remote calling, the procedure needs to be registered. Registering a procedure is done by calling autobahn.wamp.interfaces.ICallee.register()
from a session.
Here is an example using Twisted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | from autobahn.twisted.wamp import ApplicationSession
from twisted.internet.defer import inlineCallbacks
class MyComponent(ApplicationSession):
@inlineCallbacks
def onJoin(self, details):
print("session ready")
def add2(x, y):
return x + y
try:
yield self.register(add2, u'com.myapp.add2')
print("procedure registered")
except Exception as e:
print("could not register procedure: {0}".format(e))
|
The procedure add2
is registered (line 14) under the URI u"com.myapp.add2"
immediately in the onJoin
callback which fires when the session has connected to a Router and joined a Realm.
Tip
You can register local functions like in above example, global functions as well as methods on class instances. Further, procedures can also be automatically registered using decorators.
When the registration succeeds, authorized callers will immediately be able to call the procedure (see Calling Procedures) using the URI under which it was registered (u"com.myapp.add2"
).
A registration may also fail, e.g. when a procedure is already registered under the given URI or when the session is not authorized to register procedures.
Using asyncio, the example looks like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | from autobahn.asyncio.wamp import ApplicationSession
from asyncio import coroutine
class MyComponent(ApplicationSession):
@coroutine
def onJoin(self, details):
print("session ready")
def add2(x, y):
return x + y
try:
yield from self.register(add2, u'com.myapp.add2')
print("procedure registered")
except Exception as e:
print("could not register procedure: {0}".format(e))
|
The differences compared with the Twisted variant are:
import
of ApplicationSession
@coroutine
to decorate co-routinesyield from
instead of yield
Calling a procedure (that has been previously registered) is done using autobahn.wamp.interfaces.ICaller.call()
.
Here is how you would call the procedure add2
that we registered in Registering Procedures under URI com.myapp.add2
in Twisted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from autobahn.twisted.wamp import ApplicationSession
from twisted.internet.defer import inlineCallbacks
class MyComponent(ApplicationSession):
@inlineCallbacks
def onJoin(self, details):
print("session ready")
try:
res = yield self.call(u'com.myapp.add2', 2, 3)
print("call result: {}".format(res))
except Exception as e:
print("call error: {0}".format(e))
|
And here is the same done on asyncio
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from autobahn.asyncio.wamp import ApplicationSession
from asyncio import coroutine
class MyComponent(ApplicationSession):
@coroutine
def onJoin(self, details):
print("session ready")
try:
res = yield from self.call(u'com.myapp.add2', 2, 3)
print("call result: {}".format(res))
except Exception as e:
print("call error: {0}".format(e))
|
Publish & Subscribe (PubSub) is a messaging pattern involving peers of three roles:
A Publishers publishes events to topics by providing the topic URI and any payload for the event. Subscribers of the topic will receive the event together with the event payload.
Subscribers subscribe to topics they are interested in with Brokers. Publishers initiate publication first at Brokers. Brokers route events incoming from Publishers to Subscribers that are subscribed to respective topics.
The Publisher and Subscriber will usually run application code, while the Broker works as a generic router for events decoupling Publishers from Subscribers.
To receive events published to a topic, a session needs to first subscribe to the topic.
Subscribing to a topic is done by calling autobahn.wamp.interfaces.ISubscriber.subscribe()
.
Here is a Twisted example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | from autobahn.twisted.wamp import ApplicationSession
from twisted.internet.defer import inlineCallbacks
class MyComponent(ApplicationSession):
@inlineCallbacks
def onJoin(self, details):
print("session ready")
def oncounter(count):
print("event received: {0}", count)
try:
yield self.subscribe(oncounter, u'com.myapp.oncounter')
print("subscribed to topic")
except Exception as e:
print("could not subscribe to topic: {0}".format(e))
|
We create an event handler function oncounter
(you can name that as you like) which will get called whenever an event for the topic is received.
To subscribe (line 15), we provide the event handler function (oncounter
) and the URI of the topic to which we want to subscribe (u'com.myapp.oncounter'
).
When the subscription succeeds, we will receive any events published to u'com.myapp.oncounter'
. Note that we won’t receive events published before the subscription succeeds.
The corresponding asyncio code looks like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | from autobahn.asyncio.wamp import ApplicationSession
from asyncio import coroutine
class MyComponent(ApplicationSession):
@coroutine
def onJoin(self, details):
print("session ready")
def oncounter(count):
print("event received: {0}", count)
try:
yield from self.subscribe(oncounter, u'com.myapp.oncounter')
print("subscribed to topic")
except Exception as e:
print("could not subscribe to topic: {0}".format(e))
|
Again, nearly identical to Twisted.
Publishing an event to a topic is done by calling autobahn.wamp.interfaces.IPublisher.publish()
.
Events can carry arbitrary positional and keyword based payload - as long as the payload is serializable in JSON.
Here is a Twisted example that will publish an event to topic u'com.myapp.oncounter'
with a single (positional) payload being a counter that is incremented for each publish
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from autobahn.twisted.wamp import ApplicationSession
from autobahn.twisted.util import sleep
from twisted.internet.defer import inlineCallbacks
class MyComponent(ApplicationSession):
@inlineCallbacks
def onJoin(self, details):
print("session ready")
counter = 0
while True:
self.publish(u'com.myapp.oncounter', counter)
counter += 1
yield sleep(1)
|
The corresponding asyncio code looks like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from autobahn.asyncio.wamp import ApplicationSession
from asyncio import sleep
from asyncio import coroutine
class MyComponent(ApplicationSession):
@coroutine
def onJoin(self, details):
print("session ready")
counter = 0
while True:
self.publish(u'com.myapp.oncounter', counter)
counter += 1
yield from sleep(1)
|
Tip
By default, a publisher will not receive an event it publishes even when the publisher is itself subscribed to the topic subscribed to. This behavior can be overridden.
Tip
By default, publications are unacknowledged. This means, a publish()
may fail silently (like when the session is not authorized to publish to the given topic). This behavior can be overridden.
A WAMP application component has this lifecycle:
The ApplicationSession will fire the following events which you can handle by overriding the respective method (see autobahn.wamp.interfaces.ISession
for more information):
class MyComponent(ApplicationSession):
def __init__(self, config = None):
ApplicationSession.__init__(self, config)
print("component created")
def onConnect(self):
print("transport connected")
self.join(self.config.realm)
def onChallenge(self, challenge):
print("authentication challenge received")
def onJoin(self, details):
print("session joined")
def onLeave(self, details):
print("session left")
def onDisconnect(self):
print("transport disconnected")
Starting with release 0.8.0, Autobahn now supports WAMP v2, and also support both Twisted and asyncio. This required changing module naming for WAMP v1 (which is Twisted only).
Hence, WAMP v1 code for Autobahn < 0.8.0
from autobahn.wamp import WampServerFactory
should be modified for Autobahn >= 0.8.0 for (using Twisted)
from autobahn.wamp1.protocol import WampServerFactory
Warning
WAMP v1 will be deprecated with the 0.9 release of Autobahn which is expected in Q4 2014.
Starting with release 0.9.4, all WAMP router code in Autobahn has been split out and moved to Crossbar.io. Please see the announcement here.