# 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="/pages/Dr9ei5u9hGUsr5yJogkl#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="/pages/aWvjt2wMHOQrfJYJeJia">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="/pages/Dr9ei5u9hGUsr5yJogkl#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="/pages/aWvjt2wMHOQrfJYJeJia">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="/files/ncabgSYz1pRE21aTXoc8" 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="/files/8HXvADFUYakM8HPifYyZ" 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="/files/JsgPB30MjraiLhMEFB4U" alt=""><figcaption><p>Example using NEP-CLI and multiple computers</p></figcaption></figure>

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

Soon


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coronadoenrique.gitbook.io/nep+/concepts-for-developers/tutorials-for-developers/connecting-2-scripts-in-a-local-network-e.g.-two-computers-on-the-same-wifi-network.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
