Package: lime-python
Lime PythonPython implementation of LIME - A lightweight messaging libraryLIME allows you to build scalable, real-time messaging applications using a JSON-basedopen protocol. It'sasynchronousand supports any persistent transport like TCP or Websockets.You can send and receive any type of object into the wire as long it can be represented as JSON or text (plain or encoded with base64) and it has aMIME typeto allow the other party handle it in the right way.The connected nodes can send receipts to the other parties to notify events about messages (for instance, a message was received or the content invalid or not supported).Besides that, there's aREST capablecommand interface with verbs (get, set and delete) and resource identifiers (URIs) to allow rich messaging scenarios. You can use that to provide services like on-band account registration or instance-messaging resources, like presence or roster management.Finally it has built-in support for authentication, transport encryption and compression.Protocol overviewEnvelopesThe basic protocol data package is calledenvelope. As mentioned before, there are four types:Message- Transports content between nodesNotification- Notify about message eventsCommand- Provides an interface for resource managementSession- Establishes the communication channelAll envelope types share some properties (like theid- the envelope unique identifier - and thefromandtorouting information) but there are some unique properties of each one that allows the proper deserialization when a JSON object is received by the transport.TransportsTheTransportinterface represents a persistent transport connection that allows the management of the connection state, besides sending and receiving envelopes. Currently, the python implementation provides one package for Lime transport class. These are publicly available on PyPI and on ourGithub:WebSocketChannelsWhen two nodes are connected to each other asessioncan be established between them. To help the management of the session state, the library defines theChannelabstract class, an abstraction of the session over theTransportinstance. The node that received the connection is theserverand the one who is connecting is theclient. There is currently only one specific implementation of this class for the client (ClientChannel), providing specific functionality for the client role in the connection. The only difference between the client and the server is related to the session state management, where the server has full control of it. Besides that, they share the same set of funcionalities.How to useSimply install thelime-pythonpackage with pip.pipinstalllime-pythonExamplefromlime_pythonimportSessionOrimportlime_pythonclassMyClass:def__init__(self):self.session=lime_python.Session(lime_python.SessionState.NEW)Starting a connectionTo start a connection with a server in a specific domain, the client can useDNS queriesto check for alimeSRV entry and get the server address. This is not mandatory and the client can use static connection information, but its a good idea to rely on DNS since the protocol is domain based. In the code, the methodopen_asyncof theTransportinterface should be called passing the remote URI (in the server, the URI parameter can be null).After connecting the transport, the client should send anew sessionenvelope to start the session negotiation. TheClientChannelclass provides the methodstart_new_session_asyncfor that.Example# Creates a new transport and connect to the serverserver_uri='net.tcp://localhost:55321'transport=WebsocketTransport()awaittransport.open_async(server_uri)client_channel=ClientChannel(transport)Session establishmentThe server is the responsible for the establishment of the session and its parameters, like theidand node information (both local and remote). It can optionally negotiate transport options and authenticate the client using a supported authentication scheme. Note that the protocol did not dictate that the session negotiation and authentication are mandatory. In fact, after receiving anew sessionenvelope, the server can just send anestablished sessionenvelope to the client to start the envelope exchanging.NegotiationDuring the negotiation of transport options, the server sends to the client the available compression and encryption options and allows it to choose which one it wants to use in the session. The client select its options using thenegotiate_session_asyncmethod fromClientChannel. After receiving and validating the client choices the server echoes them back to the client, allowing it to apply the transport options, and the server does itself the same. TheTransportinterface has the methodsset_compressionandset_encryptionfor this reason, but theChannelimplementation already handles that automatically.AuthenticationAfter the negotiation of transport options negotiation, the server presents to the client the available authentication schemes and the client shall provide the scheme-specific data and identify itself with an identity, which is presented asname@domain(like an e-mail). Usually the domain of the client identity is the same of the server if the client is using a local authentication scheme (username/password) but can be a stranger domain if the client is using a transport authentication (TLS certificate).EstablishmentWhen the server establishes the session, it assigns to the client a unique node identifier, in the formatname@domain/instancesimilar to the Jabber ID in the XMPP protocol. This identifier is important for envelope routing in multi-party server connection scenarios.Exchanging envelopesWith an established session the nodes can exchange messages, notifications and commands until the server finishes the session. TheChannelclass defines methods to send and receive specific envelopes, likesend_messageandon_messagefor messages orsend_commandandon_commandfor commands.RoutingThe protocol doesn't explicitly define how envelope routing should work during a session. The sole definition is that if an originator does not provide thetoproperty value, it means that the message is addressed to the immediate remote party; in the same way that if a node has received an envelope without thefromproperty value, it must assume that the envelope is originated by the remote party.An originator can send an envelope addressed to any destination in the remote server and it may or may not accept it. But an originator should address an envelope to a node different of the remote party only if it is trusted for receiving these envelopes. A remote party can be trusted for that if it has presented a valid domain certificate during the session negotiation. In this case, this node can receive and send envelopes for any identity of the authenticated domain.ExamplesMessages and notifications# Sending a plain text message to the remote partytext_message=Message('text/plain','Hello!')client_channel.send_message(text_message)# Sending a generic JSON message addressed to a specific nodecontent={'property1':'string value','property2':2,'property3':True}json_message=Message('application/json',content)json_message.to='anyone@domain.com'client_channel.send_message(json_message)# Receive a messagedefmy_on_message(message:Message)->None:print(f'Message received from{message.from_n}:{message.content}');# Send a notificationnotification=Notification(NotificationEvent.RECEIVED)notification.id=message.idclient_channel.send_notification(notification)client_channel.on_message=my_on_message# Receive a notificationdefmy_on_notification(notification:Notification)->None:print(f'Message received from{notification.from_n}:{notification.event}')client_channel.on_notification=my_on_notificationCommands# Arbitrary commandsget_contacts_command=Command(CommandMethod.GET,MY_CONTACTS_URI)get_contacts_command.id=str(uuid.uuid4())client_channel.send_command(get_contacts_command)defmy_on_command(command:Command)->None:ifcommand.id==get_contacts_command.idandcommand.status==CommandStatus.SUCCESS:contacts=command.resourceforcontactincontacts:print(contact)client_channel.on_command=my_on_command