avatar     NEP Home home Reference NEP school
 
Menu Get started Choregraphe

What is nep?

 

NEP is a user-friendly and cross-platform communication framework for building distributed applications. NEP acts as an intermediary between two or more software modules to facilitate communication between them using sockets. These modules or nodes can be written in different programming languages and be executed from different operating systems.

NEP was designed to be simple, easy-to-install, easy-to-use and easy-to-learn. NEP is mainly based on high-performance and usable ZeroMQ sockets.

Installation

 

The easiest way to try out NEP is using the NEP App. Feel free to install NEP App for your preferred operating system

 
Smiley face    Download for Windows
 
Smiley face    Download for Mac OSX
 
   Download for Ubuntu

A simple publisher/subscriber communication example

 

In this example we need to send information from a Program A written in Python 3 to a Program B written in Python 2. Both programs will be executed in the same device defined as Computer A.

 
Paris
 
This example tries to show how to communicate programs in different Python versions. However, the same approach can be used to communicate two programs written in Python 2 or two programs written in Python 3.
 
This tutorial suppose that you have installed a version of Python 2 or Python 3 (or both) in your computer. This tutorial also suppose you know how to install a python package using pip. To know what is pip and how to use it, see this user guide of pip
 

Use the next pip command to install nep (in some terminal):

 pip install nep 
 

If you have both Python 2 and Python 3 installed use the next command to install nep in Python 2 (in Windows):

 py -2 -m pip install nep 

To install nep in Python 3 use the next command ((n Windows)):

 py -3 -m pip install nep 
 

Defining a Publisher

 

The code for Program A or publisher is:

{{code_pub}}
 

In lines 2 and 3 we are adding the required nep and time libraries. Line 4 creates a new nep node which is used as an identifier of Program A. This identifier is defined as sender. The method new_pub of the object node (line 5) is used to define the name of the topic (i.e., a communication channel) and the type of message we would like to send. In this case, we define test_topic as the name of the topic and json as the type of message to send.

JSON messages in Python can be defined as python dictionaries. If you dont know what is a python dictionary, please visit this link With NEP you can send any combination of data that is JSON serializable, such as int, float, string, and lists.

In this program, we are sending a JSON message (python dictionary) each second in an infinite loop. Inside while loop you can add your code. Line 12 defines the message to send to Program B. Line 14 uses the method publish of the object pub to send the JSON message.

 

Defining a Subscriber without callbacks

 

The code for Program B or subscriber is:

{{code_sub}}
 

In this program we define a new node denoted as receiver (line 4). Then, we create a subscriber instance sub which will be used to listen to json messages from the topic test_topic. Method listen (line 7) is used to read messages. This function does not block even if there is no message received. Therefore, we need to query if a message was sent by the sender (publisher) using the value saved in is_message. If is_message == true (line 8) then msg will contain a message send from Program A. Otherwise msg = {} (null dictionary). Therefore, your code processing the message must be inside if statement (line 8). Otherwise, set a very small time before reading the next message (line 12 and 13 ).

 
Each program must have a unique node name.
 

Defining a Subscriber using callbacks

 
 

Rather than using the listen() function to read the from the publisher as use query if a message is in the socket, you can create a special function (callback) that will be executed only when new messages arrive. The code for Program B can be changed to support callbacks as follow:

{{code_subcallback}}
 

In this case we need to use the method new_callback() . Similar to the previous example, we need to define the name of the topic (in this case "test") and the type of message to send (in this case "json"). Additionally, we need to set the function that will be executed each time a message arrives (in this case callback )

 

To use callback in python you need to install asyncio and tornado in Python 3 and trollius and tornado in Python 2

 

Running the code

 

Before running the publisher and the subscriber we need to start the NEP App. You will see the name of the topic in the table of this interface, such as the next image.

 
Paris
 

Topics are used in NEP to manage sockets endpoints. In this way, you don't need to care about defining port and IP values in large and complex applications.

 
Publishers and Subscribers can only be communicated if they share the same topic name and message type.
 

Some useful and basic terminology

 

Nodes

 

Nodes are simple, loosely coupled, independent and re-usable building blocks applications. They usually run in parallel in one or different machines and can be written in different programming languages. In NEP communication between nodes is done using sockets

 

Sockets and endpoints

 

A network socket is an inter-process communication method for sending or receiving data between nodes on a computer network. A socket is mainly defined by an endpoint, which is composed of a protocol, an IP address , and a port value.

 

An example of endpoint definition is: tcp://127.0.0.1:5000 where tcp indicates the protocol, 127.0.0.1 indicates the IP address, and 5000 indicates the port of the connection.

 

Topics

 

Topics are used in NEP to manage sockets endpoints. In this way, you don't need to care about defining port and IP values in large and complex applications. Those nodes where a specific topic has been defined can only communicate with others where the same topic is defined.

 
 

Publish/Subscribe pattern

 

In the Publish/Subscribe pattern the producers of messages, called publishers, send messages without the knowledge of the receivers denoted subscribers. This communication method is asynchronous. Therefore, the subscriber node will not block its execution when trying to read a message.

 
Paris Using the NEP App you can create N publishers using the same topic to send messages to N subscribers.