Autobahn Python
Autobahn Python is a networking library that provides open-source implementations of the WebSocket Protocol and the Web Application Messaging Protocol (WAMP). It allows developers to create high-performance, asynchronous WebSocket clients and servers, and WAMP clients and application components, running on either Twisted or asyncio event loops. Currently at version 25.12.2, the project maintains an active development pace with frequent nightly and development builds, alongside regular stable releases.
Warnings
- breaking Autobahn Python has dropped support for older Python versions. As of version 25.12.2, it officially requires Python >=3.11. Older versions supported Python 3.7+ (since v21.2.1) and Python 2 up to v19.11.2.
- gotcha When using asyncio in child threads or subprocesses (e.g., with `multiprocessing`), you must explicitly create and set a new event loop for each new thread/process on Unix-like systems. If you don't, you'll encounter `AssertionError: There is no current event loop in thread '...'`.
- gotcha WAMP clients (both Python and JavaScript) require a WAMP router (e.g., Crossbar.io) to mediate communication. Autobahn provides the client/component library, but not the router itself. WAMP servers should also explicitly set the realm they intend to join.
- deprecated The `autobahn.util.time_ns` helper was deprecated in favor of `txaio.time_ns` in version 20.1.3.
- deprecated The `accelerate` install variant is no longer recommended. Autobahn now includes NVX (Native Vector Extensions) for SIMD-accelerated WebSocket operations (XOR masking and UTF-8 validation), which leverages CFFI for performance.
Install
-
pip install autobahn -
pip install autobahn[all] -
pip install autobahn[asyncio,encryption] -
pip install autobahn[twisted,encryption]
Imports
- WebSocketServerProtocol
from autobahn.asyncio.websocket import WebSocketServerProtocol # or: from autobahn.twisted.websocket import WebSocketServerProtocol
- WebSocketClientProtocol
from autobahn.asyncio.websocket import WebSocketClientProtocol # or: from autobahn.twisted.websocket import WebSocketClientProtocol
- WebSocketServerFactory
from autobahn.asyncio.websocket import WebSocketServerFactory # or: from autobahn.twisted.websocket import WebSocketServerFactory
- ApplicationSession
from autobahn.asyncio.wamp import ApplicationSession # or: from autobahn.twisted.wamp import ApplicationSession
- Component
from autobahn.wamp.component import Component
Quickstart
import asyncio
from autobahn.asyncio.websocket import WebSocketServerFactory, WebSocketServerProtocol
class MyServerProtocol(WebSocketServerProtocol):
def onConnect(self, request):
print(f"Client connecting: {request.peer}")
def onOpen(self):
print("WebSocket connection open.")
def onMessage(self, payload, isBinary):
if isBinary:
print(f"Binary message received: {len(payload)} bytes")
else:
print(f"Text message received: {payload.decode('utf8')}")
# echo back message verbatim
self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
print(f"WebSocket connection closed: {reason} (code={code}, clean={wasClean})")
async def main():
factory = WebSocketServerFactory("ws://127.0.0.1:9000")
factory.protocol = MyServerProtocol
server = await asyncio.get_event_loop().create_server(factory, '127.0.0.1', 9000)
print("WebSocket server started on ws://127.0.0.1:9000")
try:
await server.serve_forever()
except KeyboardInterrupt:
pass
finally:
server.close()
await server.wait_closed()
asyncio.get_event_loop().close()
if __name__ == '__main__':
asyncio.run(main())