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.
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
- Service Discovery (Redis): Services register themselves in Redis with a TTL. They maintain presence via periodic heartbeats.
- 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.
- Framing: Messages are sent using a robust Length-Prefixed framing protocol (4-byte header + JSON payload) to ensure data integrity.
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.
- Docker and Docker Compose
- Python 3.8+
- Node.js 18+
docker-compose up -d redisOpen a terminal, navigate to local mesh/python directory:
cd python
source .venv/bin/activate
python mesh_peer.pyIt registers python-peer and waits.
Open a new terminal, navigate to local mesh/node directory:
cd node
npx ts-node mesh_peer.tsIt registers node-peer, connects to python-peer, and starts streaming messages.
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
...
- Node.js: See node/README.md
- Python: See python/README.md
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.