2. API Reference

2.1. Default API

Autobahn-Sync exposes an initialized autobahn_sync.AutobahnSync at the root of the module as a quick&easy access to the API:

import autobahn_sync
autobahn_sync.run(url=MY_ROUTER_URL, realm=MY_REALM)

@autobahn_sync.subscribe('com.app.event')
def on_event(e):
    print('%s happened' % e)

autobahn_sync.publish('com.app.event', 'trigger !')

2.2. Advanced API

With the need to connect to multiple realms/routers, the default API is not enought and you should create other instances of autobahn_sync.AutobahnSync.

class autobahn_sync.core.AutobahnSync(authmethods=None)[source]

Main class representing the AutobahnSync application

on_challenge(func)[source]

Decorator providing a callback to the onChallenge event, use this instead of subclassing autobahn.twisted.wamp.ApplicationSession.onChallenge()

Note

This decorator can only be used before AutobahnSync.run() is called given the Challenge event is triggered at this time

register(procedure=None, options=None)[source]

Decorator for the AutobahnSync.session.register()

Note

This decorator can be used before AutobahnSync.run() is called. In such case the actual registration will be done at run() time.

run(url=u'ws://127.0.0.1:8080/ws', realm=u'realm1', authmethods=None, authid=None, authrole=None, authextra=None, blocking=False, callback=None, **kwargs)[source]

Start the background twisted thread and create the WAMP connection

Parameters:blocking – If False (default) this method will spawn a new

thread that will be used to run the callback events (e.i. registered and subscribed functions). If True this method will not returns and use the current thread to run the callbacks. :param callback: This callback will be called once init is done, use it with blocking=True to put your WAMP related init

run_in_twisted(url=u'ws://127.0.0.1:8080/ws', realm=u'realm1', authmethods=None, authid=None, authrole=None, authextra=None, callback=None, **kwargs)[source]

Start the WAMP connection. Given we cannot run synchronous stuff inside the twisted thread, use this function (which returns immediately) to do the initialization from a spawned thread.

Parameters:callback – function that will be called inside the spawned thread.

Put the rest of you init (or you main loop if you have one) inside it

Parameters:
  • authmethods – Passed to autobahn.wamp.protocol.ApplicationSession.join()
  • authid – Passed to autobahn.wamp.protocol.ApplicationSession.join()
  • authrole – Passed to autobahn.wamp.protocol.ApplicationSession.join()
  • authextra – Passed to autobahn.wamp.protocol.ApplicationSession.join()

Note

This function must be called instead of AutobahnSync.run() if we are calling from twisted application (typically if we are running our application inside crossbar as a wsgi component)

session

Return the underlying session.SyncSession object if available or raise an exceptions.NotRunningError

stop()[source]

Terminate the WAMP session

Note

If the AutobahnSync.run() has been run with blocking=True, it will returns then.

subscribe(topic, options=None)[source]

Decorator for the AutobahnSync.session.subscribe()

Note

This decorator can be used before AutobahnSync.run() is called. In such case the actual registration will be done at run() time.

class autobahn_sync.session.SyncSession(callbacks_runner, on_challenge_callback)[source]

Synchronous version of autobahn.twisted.wamp.ApplicationSession

call(*args, **kwargs)[source]

Call a remote procedure.

Replace autobahn.wamp.interface.IApplicationSession.call()

leave(*args, **kwargs)[source]

Actively close this WAMP session.

Replace autobahn.wamp.interface.IApplicationSession.leave()

publish(*args, **kwargs)[source]

Publish an event to a topic.

Replace autobahn.wamp.interface.IApplicationSession.publish()

register(*args, **kwargs)[source]

Register a procedure for remote calling.

Replace autobahn.wamp.interface.IApplicationSession.register()

subscribe(*args, **kwargs)[source]

Subscribe to a topic for receiving events.

Replace autobahn.wamp.interface.IApplicationSession.subscribe()

2.3. Exceptions

Note

Most exceptions are republished from autobahn.wamp.exception and twisted.internet.error

exception autobahn_sync.exceptions.Error[source]

Base class for all exceptions related to WAMP.

exception autobahn_sync.exceptions.SessionNotReady[source]

The application tried to perform a WAMP interaction, but the session is not yet fully established.

exception autobahn_sync.exceptions.SerializationError[source]

Exception raised when the WAMP serializer could not serialize the application payload (args or kwargs for CALL, PUBLISH, etc).

exception autobahn_sync.exceptions.ProtocolError[source]

Exception raised when WAMP protocol was violated. Protocol errors are fatal and are handled by the WAMP implementation. They are not supposed to be handled at the application level.

exception autobahn_sync.exceptions.TransportLost[source]

Exception raised when the transport underlying the WAMP session was lost or is not connected.

exception autobahn_sync.exceptions.ApplicationError(error, *args, **kwargs)[source]

Base class for all exceptions that can/may be handled at the application level.

error_message()[source]

Get the error message of this exception.

Returns:The error message.
Return type:str
exception autobahn_sync.exceptions.NotAuthorized[source]

Not authorized to perform the respective action.

exception autobahn_sync.exceptions.InvalidUri[source]

The URI for a topic, procedure or error is not a valid WAMP URI.

exception autobahn_sync.exceptions.ConnectionRefusedError(osError=None, string='')[source]

Connection was refused by other side

exception autobahn_sync.exceptions.AbortError[source]

Error raised when the soutes respond with an ABORT message to our HELLO

exception autobahn_sync.exceptions.AlreadyRunningError[source]

Error raised when trying to run() multiple time an autobahn_sync.AutobahnSync

exception autobahn_sync.exceptions.NotRunningError[source]

Error raised when trying to stop() multiple time an autobahn_sync.AutobahnSync

2.4. Flask extension

Flask extension provide an easier integration of Autobahn-Sync by doing it configuration in three ways (by order of priority):

  • Configuration explicitly passed in init_app
  • Configuration present in app.config
  • Default configuration
Variable name Description
AUTHOBAHN_ROUTER WAMP router to connect to (default: ws://localhost:8080/ws)
AUTHOBAHN_REALM WAMP realm to connect to (default: realm1)
AUTHOBAHN_IN_TWISTED Set to true if the code is going to run inside a Twisted application (default: false)
class autobahn_sync.extensions.flask.FlaskAutobahnSync(app=None, **config)[source]

Inherit from autobahn_sync.AutobahnSync to integrate it with Flask.

Parameters:
  • app – Flask app to configure, if provided init_app() is automatically called
  • config – remaining kwargs will be passed to init_app as configuration
init_app(app, router=None, realm=None, in_twisted=None)[source]

Configure and call the AutobahnSync.start() method

Parameters:
  • app – Flask app to configure
  • router – WAMP router to connect to
  • realm – WAMP realm to connect to
  • in_twisted – Is the code is going to run inside a Twisted application

Note

The config provided as argument will overwrite the one privided by app.config