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.components.messaging.Messaging(session)

Bases
diffusion.components.Component

Messaging component.

Parameters
  • session (Session) The active Session to operate on.
Methods
  • add_filter_response_handler(session_filter, callback, error_callback) Registers a session filter response handler.
  • add_request_handler(path, callback, request_type, response_type, addressed) Registers a request handler.
  • register_request_handler(path, callback, request_type, response_type, session_properties) Register the session as the handler for requests to a given path.
  • 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_request_handler(path, callback, request_type, response_type, addressed=False)

Registers a request handler.

The callback callable is executed 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.
  • callback (callable) A callable to handle the request.
  • request_type (Union(int, str, type of datatype)) The required data type of the request.
  • response_type (Union(int, str, type of datatype)) The data type to be returned as the response.
  • 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, callback, error_callback=None)

Registers a session filter response handler.

The callback callable is executed when the session receives a response to a request sent to the session filter.

Parameters
  • session_filter (str) A session filtering query.
  • callback (callable) A callable to handle the response.
  • error_callback (callable, optional) A callable to handle error response.
method

register_request_handler(path, callback, request_type, response_type, 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 callback to these requests is added at the same time, using add_request_handler internally.

Parameters
  • path (str) The handler will respond to the requests to this path.
  • callback (callable) A callable to handle the request.
  • request_type (Union(int, str, type of datatype)) The expected data type of the request.
  • response_type (Union(int, str, type of datatype)) The data type to be returned by the callback.
  • 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_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

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.

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.