API Reference

Client

class aiocometd.Client(url, connection_types=None, *, connection_timeout=10.0, ssl=None, max_pending_count=100, extensions=None, auth=None, loop=None)[source]

CometD client

Parameters:
  • url (str) – CometD service url
  • connection_types (list[ConnectionType], ConnectionType or None) – List of connection types in order of preference, or a single connection type name. If None, [WEBSOCKET, LONG_POLLING] will be used as a default value.
  • connection_timeout (int, float or None) – The maximum amount of time to wait for the transport to re-establish a connection with the server when the connection fails.
  • ssl – SSL validation mode. None for default SSL check (ssl.create_default_context() is used), False for skip SSL certificate validation, aiohttp.Fingerprint for fingerprint validation, ssl.SSLContext for custom SSL certificate validation.
  • max_pending_count (int) – The maximum number of messages to prefetch from the server. If the number of prefetched messages reach this size then the connection will be suspended, until messages are consumed. If it is less than or equal to zero, the count is infinite.
  • extensions (list[Extension] or None) – List of protocol extension objects
  • auth (AuthExtension) – An auth extension
  • loop – Event loop used to schedule tasks. If loop is None then asyncio.get_event_loop() is used to get the default event loop.
coroutine open()[source]

Establish a connection with the CometD server

This method works mostly the same way as the handshake method of CometD clients in the reference implementations.

Raises:
  • ClientError – If none of the connection types offered by the server are supported
  • ClientInvalidOperation – If the client is already open, or in other words if it isn’t closed
  • TransportError – If a network or transport related error occurs
  • ServerError – If the handshake or the first connect request gets rejected by the server.
coroutine close()[source]

Disconnect from the CometD server

coroutine publish(channel, data)[source]

Publish data to the given channel

Parameters:
  • channel (str) – Name of the channel
  • data (dict) – Data to send to the server
Returns:

Publish response

Return type:

dict

Raises:
coroutine subscribe(channel)[source]

Subscribe to channel

Parameters:

channel (str) – Name of the channel

Raises:
coroutine unsubscribe(channel)[source]

Unsubscribe from channel

Parameters:

channel (str) – Name of the channel

Raises:
coroutine receive()[source]

Wait for incoming messages from the server

Returns:

Incoming message

Return type:

dict

Raises:
  • ClientInvalidOperation – If the client is closed, and has no more pending incoming messages
  • ServerError – If the client receives a confirmation message which is not successful
  • TransportTimeoutError – If the transport can’t re-establish connection with the server in connection_timeout time.
closed

Marks whether the client is open or closed

subscriptions

Set of subscribed channels

connection_type

The current connection type in use if the client is open, otherwise None

pending_count

The number of pending incoming messages

Once open is called the client starts listening for messages from the server. The incoming messages are retrieved and stored in an internal queue until they get consumed by calling receive.

has_pending_messages

Marks whether the client has any pending incoming messages

ConnectionType

class aiocometd.ConnectionType[source]

CometD Connection types

LONG_POLLING = 'long-polling'

Long polling connection type

WEBSOCKET = 'websocket'

Websocket connection type

Extensions

class aiocometd.Extension[source]

Bases: abc.ABC

Defines operations supported by extensions

coroutine incoming(payload, headers=None)[source]

Process incoming payload and headers

Called just after a payload is received

Parameters:
  • payload (list[dict]) – List of incoming messages
  • headers (dict or None) – Headers to send
coroutine outgoing(payload, headers)[source]

Process outgoing payload and headers

Called just before a payload is sent

Parameters:
  • payload (list[dict]) – List of outgoing messages
  • headers (dict) – Headers to send
class aiocometd.AuthExtension[source]

Bases: aiocometd.extensions.Extension

Extension with support for authentication

coroutine authenticate()[source]

Called after a failed authentication attempt

For authentication schemes where the credentials are static it doesn’t makes much sense to reimplement this function. However for schemes where the credentials can expire (like OAuth, JWT…) this method can be reimplemented to update those credentials

Exceptions

Exception types

Exception hierarchy:

AiocometdException
    ClientError
        ClientInvalidOperation
    TransportError
        TransportInvalidOperation
        TransportTimeoutError
        TransportConnectionClosed
    ServerError
exception aiocometd.exceptions.AiocometdException[source]

Base exception type.

All exceptions of the package inherit from this class.

exception aiocometd.exceptions.ClientError[source]

ComtedD client side error

exception aiocometd.exceptions.ClientInvalidOperation[source]

The requested operation can’t be executed on the current state of the client

exception aiocometd.exceptions.ServerError[source]

CometD server side error

If the response contains an error field it gets parsed according to the specs

Parameters:
  • message (str) – Error description
  • response (dict) – Server response message
error

Error field in the response

error_args

Arguments part of the error, message field

error_code

Error code part of the error code part of the error, message field

error_message

Description part of the error, message field

message

Error description

response

Server response message

exception aiocometd.exceptions.TransportConnectionClosed[source]

The connection unexpectedly closed

exception aiocometd.exceptions.TransportError[source]

Error during the transportation of messages

exception aiocometd.exceptions.TransportInvalidOperation[source]

The requested operation can’t be executed on the current state of the transport

exception aiocometd.exceptions.TransportTimeoutError[source]

Transport timeout