asphalt.wamp.client

exception asphalt.wamp.client.ConnectionError

Raised when there was an error connecting to the WAMP router.

class asphalt.wamp.client.WAMPClient(host='localhost', port=8080, path='/ws', realm='realm1', *, protocol_options=None, connection_timeout=10, reconnect_delay=5, max_reconnection_attempts=15, shutdown_timeout=15, registry=None, tls=False, tls_context=None, serializer=None, auth_method='anonymous', auth_id=None, auth_secret=None)

A WAMP client.

Variables:
  • realm_joined (Signal) – a signal (SessionJoinEvent) dispatched when the client has joined the realm and has registered any procedures and subscribers on the router
  • realm_left (Signal) – a signal (SessionLeaveEvent) dispatched when the client has left the realm
Parameters:
  • host (str) – host address of the WAMP router
  • port (int) – port to connect to
  • path (str) – HTTP path on the router
  • realm (str) – the WAMP realm to join the application session to (defaults to the resource name if not specified)
  • protocol_options (Optional[Dict[str, Any]]) – dictionary of Autobahn’s websocket protocol options
  • connection_timeout (float) – maximum time to wait for the client to connect to the router and join a realm
  • reconnect_delay (float) – delay between connection attempts (in seconds)
  • max_reconnection_attempts (Optional[int]) – maximum number of connection attempts before giving up
  • shutdown_timeout (Optional[float]) – maximum number of seconds to wait for the client to complete its shutdown sequence (unregister procedures/subscriptions, wait for running handlers to finish, leave the realm)
  • registry (Union[WAMPRegistry, str, None]) – a WAMPRegistry instance, a module:varname reference or resource name of one
  • tls (bool) – True to use TLS when connecting to the router
  • tls_context (Union[str, SSLContext, None]) – an SSLContext instance or the resource name of one
  • serializer (Union[Serializer, str, None]) – a asphalt.serialization.api.Serializer instance or the resource name of one
  • auth_method (str) – authentication method to use (valid values are currently anonymous, wampcra and ticket)
  • auth_id (Optional[str]) – authentication ID (username)
  • auth_secret (Optional[str]) – secret to use for authentication (ticket or password)
coroutine call(self, endpoint, *args, options=None, **kwargs)

Call an RPC function.

Parameters:
  • endpoint (str) – name of the endpoint to call
  • args – positional arguments to call the endpoint with
  • options (Union[CallOptions, Dict[str, Any], None]) – either an Autobahn call options object or a dictionary of keyword arguments to make one
  • kwargs – keyword arguments to call the endpoint with
Returns:

the return value of the call

Raises:

TimeoutError – if the call times out

connect()

Connect to the WAMP router and join the designated realm.

When the realm is successfully joined, exceptions, procedures and event subscriptions from the registry are automatically registered with the router.

The connection process is restarted if connection, joining the realm or registering the exceptions/procedures/subscriptions fails. If max_connection_attempts is set, it will limit the number of attempts. If this limit is reached, the future gets the last exception set to it. Otherwise, the process is repeated indefinitely until it succeeds.

If the realm has already been joined, the future completes instantly.

Raises:ConnectionError – if there is a protocol level problem connecting to the router
Return type:Future
details

Return the session details object provided by Autobahn if the session has been established.

Return type:Optional[SessionDetails]
map_exception(exc_class, error)

Map a Python exception to a WAMP error.

Parameters:
Return type:

None

coroutine publish(self, topic, *args, options=None, **kwargs)

Publish an event on the given topic.

Parameters:
  • topic (str) – the topic to publish on
  • args – positional arguments to pass to subscribers
  • options (Union[PublishOptions, Dict[str, Any], None]) – either an Autobahn publish options object or a dictionary of keyword arguments to make one
  • kwargs – keyword arguments to pass to subscribers
Return type:

Optional[int]

Returns:

publication ID (if the acknowledge option is True)

coroutine register(self, handler, name=None, options=None)

Add a procedure handler to the registry and attempt to register it on the router.

Parameters:
  • handler (Callable) – callable that handles calls for the given endpoint
  • name (Optional[str]) – name of the endpoint to register (e.g. x.y.z); omit to use the internal name of the callable
  • options (Union[RegisterOptions, Dict[str, Any], None]) – either an Autobahn register options object or a dictionary of keyword arguments to make one

Note

the details_arg option is set by WAMPClient itself so do not attempt to set it yourself.

Return type:None
session_id

Return the current WAMP session ID.

Return type:Optional[int]
Returns:the session ID or None if not in a session.
coroutine stop()

Finish outstanding tasks and then disconnect from the router.

First, all subscriptions and registrations are undone to prevent more publications or calls from coming in. Next, all outstanding tasks are awaited on. Finally, the client leaves the realm and disconnects from the router.

coroutine subscribe(self, handler, topic, options=None)

Add a WAMP event subscriber to the registry and attempt to register it on the router.

Parameters:
  • handler (Callable) – the callable that is called when a message arrives
  • topic (str) – topic to subscribe to
  • options (Union[SubscribeOptions, Dict[str, Any], None]) – either an Autobahn subscribe options object or a dictionary of keyword arguments to make one

Note

the details option is set by WAMPClient itself so do not attempt to set it yourself.

Return type:None