Connecting Multiple Scripts on the Same Computer using NEP+ Tools: A Step-by-Step Tutorial
In this tutorial, we will guide you through the process of connecting two or more scripts that reside on the same computer using NEP+ tools and libraries
Background:
In this tutorial, we will explore the Publisher-Subscriber pattern for sending messages between scripts. A fascinating aspect of this approach is its ability to connect scripts written in different programming languages.
What is the Publisher-Subscriber pattern?
The publisher-subscriber pattern is a messaging design pattern where publishers and subscribers interact without direct knowledge of each other. Publishers are responsible for sending messages, while subscribers express interest in receiving specific types of messages.
The publisher-subscriber pattern offers the advantage of asynchrony, allowing publishers to send messages without waiting for subscribers to process them and enabling subscribers to receive messages at their own pace. This asynchrony enhances system responsiveness and overall performance by decoupling the timing of message production and consumption.
The publisher-subscriber pattern provides the benefit of loose coupling, which enables independent development and maintenance of publishers and subscribers.
What is a topic?
In the publisher-subscriber pattern, a topic is a category or subject assigned to messages. Publishers use topics to indicate the content of messages, while subscribers express interest in receiving messages based on specific topics. Topics enable subscribers to filter and receive only relevant messages, optimizing communication efficiency. They serve as a classification mechanism that helps organize and target messages based on shared interests or message types.
What is a node?
In the context of distributed systems or network architectures, a node refers to an individual device or entity that participates in the system. Each node typically has a unique identifier, commonly known as a node ID or node name. The node name is a distinctive value assigned to the node, allowing it to be uniquely identified and distinguished from other nodes within the system.
What is a message type?
The message type or format refers to the structure and encoding of the data within a message. It defines how the data is organized, represented, and interpreted by the publisher and subscriber components. JSON (JavaScript Object Notation) is a commonly used format that specifies the structure of the message using key-value pairs. With NEP+, publishers and subscribers can encode and decode messages in JSON format, ensuring a shared understanding of the data's structure and facilitating consistent communication within the publisher-subscriber system.
Prerequisites:
Before you proceed with this tutorial, make sure you have one of the following NEP+ tools installed on your computer:
Step 1: Executing NEP+ environment
If using NEP-CLI
Execute the following command, and don't close the terminal
nep master
If using NEP+ App
Just open the NEP+ App and don't close the interface
Step 2: Create a Publisher socket
Importing the necessary modules:
import nep
import time
Creating a new nep node:
node = nep.node('basic_pub_py')
Create a new Publisher and define the type of message as JSON:
pub = node.new_pub('test','json')
Send a JSON message:
# Define the message to send using the JSON format (a python dictionary)
msg = {'message':0}
# Send the message with the next line
pub.publish(msg)
A full example of sending 50 messages each .5 seconds is show below:
import nep
import time
# Create a new nep node
# The parameter given to this function must be different for each script
node = nep.node('basic_pub_py')
# Create a new publisher with the topic 'test'
pub = node.new_pub('test','json')
i = 0
while i<50 :
# You will put your code here ...
i = i + 1
# Define the message to send using the JSON format (a python dictionary)
msg = {'message':i}
# Send the message with the next line
pub.publish(msg)
# Use the next line to send messages each 0.5 seconds approx.
time.sleep(.5)
print('Publisher program finished!')
Step 3: Create a Subscriber socket (in another script)
Importing the necessary modules:
import nep
import time
Creating a new nep node:
node = nep.node('basic_sub_py')
Create a new Subscriber and define the type of message as json:
sub = node.new_sub('test','json')
Listen JSON messages:
s, msg = sub.listen()
# if s == True, then there is data in the socket
if s:
# Print messsage
print(msg)
# otherwise, the program does not block
else:
# A small sleep is not compulsory but can reduce the computational load
time.sleep(.0001)
A full example of a subscriber is show below:
import nep
import time
# Create a new nep node. The name set to a NEP node must be different for each script
node = nep.node('basic_sub_py')
# Create a new subscriber with the topic 'test'
sub = node.new_sub('test','json')
i = 0
while i<50:
# 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)
i = i + 1
# otherwise, the program does not block
else:
# A small sleep is not compulsory but can reduce the computational load
time.sleep(.0001)
print('Subscriber program finished!')
Last updated