8.1 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	Reference
Release v. (Changelog)
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 .
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 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:
There are many more examples showing options and advanced features,
listed on the example overview page <examples>.
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
- framework for WebSocket / WAMP clients and servers
 - compatible with Python 2.6, 2.7, 3.3 and 3.4
 - runs on CPython, PyPy and Jython
 - runs under Twisted and asyncio
 - implements WebSocket RFC6455, Draft Hybi-10+ and Hixie-76
 - implements WebSocket compression
 - implements WAMPv1 and WAMPv2
 - high-performance, fully asynchronous implementation
 - best-in-class standards conformance (100% strict passes with AutobahnTestsuite)
 - message-, frame- and streaming-APIs for WebSocket
 - supports TLS (secure WebSocket) and proxies
 - Open-source (Apache 2 license)
 
Python support
Support for 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
examples lists code
examples covering a broader range of uses cases and advanced WAMP
features.
websocketprogramming
explains all you need to know about using as a WebSocket library, and
includes a full reference for the relevant parts of the API.
wampprogramming
gives an introduction for programming with WAMP (v2) in Python using
.
reference contains
the full API reference for both WebSocket and WAMP.
installation examples websocketprogramming wampprogramming reference changelog table_of_contents