Skip to content

distributed runtime core for marketrix services (service discovery, communication, data access)

Notifications You must be signed in to change notification settings

Marketrix-ai/mesh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mesh: Cross-Language P2P Communication Library

Overview

Mesh is a lightweight, high-performance library designed to facilitate direct Peer-to-Peer (P2P) communication between services written in different languages (currently Python and Node.js). It abstracts away the complexities of service discovery and connection management, allowing services to interact as if they were local components.

Architecture

Mesh uses a hybrid architecture combining a centralized control plane for discovery with a decentralized data plane for communication.

graph TD
    subgraph "Control Plane (Discovery)"
        Redis[(Redis)]
    end

    subgraph "Data Plane (P2P)"
        ServiceA[Python Service]
        ServiceB[Node.js Service]
    end

    ServiceA -- "1. Register/Heartbeat" --> Redis
    ServiceB -- "1. Register/Heartbeat" --> Redis
    
    ServiceA -- "2. Discover" --> Redis
    ServiceB -- "2. Discover" --> Redis

    ServiceA <== "3. HTTP/2 Stream (JSON)" ==> ServiceB
Loading

Key Components

  1. Service Discovery (Redis): Services register themselves in Redis with a TTL. They maintain presence via periodic heartbeats.
  2. Communication (HTTP/2): Once a peer is discovered, a direct HTTP/2 connection is established. This allows multiplexed, bidirectional streaming over a single TCP connection.
  3. Framing: Messages are sent using a robust Length-Prefixed framing protocol (4-byte header + JSON payload) to ensure data integrity.

Continuous Bidirectional Mesh Communication Walkthrough

This walkthrough demonstrates how to run the sample code for high-throughput, continuous bidirectional communication. Node.js initiates the connection, and then both sides stream messages to each other in parallel.

Prerequisites

  • Docker and Docker Compose
  • Python 3.8+
  • Node.js 18+

1. Start Redis

docker-compose up -d redis

2. Run the Python Peer (Server side for this flow)

Open a terminal, navigate to local mesh/python directory:

cd python
source .venv/bin/activate
python mesh_peer.py

It registers python-peer and waits.

3. Run the Node.js Peer (Client side for this flow)

Open a new terminal, navigate to local mesh/node directory:

cd node
npx ts-node mesh_peer.ts

It registers node-peer, connects to python-peer, and starts streaming messages.

4. Verify Continuous Streaming

You should see a rapid stream of logs in BOTH terminals.

Python Terminal:

... Accepted connection from Node.
... Received: Message from Node #0
... Sent: Message from Python #0
... Received: Message from Node #1
... Sent: Message from Python #1
...

Node.js Terminal:

... Connected to 'python-peer'!
... Sent: Message from Node #0
... Received: Message from Python #0
... Sent: Message from Node #1
... Received: Message from Python #1
...

Installation

Implementing New Clients

If you wish to implement a Mesh client in a new language (e.g., Go, Rust, Java), please refer to the Protocol Specification. This document details the Redis keyspace, HTTP/2 handshake, and message framing requirements.

About

distributed runtime core for marketrix services (service discovery, communication, data access)

Resources

Stars

Watchers

Forks

Packages

No packages published