autobahn.twisted.wamp


Module Contents

Classes

ApplicationSession

WAMP application session for Twisted-based applications.

ApplicationSessionFactory

WAMP application session factory for Twisted-based applications.

ApplicationRunner

This class is a convenience tool mainly for development and quick hosting

Application

A WAMP application. The application object provides a simple way of

Service

A WAMP application as a twisted service.

Session

shim that lets us present pep8 API for user-classes to override,

class autobahn.twisted.wamp.ApplicationSession(config: Optional[autobahn.wamp.types.ComponentConfig] = None)[source]

Bases: autobahn.wamp.protocol.ApplicationSession

WAMP application session for Twisted-based applications.

Implements:

log
class autobahn.twisted.wamp.ApplicationSessionFactory(config=None)[source]

Bases: autobahn.wamp.protocol.ApplicationSessionFactory

WAMP application session factory for Twisted-based applications.

session :ApplicationSession

The application session class this application session factory will use. Defaults to autobahn.twisted.wamp.ApplicationSession.

log
class autobahn.twisted.wamp.ApplicationRunner(url: str, realm: Optional[str] = None, extra: Optional[Dict[str, Any]] = None, serializers: Optional[List[autobahn.wamp.interfaces.ISerializer]] = None, ssl: Optional[twisted.internet.ssl.CertificateOptions] = None, proxy: Optional[Dict[str, Any]] = None, headers: Optional[Dict[str, Any]] = None, websocket_options: Optional[Dict[str, Any]] = None, max_retries: Optional[int] = None, initial_retry_delay: Optional[float] = None, max_retry_delay: Optional[float] = None, retry_delay_growth: Optional[float] = None, retry_delay_jitter: Optional[float] = None)[source]

Bases: object

This class is a convenience tool mainly for development and quick hosting of WAMP application components.

It can host a WAMP application component in a WAMP-over-WebSocket client connecting to a WAMP router.

log
stop()[source]

Stop reconnecting, if auto-reconnecting was enabled.

run(make, start_reactor: bool = True, auto_reconnect: bool = False, log_level: str = 'info', endpoint: Optional[twisted.internet.interfaces.IStreamClientEndpoint] = None, reactor: Optional[twisted.internet.interfaces.IReactorCore] = None) Union[type(None), twisted.internet.defer.Deferred][source]

Run the application component.

Parameters
  • make – A factory that produces instances of autobahn.twisted.wamp.ApplicationSession when called with an instance of autobahn.wamp.types.ComponentConfig.

  • start_reactor – When True (the default) this method starts the Twisted reactor and doesn’t return until the reactor stops. If there are any problems starting the reactor or connect()-ing, we stop the reactor and raise the exception back to the caller.

  • auto_reconnect

  • log_level

  • endpoint

  • reactor

Returns

None is returned, unless you specify start_reactor=False in which case the Deferred that connect() returns is returned; this will callback() with an IProtocol instance, which will actually be an instance of WampWebSocketClientProtocol

class autobahn.twisted.wamp.Application(prefix=None)[source]

Bases: object

A WAMP application. The application object provides a simple way of creating, debugging and running WAMP application components.

log
__call__(config)[source]

Factory creating a WAMP application session for the application.

Parameters

config (Instance of autobahn.wamp.types.ComponentConfig) – Component configuration.

Returns

obj – An object that derives of autobahn.twisted.wamp.ApplicationSession

run(url='ws://localhost:8080/ws', realm='realm1', start_reactor=True)[source]

Run the application.

Parameters
  • url (unicode) – The URL of the WAMP router to connect to.

  • realm (unicode) – The realm on the WAMP router to join.

register(uri=None)[source]

Decorator exposing a function as a remote callable procedure.

The first argument of the decorator should be the URI of the procedure to register under.

Example

@app.register('com.myapp.add2')
def add2(a, b):
   return a + b

Above function can then be called remotely over WAMP using the URI com.myapp.add2 the function was registered under.

If no URI is given, the URI is constructed from the application URI prefix and the Python function name.

Example

app = Application('com.myapp')

# implicit URI will be 'com.myapp.add2'
@app.register()
def add2(a, b):
   return a + b

If the function yields (is a co-routine), the @inlineCallbacks decorator will be applied automatically to it. In that case, if you wish to return something, you should use returnValue:

Example

from twisted.internet.defer import returnValue

@app.register('com.myapp.add2')
def add2(a, b):
   res = yield stuff(a, b)
   returnValue(res)
Parameters

uri (unicode) – The URI of the procedure to register under.

subscribe(uri=None)[source]

Decorator attaching a function as an event handler.

The first argument of the decorator should be the URI of the topic to subscribe to. If no URI is given, the URI is constructed from the application URI prefix and the Python function name.

If the function yield, it will be assumed that it’s an asynchronous process and inlineCallbacks will be applied to it.

Example

@app.subscribe('com.myapp.topic1')
def onevent1(x, y):
   print("got event on topic1", x, y)
Parameters

uri (unicode) – The URI of the topic to subscribe to.

signal(name)[source]

Decorator attaching a function as handler for application signals.

Signals are local events triggered internally and exposed to the developer to be able to react to the application lifecycle.

If the function yield, it will be assumed that it’s an asynchronous coroutine and inlineCallbacks will be applied to it.

Current signals :

  • onjoined: Triggered after the application session has joined the

    realm on the router and registered/subscribed all procedures and event handlers that were setup via decorators.

  • onleave: Triggered when the application session leaves the realm.

@app.signal('onjoined')
def _():
   # do after the app has join a realm
Parameters

name (unicode) – The name of the signal to watch.

_fire_signal(name, *args, **kwargs)[source]

Utility method to call all signal handlers for a given signal.

Parameters

name (str) – The signal name.

class autobahn.twisted.wamp.Service(url, realm, make, extra=None, context_factory=None)[source]

Bases: twisted.application.service.MultiService

A WAMP application as a twisted service. The application object provides a simple way of creating, debugging and running WAMP application components inside a traditional twisted application

This manages application lifecycle of the wamp connection using startService and stopService Using services also allows to create integration tests that properly terminates their connections

It can host a WAMP application component in a WAMP-over-WebSocket client connecting to a WAMP router.

factory
setupService()[source]

Setup the application component.

class autobahn.twisted.wamp.Session(config: Optional[autobahn.wamp.types.ComponentConfig] = None)[source]

Bases: autobahn.wamp.protocol._SessionShim

shim that lets us present pep8 API for user-classes to override, but also backwards-compatible for existing code using ApplicationSession “directly”.

NOTE: this is not public or intended for use; you should import either autobahn.asyncio.wamp.Session or autobahn.twisted.wamp.Session depending on which async framework yo’re using.

on_welcome(welcome_msg)[source]
on_join(details)[source]
on_leave(details)[source]
on_connect()[source]
on_disconnect()[source]