Quick Start

InteractionFreePy is extremely easy to use. Follow the steps below as a quick start.

  1. Install

  pip install interactionfreepy
  1. Run a service

We assume Alice wants to publish a service that provides a dragon-language encryption for everyone. She defines the service as follows:

 1class DragonCipher:
 2  def encrypt_to_dragon_speech(self, text: str) -> str:
 3    result = []
 4    for char in text:
 5      lower_char = char.lower()
 6      if lower_char in {'a', 'e', 'i', 'o', 'u'}:
 7        new_char = '*'
 8      elif char.isalpha():
 9        new_char = f"{char}-ar" if char.isupper() else f"{char}-ar"
10      else:
11        new_char = char
12      result.append(new_char)
13    return ''.join(result)

As an example, the details of the implementation are not important. It can be any function that does anything you want. With the function defined, she can publish the service as follows:

1from interactionfreepy import IFWorker, IFLoop
2
3worker = IFWorker('tcp://interactionfree.cn:1061', 'DragonCipher_Alice', DragonCipher())
4IFLoop.join()

Here, tcp://interactionfree.cn:1061 is the address of the broker. It is provided by the InteractionFreePy project for testing. Due to the limitations of the server, visiting our public broker may be slow. You can also host your own broker to ensure the performance, latency time, and privacy of your service. See Hosting a broker for more details. 'DragonCipher_Alice' is the name of the service, which can be any string that you want. DragonCipher() provided the instance of the service. Finally, IFLoop.join() prevents the main thread from exiting, keeping the service running.

  1. Run a client

Now, Bob has some text that he wants to encrypt into dragon speech.

1from interactionfreepy import IFWorker
2
3worker = IFWorker('tcp://interactionfree.cn:1061')
4print(worker.listServiceNames())
5humam_speech = 'To be or not to be'
6dragon_speech = worker.DragonCipher_Alice.encrypt_to_dragon_speech(humam_speech)
7print(f'{humam_speech} -> {dragon_speech}')

As a client, he does not need to provide the service address or the instance of the service. With the broker address provided, he can list all the services available by calling listServiceNames(). Then, he can call the service directly by using the service name as an attribute of the worker instance, worker.DragonCipher_Alice.encrypt_to_dragon_speech(human_speech). The most interesting part is that Bob can run the code on a completely different machine and never needs to know the implementation details of the service. He does not even need to import anything related to Alice. With this code running, he might get the output like this:

['DragonCipher_Alice']
To be or not to be -> T-ar* b-ar* *r-ar n-ar*t-ar t-ar* b-ar*

Note

The result of listServiceNames() may be different from the one shown above. It is the list of all the services that are currently running on the broker. More importantly, as the service name is expected to be unique, you might get an error if you try to run the service with the same name as the one already running on the broker. Therefore, when using my public testing broker located at interactionfree.cn:1061, it is recommended to use a unique name for your service, such as your GitHub username or any other unique identifier. For example, as my GitHub username is hwaipy, I can use DragonCipher_hwaipy as the service name.

  1. Host a broker

You can host your own broker by running the following code:

1from interactionfreepy import IFBroker, IFLoop
2
3broker = IFBroker()
4IFLoop.join()

It will start a broker on the default port 1061. You can also specify the port by passing the port number to the constructor:

1from interactionfreepy import IFBroker, IFLoop
2
3broker = IFBroker('tcp://*:999')
4IFLoop.join()

You can also host the broker in a Docker container. The Docker image is available as hwaipy/ifbroker. To run the broker in a Docker container, you can use the following command:

docker run -d -p 1061:1061 --name IFBroker hwaipy/ifbroker:latest