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

{% hint style="success" %}
This tutorial will show you how to connect two or more computers that are on the same local network (e.g., connected to the same Wifi network) using NEP+ tools.
{% endhint %}

{% hint style="danger" %}
**For Windows users**: In some computers, the antivirus will not allow local network connections using ZMQ sockets. A fast solution for testing purposes can be to disable the antivirus.
{% endhint %}

We will explore three different methods:

<table><thead><tr><th width="174.04817179338363">Method</th><th>Requirements</th><th>Advantages</th><th>Disadvantages</th></tr></thead><tbody><tr><td><a href="#using-a-direct-connection">Direct</a></td><td>NEP libraries installed (also required in the other methods)</td><td>Less tools to install</td><td>It can be annoying to deal with IP addresses, communication modes and port numbers </td></tr><tr><td><a href="../../../developer-tools/nep-cli#manage-connection-between-nodes">NEP CLI</a></td><td>Node.js and nep-cli package installed</td><td>You don't need to care anymore about port numbers and modes. </td><td>You need to remember and use the same  IP address for all your nodes.</td></tr><tr><td><a href="../../user-interfaces/nep+-app-0.0.4-deprecated">Nep+ App</a></td><td>NEP+ app installed</td><td>Same that NEP CLI </td><td>Same that NEP CLI</td></tr></tbody></table>

Where it is recommended to use these methods?

<table><thead><tr><th width="135.02081397402162">Method</th><th>When is recommended?</th></tr></thead><tbody><tr><td><a href="#using-a-direct-connection">Direct</a></td><td>When you only need to connect two modules without caring about the scalability of the project</td></tr><tr><td><a href="../../../developer-tools/nep-cli#manage-connection-between-nodes">NEP CLI</a></td><td>When will you use your modules in many projects and care about reusability and scalability</td></tr><tr><td><p></p><p><a href="../../user-interfaces/nep+-app-0.0.4-deprecated">Nep+ App</a></p></td><td>Same as nep-cli but for users preferring a user interface</td></tr></tbody></table>

## 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.

{% hint style="info" %}
The command **ipconfig** (in Windows) or **ifconfig** (in Linux and OSX) can be used to know the IP address of your computer.
{% endhint %}

{% hint style="warning" %}
**Remember:** All the devices/computers must be in the same Wifi or Ethernet local network.
{% endhint %}

### Connecting Computer A and Computer B

{% hint style="warning" %}
To ensure proper configuration, replace the IP address <mark style="color:blue;">**127.0.0.1**</mark> in the examples with the correct IP of your computer within the Wi-Fi or Ethernet network.

In the case of the <mark style="color:green;">**one2many**</mark> mode, which involves a single publisher and multiple subscribers, you need to set the IP address of the computer executing the <mark style="color:blue;">**publisher**</mark> in both scripts executing the publisher and subscriber.

In the case of the <mark style="color:green;">**many2one**</mark> mode, which involves multiple publishers sending messages to a single subscriber, you need to set the IP address of the computer executing the <mark style="color:blue;">**subscriber**</mark> in both scripts executing the publisher and subscriber.

To use a <mark style="color:green;">**many2many**</mark> mode it is required to use the <mark style="color:blue;">**NEP-CLI**</mark> or <mark style="color:blue;">**NEP+ App**</mark> method.
{% endhint %}

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

{% tabs %}
{% tab title="Python" %}
```python
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)
```
{% endtab %}

{% tab title="C#" %}
Soon
{% endtab %}

{% tab title="Javascript" %}

{% endtab %}

{% tab title="C++" %}
Soon
{% endtab %}

{% tab title="Java" %}
Soon
{% endtab %}
{% endtabs %}

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

{% tabs %}
{% tab title="Python" %}
```python
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)
```
{% endtab %}

{% tab title="C#" %}
Soon
{% endtab %}

{% tab title="Javascript" %}
Soon
{% endtab %}

{% tab title="C++" %}
Soon
{% endtab %}

{% tab title="Java" %}
Soon
{% endtab %}
{% endtabs %}

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).

<figure><img src="https://423882023-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9aELFWec9V26rEZKHjDA%2Fuploads%2FICOM2wzagu5IyjnAEU8d%2FScreen%20Shot%202023-08-30%20at%2010.38.52.png?alt=media&#x26;token=f0d476f3-a1a1-4397-9816-76e92173b288" alt=""><figcaption><p>Example of how to set IP values for direct method</p></figcaption></figure>

## Method 2: Using NEP CLI (recommended)

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.&#x20;

{% hint style="info" %}
Using NEP CLI or NEP+ App all communication is performed in a <mark style="color:blue;">**many2many**</mark> mode (i.e., multiple publishers can be connected to multiple subscribers using the same topic).&#x20;
{% endhint %}

### Connecting Computer A and Computer B

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

{% tabs %}
{% tab title="Python" %}
```python
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)
```
{% endtab %}

{% tab title="C#" %}
Soon
{% endtab %}

{% tab title="Javascript" %}

{% endtab %}

{% tab title="C++" %}
Soon
{% endtab %}

{% tab title="Java" %}
Soon
{% endtab %}
{% endtabs %}

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

{% tabs %}
{% tab title="Python" %}
```python
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)
```
{% endtab %}

{% tab title="C#" %}
Soon
{% endtab %}

{% tab title="Javascript" %}
Soon
{% endtab %}

{% tab title="C++" %}
Soon
{% endtab %}

{% tab title="Java" %}
Soon
{% endtab %}
{% endtabs %}

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.

<figure><img src="https://423882023-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9aELFWec9V26rEZKHjDA%2Fuploads%2FIHLYY2oqwUHI31a7zO7J%2FScreen%20Shot%202023-08-30%20at%2011.06.08.png?alt=media&#x26;token=42123438-6b17-4701-bc69-3c9e65bc1519" alt=""><figcaption><p>Example using NEP-CLI </p></figcaption></figure>

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

<figure><img src="https://423882023-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9aELFWec9V26rEZKHjDA%2Fuploads%2FCTCmGVXJoROD6U54Dm4C%2FScreen%20Shot%202023-08-30%20at%2011.11.27.png?alt=media&#x26;token=6f5d5b43-bdf1-4911-88d1-6554b02543a5" alt=""><figcaption><p>Example using NEP-CLI and multiple computers</p></figcaption></figure>

## Method 3: Using NEP+ App (soon)

Soon
