Request-Response Messaging

This package provides a client session with request-response messaging capabilities that can be used to implement application services.

Request-response messaging allows a session to send requests to other sessions. Each receiving session provides a corresponding response, which is returned to the sending session. Each request and response carries an application provided value.

Types of Messages

There are three mechanisms to specify the recipient(s) of a message:

  • Send to path: The messages of this type are sent without specifying the receiving session. The server will forward an unaddressed message to any session which was registered as a handler for the path indicated in the message.

  • Send to session: Messages can be sent to a specific session by specifying its ID. To be able to respond to messages addressed to itself, a session still needs to have an internal handler.

  • Send to filter: Messages can also be addressed using a session filter. When receiving such a message, the server will locate all the currently active sessions which match the filter's conditions, and respond to the sender with the number of located sessions; the sender can expect up to that many responses, so it needs to have a handler ready to handle those responses.

class

diffusion.messaging.Messaging(session)

Bases
diffusion.internal.components.Component

Messaging component.

It is not supposed to be instantiated independently; an instance is available on each Session instance as session.messaging.

Parameters
  • session (Session) The active Session to operate on.
Attributes
  • services (ServiceLocator) Alias for the internal session's service locator.
Classes
Methods
  • add_filter_response_handler(session_filter, handler) Registers a session filter response handler.
  • add_request_handler(path, handler, session_properties) Register the session as the handler for requests to a given path.
  • add_stream_handler(path, handler, addressed) Registers a request stream handler.
  • send_request_to_filter(session_filter, path, request) (int) Send a request to other sessions, specified by the filter.
  • send_request_to_path(path, request, response_type) (DataType, optional) Send a request to sessions based on a path.
  • send_request_to_session(path, session_id, request, response_type) (DataType, optional) Send a request to a single session.
method

add_stream_handler(path, handler, addressed=False)

Registers a request stream handler.

The handler is invoked when the session receives a request sent to the given path or session filter.

Parameters
  • path (str) The handler will respond to the requests to this path.
  • handler (Messaging.RequestHandler) A Handler instance to handle the request.
  • addressed (bool, optional) True to handle the requests addressed to the session's ID or using a session filter; False for unaddressed requests.
method

add_filter_response_handler(session_filter, handler)

Registers a session filter response handler.

The handler is invoked when the session receives a response to a request sent to the session filter.

Parameters
  • session_filter (str) A session filtering query.
  • handler (Handler) A Handler instance to handle the request.
method

send_request_to_path(path, request, response_type=None)

Send a request to sessions based on a path.

Parameters
  • path (str) The path to send a request to.
  • request (DataType) The request to be sent, wrapped into the required DataType class.
  • response_type (Union(int, str, type of datatype, nonetype), optional) The type to convert the response to. If omitted, it will be the same as the request's data type.
Returns (DataType, optional)

The response value of the provided response_type.

method

add_request_handler(path, handler, session_properties=())

Register the session as the handler for requests to a given path.

This method is used to inform the server that any unaddressed requests to the given path should be forwarded to the active session. The handler to these requests is added at the same time, using add_stream_handler internally.

Parameters
  • path (str) The handler will respond to the requests to this path.
  • handler (Messaging.RequestHandler) A callable to handle the request.
  • session_properties (collection of str, optional) A list of keys of session properties that should be supplied with each request. To request all fixed properties include ALL_FIXED_PROPERTIES as a key; any other fixed property keys will be ignored. To request all user properties include ALL_USER_PROPERTIES as a key; any other user properties will be ignored.
method

send_request_to_filter(session_filter, path, request)

Send a request to other sessions, specified by the filter.

Parameters
  • session_filter (str) A session filtering query.
  • path (str) The path to send a request to.
  • request (DataType) The request to be sent, wrapped into the required DataType class.
Returns (int)

The number of sessions that correspond to the filter, which is the number of responses that can be expected. When each of the responses is received, the handler registered for the filter will be executed.

method

send_request_to_session(path, session_id, request, response_type=None)

Send a request to a single session.

Parameters
  • path (str) The path to send a request to.
  • session_id (SessionId) The ID of the session to send the request to.
  • request (DataType) The request to be sent, wrapped into the required DataType class.
  • response_type (Union(int, str, type of datatype, nonetype), optional) The type to convert the response to. If omitted, it will be the same as the request's data type.
Returns (DataType, optional)

The response value of the provided response_type.

class

RequestHandler(*args, **kwds)

Bases
diffusion.handlers.Handler typing.Protocol typing.Generic

Handler for messaging requests.

Classes
  • _ProtocolMeta Metaclass for defining Abstract Base Classes (ABCs).
Methods
  • __init_subclass__(*args, **kwargs) This method is called when a class is subclassed.
  • handle(event, **kwargs) (DataType) Execute the callback.
class
typing._ProtocolMeta(name, bases, namespace, **kwargs)
Bases
abc.ABCMeta

Metaclass for defining Abstract Base Classes (ABCs).

Use this metaclass to create an ABC. An ABC can be subclassed directly, and then acts as a mix-in class. You can also register unrelated concrete classes (even built-in classes) and unrelated ABCs as 'virtual subclasses' -- these and their descendants will be considered subclasses of the registering ABC by the built-in issubclass() function, but the registering ABC won't show up in their MRO (Method Resolution Order) nor will method implementations defined by the registering ABC be callable (not even via super()).

Methods
  • __instancecheck__(cls, instance) Override for isinstance(instance, cls).
  • __subclasscheck__(cls, subclass) Override for issubclass(subclass, cls).
  • register(cls, subclass) Register a virtual subclass of an ABC.
staticmethod
register(cls, subclass)

Register a virtual subclass of an ABC.

Returns the subclass, to allow usage as a class decorator.

staticmethod
__subclasscheck__(cls, subclass)

Override for issubclass(subclass, cls).

staticmethod
__instancecheck__(cls, instance)

Override for isinstance(instance, cls).

classmethod
__init_subclass__(*args, **kwargs)

This method is called when a class is subclassed.

The default implementation does nothing. It may be overridden to extend subclasses.

method
handle(event='request', **kwargs) → DataType

Execute the callback.