Autobahn|Python Reference

Release v0.8.9. (Changelog)

Autobahn|Python is a subproject of Autobahn and provides open-source implementations of

in Python 2 and 3, running on Twisted and asyncio.

WebSocket allows bidirectional real-time messaging on the Web.

WAMP implements two messaging patterns on top of WebSocket:

  • Publish & Subscribe: Publishers publish events to a topic, and subscribers to the topic receive these events. A router brokers these events.
  • Remote Procedure Calls: A callee registers a remote procedure with a router. A caller makes a call for that procedure to the router. The router deals the call to the callee and returns the result to the caller.

Basic router functionality is provided by Autobahn|Python.

WAMP is ideal for distributed, multi-client and server applications, such as multi-user database-drive business applications, sensor networks (IoT), instant messaging or MMOGs (massively multi-player online games) .

WAMP enables application architectures with application code distributed freely across processes and devices according to functional aspects. Since WAMP implementations exist for multiple languages, WAMP applications can be polyglott. Application components can be implemented in a language and run on a device which best fit the particular use case.

Show me some code

Using Autobahn|Python you can create both clients and servers in Python speaking just plain WebSocket or WAMP.

WebSocket

A sample WebSocket server:

class MyServerProtocol(WebSocketServerProtocol):

   def onConnect(self, request):
      print("Client connecting: {}".format(request.peer))

   def onOpen(self):
      print("WebSocket connection open.")

   def onMessage(self, payload, isBinary):
      if isBinary:
         print("Binary message received: {} bytes".format(len(payload)))
      else:
         print("Text message received: {}".format(payload.decode('utf8')))

      ## echo back message verbatim
      self.sendMessage(payload, isBinary)

   def onClose(self, wasClean, code, reason):
      print("WebSocket connection closed: {}".format(reason))

Complete example code:

WAMP

A sample WAMP application component implementing all client roles:

class MyComponent(ApplicationSession):

   def onConnect(self):
      self.join("realm1")


   @inlineCallbacks
   def onJoin(self, details):

      # 1) subscribe to a topic
      def onevent(msg):
         print("Got event: {}".format(msg))

      yield self.subscribe(onevent, 'com.myapp.hello')

      # 2) publish an event
      self.publish('com.myapp.hello', 'Hello, world!')

      # 3) register a procedure for remoting
      def add2(x, y):
         return x + y

      self.register(add2, 'com.myapp.add2');

      # 4) call a remote procedure
      res = yield self.call('com.myapp.add2', 2, 3)
      print("Got result: {}".format(res))

Complete example code:

  • server, which provides a remote procedure enpoint and publishes to a topic - Twisted - asyncio
  • client, which calls the procedure and subscribes to the topic - Twisted - asyncio

There are many more examples showing options and advanced features, listed on the example overview page.

Note

  • WAMP application components can be run in servers and clients without any modification to your component class.
  • AutobahnJS allows you to write WAMP application components in JavaScript which run in browsers and Nodejs. Here is how above example looks like in JavaScript.

Features

Python support

Support for Autobahn|Python on Twisted and asyncio is as follows:

Python Twisted asyncio Notes
CPython 2.6 yes yes asyncio support via trollius
CPython 2.7 yes yes asyncio support via trollius
CPython 3.3 yes yes asyncio support via tulip
CPython 3.4+ yes yes asyncio in the standard library
PyPy 2.2+ yes yes asyncio support via trollius
Jython 2.7+ yes ? Issues: 1, 2

WebSocket - implemented on both Twisted and asyncio

WAMP - currently only implented on Twisted

Where to go from here

Overview of Examples lists code examples covering a broader range of uses cases and advanced WAMP features.

Programming with WebSocekt explains all you need to know about using Autobahn|Python as a WebSocket library, and includes a full reference for the relevant parts of the API.

Programming with WAMP gives an introduction for programming with WAMP (v2) in Python using Autobahn|Python.

API Reference contains the full API reference for both WebSocket and WAMP.

Autobahn|Python

Autobahn|Python is a subproject of Autobahn and provides open-source implementations of The WebSocket Protocol and the The Web Application Messaging Protocol (WAMP) in Python running on Twisted and asyncio.