API Reference

Client

class aiocometd.Client(url, connection_types=None, *, connection_timeout=10.0, ssl=None, max_pending_count=100, extensions=None, auth=None, json_dumps=<function dumps>, json_loads=<function loads>, loop=None)[source]

CometD client

Parameters:
  • url (str) – CometD service url
  • connection_types (Union[ConnectionType, List[ConnectionType], 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 (Union[int, float]) – The maximum amount of time to wait for the transport to re-establish a connection with the server when the connection fails.
  • ssl (Union[SSLContext, bool, Fingerprint, None]) – 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 (Optional[List[Extension]]) – List of protocol extension objects
  • auth (Optional[AuthExtension]) – An auth extension
  • json_dumps (Callable[[Dict[str, Any]], str]) – Function for JSON serialization, the default is json.dumps()
  • json_loads (Callable[[str], Dict[str, Any]]) – Function for JSON deserialization, the default is json.loads()
  • loop (Optional[AbstractEventLoop]) – 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(self)[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.
Return type:

None

coroutine close(self)[source]

Disconnect from the CometD server

Return type:None
coroutine publish(self, channel, data)[source]

Publish data to the given channel

Parameters:
  • channel (str) – Name of the channel
  • data (Dict[str, Any]) – Data to send to the server
Return type:

Dict[str, Any]

Returns:

Publish response

Raises:
coroutine subscribe(self, channel)[source]

Subscribe to channel

Parameters:

channel (str) – Name of the channel

Raises:
Return type:

None

coroutine unsubscribe(self, channel)[source]

Unsubscribe from channel

Parameters:

channel (str) – Name of the channel

Raises:
Return type:

None

coroutine receive(self)[source]

Wait for incoming messages from the server

Return type:

Dict[str, Any]

Returns:

Incoming message

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

Return type:bool
subscriptions

Set of subscribed channels

Return type:Set[str]
connection_type

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

Return type:Optional[ConnectionType]
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.

Return type:int
has_pending_messages

Marks whether the client has any pending incoming messages

Return type:bool

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(self, payload, headers=None)[source]

Process incoming payload and headers

Called just after a payload is received

Parameters:
Return type:

None

coroutine outgoing(self, payload, headers)[source]

Process outgoing payload and headers

Called just before a payload is sent

Parameters:
Return type:

None

class aiocometd.AuthExtension[source]

Bases: aiocometd.extensions.Extension

Extension with support for authentication

coroutine authenticate(self)[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

Return type:None

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(message, response)[source]

CometD server side error

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

Parameters:
error

Error field in the response

Return type:Optional[str]
error_args

Arguments part of the error, message field

Return type:Optional[List[str]]
error_code

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

Return type:Optional[int]
error_message

Description part of the error, message field

Return type:Optional[str]
message

Error description

Return type:str
response

Server response message

Return type:Optional[Dict[str, Any]]
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