Connecting Multiple Scripts on the Same Computer using NEP+ Tools: A Step-by-Step Tutorial

circle-check

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.

circle-info

Communication between a Publisher socket and a Subscriber socket is generally configured to be asynchronous. Therefore, the Subscriber socket will not block the program execution when trying to read a message.

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:

NEP+ tool
Requirements
Comments

Node.js and nep-cli package installed

For users preferring command line tools

NEP+ app installed

For users preferring graphical interfaces

Step 1: Executing NEP+ environment

If using NEP-CLI

Execute the following command, and don't close the terminal

If using NEP+ App

Just open the NEP+ App and don't close the interface

Step 2: Create a Publisher socket

  1. Importing the necessary modules:

  1. Creating a new nep node:

  1. Create a new Publisher and define the type of message as JSON:

  1. Send a JSON message:

A full example of sending 50 messages each .5 seconds is show below:

Step 3: Create a Subscriber socket (in another script)

  1. Importing the necessary modules:

  1. Creating a new nep node:

  1. Create a new Subscriber and define the type of message as json:

  1. Listen JSON messages:

A full example of a subscriber is show below:

Last updated