Connecting 2 scripts in a local network (e.g., two computers on the same Wifi network)

We will explore three different methods:

Method
Requirements
Advantages
Disadvantages

NEP libraries installed (also required in the other methods)

Less tools to install

It can be annoying to deal with IP addresses, communication modes and port numbers

Node.js and nep-cli package installed

You don't need to care anymore about port numbers and modes.

You need to remember and use the same IP address for all your nodes.

NEP+ app installed

Same that NEP CLI

Same that NEP CLI

Where it is recommended to use these methods?

Method
When is recommended?

When you only need to connect two modules without caring about the scalability of the project

When will you use your modules in many projects and care about reusability and scalability

Same as nep-cli but for users preferring a user interface

Method 1: Direct

Connecting two or more modules/nodes are executed on different devices/computers using a direct mode requires knowing the IP address of one of the computers and the port in which the connection is made.

The command ipconfig (in Windows) or ifconfig (in Linux and OSX) can be used to know the IP address of your computer.

Connecting Computer A and Computer B

The code required in Computer A (Publisher) is shown below

import nep
import time

# Create a new nep node
node = nep.node('publisher') 
# Set a direct connection using port <3000> and in <'one2many'> mode
# Important: 	You need to change the IP address <'127.0.0.1'> by 
# 		the IP address of your PC running the publisher node
conf = node.direct(ip = '127.0.0.1', port =  3000, mode ='one2many') 
# Create a new publisher with the topic <'test'>
pub = node.new_pub('test','json',conf)

i = 0
while True:
	# You will put your code here, which is doing some analysis...
	i = i + 1
	# The result will be sent using the JSON format (just create a python dictionary)
	msg = {'result':i}
	# Send the message with the next line	
	pub.publish(msg)
	# A small time.sleep is used for visualization purposes 		
	time.sleep(.5)

The code required in Computer B (Subscriber) is shown below.

import nep
import time

# Create a new nep node
node = nep.node("receiver")     
# Set a direct connection using port <3000> and in <'one2many'> mode
# Important: 	You need to change the IP address <'127.0.0.1'> by 
# 		the IP address of you PC running the publisher node                                                   
conf = node.direct(ip = "127.0.0.1", port =  3000, mode ="one2many")                         
# Create a new nep subscriber with the topic <'test'>
sub = node.new_sub('test', "json", conf) 

while True:
    # Read data in a non-blocking mode
    s, msg = sub.listen() 
    # if s == True, then there is data in the socket      
    if s:                       
        print(msg)
    else:
        # An small sleep will reduce computational load
        time.sleep(.0001)

When employing the direct method, it is crucial to accurately configure the IP values in both scripts. This process might pose a slight challenge initially. The following illustration offers a clear example. It's important to keep in mind that both scripts on Computer A and Computer B should share identical IP addresses and modes (either one2many or many2one).

By utilizing the NEP CLI, there is no need to manage multiple IP addresses and port numbers. Nevertheless, it is necessary to configure the same IP address for each script.

Using NEP CLI or NEP+ App all communication is performed in a many2many mode (i.e., multiple publishers can be connected to multiple subscribers using the same topic).

Connecting Computer A and Computer B

The code required in Computer A (nep publisher) is shown below

import nep
import time

# Create a new nep node
node = nep.node('publisher') 
# Important: 	You need to change the IP address <'127.0.0.1'> by 
# 		the IP address of PC running NEP CLI
conf = node.hybrid('127.0.0.1') 
# Create a new publisher with the topic <'test'>
pub = node.new_pub('test','json',conf)

i = 0
while True:
	# You will put your code here, which is doing some analysis...
	i = i + 1
	# The result will be sent using the JSON format (just create a python dictionary)
	msg = {'result':i}
	# Send the message with the next line	
	pub.publish(msg)
	# A small time.sleep is used for visualization purposes 		
	time.sleep(.5)

The code required in Computer B (nep subscriber) is shown below.

import nep
import time

# Create a new nep node
node = nep.node("receiver")     
# Important: 	You need to change the IP address <'127.0.0.1'> by 
# 		the IP address of PC running NEP CLI                                                 
conf = node.hybrid('127.0.0.1')                         
# Create a new nep subscriber with the topic <'test'>
sub = node.new_sub('test', "json", conf) 

while True:
    # Read data in a non-blocking mode
    s, msg = sub.listen() 
    # if s == True, then there is data in the socket      
    if s:                       
        print(msg)
    else:
        # An small sleep will reduce computational load
        time.sleep(.0001)

The next figure shown an example of how to configuarte your application. NEP-CLI must be executed in one computer and all the script must use be configured to communicate to the same IP address. That IP address must be the same that the PC executing NEP-CLI.

This approach can be extended to multiple computers as shown in the next figure.

Method 3: Using NEP+ App (soon)

Soon

Last updated