update
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# NATSBridge Tutorial
|
||||
|
||||
A step-by-step guide to get started with NATSBridge - a high-performance, bi-directional data bridge for **Julia**, **JavaScript**, and **Python/Micropython**.
|
||||
A step-by-step guide to get started with NATSBridge - a high-performance, bi-directional data bridge for **Julia**.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
@@ -10,13 +10,12 @@ A step-by-step guide to get started with NATSBridge - a high-performance, bi-dir
|
||||
4. [Quick Start](#quick-start)
|
||||
5. [Basic Examples](#basic-examples)
|
||||
6. [Advanced Usage](#advanced-usage)
|
||||
7. [Cross-Platform Communication](#cross-platform-communication)
|
||||
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
NATSBridge enables seamless communication between Julia, JavaScript, and Python/Micropython applications through NATS, with automatic transport selection based on payload size:
|
||||
NATSBridge enables seamless communication for Julia applications through NATS, with automatic transport selection based on payload size:
|
||||
|
||||
- **Direct Transport**: Payloads < 1MB are sent directly via NATS (Base64 encoded)
|
||||
- **Link Transport**: Payloads >= 1MB are uploaded to an HTTP file server and referenced via URL
|
||||
@@ -41,7 +40,7 @@ Before you begin, ensure you have:
|
||||
|
||||
1. **NATS Server** running (or accessible)
|
||||
2. **HTTP File Server** (optional, for large payloads > 1MB)
|
||||
3. **One of the supported platforms**: Julia, JavaScript (Node.js), or Python/Micropython
|
||||
3. **Julia** with required packages
|
||||
|
||||
---
|
||||
|
||||
@@ -59,27 +58,6 @@ Pkg.add("UUIDs")
|
||||
Pkg.add("Dates")
|
||||
```
|
||||
|
||||
### JavaScript
|
||||
|
||||
```bash
|
||||
npm install nats.js apache-arrow uuid base64-url
|
||||
```
|
||||
|
||||
### Python/Micropython
|
||||
|
||||
1. Copy `src/nats_bridge.py` to your device
|
||||
2. Install dependencies:
|
||||
|
||||
**For Python (desktop):**
|
||||
```bash
|
||||
pip install nats-py
|
||||
```
|
||||
|
||||
**For Micropython:**
|
||||
- `urequests` for HTTP requests
|
||||
- `base64` for base64 encoding (built-in)
|
||||
- `json` for JSON handling (built-in)
|
||||
|
||||
---
|
||||
|
||||
## Quick Start
|
||||
@@ -96,48 +74,12 @@ docker run -p 4222:4222 nats:latest
|
||||
# Create a directory for file uploads
|
||||
mkdir -p /tmp/fileserver
|
||||
|
||||
# Use Python's built-in server
|
||||
# Use any HTTP server that supports POST for file uploads
|
||||
python3 -m http.server 8080 --directory /tmp/fileserver
|
||||
```
|
||||
|
||||
### Step 3: Send Your First Message
|
||||
|
||||
#### Python/Micropython
|
||||
|
||||
```python
|
||||
from nats_bridge import smartsend
|
||||
|
||||
# Send a text message (is_publish=True by default)
|
||||
data = [("message", "Hello World", "text")]
|
||||
env, env_json_str = smartsend("/chat/room1", data, broker_url="nats://localhost:4222")
|
||||
print("Message sent!")
|
||||
|
||||
# Or use is_publish=False to get envelope and JSON without publishing
|
||||
env, env_json_str = smartsend("/chat/room1", data, broker_url="nats://localhost:4222", is_publish=False)
|
||||
# env: MessageEnvelope object
|
||||
# env_json_str: JSON string for publishing to NATS
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
|
||||
```javascript
|
||||
const { smartsend } = require('./src/NATSBridge');
|
||||
|
||||
// Send a text message (isPublish=true by default)
|
||||
await smartsend("/chat/room1", [
|
||||
{ dataname: "message", data: "Hello World", type: "text" }
|
||||
], { brokerUrl: "nats://localhost:4222" });
|
||||
|
||||
console.log("Message sent!");
|
||||
|
||||
// Or use isPublish=false to get envelope and JSON without publishing
|
||||
const { env, env_json_str } = await smartsend("/chat/room1", [
|
||||
{ dataname: "message", data: "Hello World", type: "text" }
|
||||
], { brokerUrl: "nats://localhost:4222", isPublish: false });
|
||||
// env: MessageEnvelope object
|
||||
// env_json_str: JSON string for publishing to NATS
|
||||
```
|
||||
|
||||
#### Julia
|
||||
|
||||
```julia
|
||||
@@ -149,33 +91,15 @@ env, env_json_str = smartsend("/chat/room1", data, broker_url="nats://localhost:
|
||||
# env: msg_envelope_v1 object with all metadata and payloads
|
||||
# env_json_str: JSON string representation of the envelope for publishing
|
||||
println("Message sent!")
|
||||
|
||||
# Or use is_publish=false to get envelope and JSON without publishing
|
||||
env, env_json_str = smartsend("/chat/room1", data, broker_url="nats://localhost:4222", is_publish=false)
|
||||
# env: msg_envelope_v1 object
|
||||
# env_json_str: JSON string for publishing to NATS
|
||||
```
|
||||
|
||||
### Step 4: Receive Messages
|
||||
|
||||
#### Python/Micropython
|
||||
|
||||
```python
|
||||
from nats_bridge import smartreceive
|
||||
|
||||
# Receive and process message
|
||||
env = smartreceive(msg)
|
||||
for dataname, data, type in env["payloads"]:
|
||||
print(f"Received {dataname}: {data}")
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
|
||||
```javascript
|
||||
const { smartreceive } = require('./src/NATSBridge');
|
||||
|
||||
// Receive and process message
|
||||
const env = await smartreceive(msg);
|
||||
for (const payload of env.payloads) {
|
||||
console.log(`Received ${payload.dataname}: ${payload.data}`);
|
||||
}
|
||||
```
|
||||
|
||||
#### Julia
|
||||
|
||||
```julia
|
||||
@@ -194,39 +118,6 @@ end
|
||||
|
||||
### Example 1: Sending a Dictionary
|
||||
|
||||
#### Python/Micropython
|
||||
|
||||
```python
|
||||
from nats_bridge import smartsend
|
||||
|
||||
# Create configuration dictionary
|
||||
config = {
|
||||
"wifi_ssid": "MyNetwork",
|
||||
"wifi_password": "password123",
|
||||
"update_interval": 60
|
||||
}
|
||||
|
||||
# Send as dictionary type
|
||||
data = [("config", config, "dictionary")]
|
||||
env, env_json_str = smartsend("/device/config", data, broker_url="nats://localhost:4222")
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
|
||||
```javascript
|
||||
const { smartsend } = require('./src/NATSBridge');
|
||||
|
||||
const config = {
|
||||
wifi_ssid: "MyNetwork",
|
||||
wifi_password: "password123",
|
||||
update_interval: 60
|
||||
};
|
||||
|
||||
const { env, env_json_str } = await smartsend("/device/config", [
|
||||
{ dataname: "config", data: config, type: "dictionary" }
|
||||
], { brokerUrl: "nats://localhost:4222" });
|
||||
```
|
||||
|
||||
#### Julia
|
||||
|
||||
```julia
|
||||
@@ -244,34 +135,6 @@ env, env_json_str = smartsend("/device/config", data, broker_url="nats://localho
|
||||
|
||||
### Example 2: Sending Binary Data (Image)
|
||||
|
||||
#### Python/Micropython
|
||||
|
||||
```python
|
||||
from nats_bridge import smartsend
|
||||
|
||||
# Read image file
|
||||
with open("image.png", "rb") as f:
|
||||
image_data = f.read()
|
||||
|
||||
# Send as binary type
|
||||
data = [("user_image", image_data, "binary")]
|
||||
env, env_json_str = smartsend("/chat/image", data, broker_url="nats://localhost:4222")
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
|
||||
```javascript
|
||||
const { smartsend } = require('./src/NATSBridge');
|
||||
|
||||
// Read image file (Node.js)
|
||||
const fs = require('fs');
|
||||
const image_data = fs.readFileSync('image.png');
|
||||
|
||||
const { env, env_json_str } = await smartsend("/chat/image", [
|
||||
{ dataname: "user_image", data: image_data, type: "binary" }
|
||||
], { brokerUrl: "nats://localhost:4222" });
|
||||
```
|
||||
|
||||
#### Julia
|
||||
|
||||
```julia
|
||||
@@ -286,13 +149,13 @@ env, env_json_str = smartsend("/chat/image", data, broker_url="nats://localhost:
|
||||
|
||||
### Example 3: Request-Response Pattern
|
||||
|
||||
#### Python/Micropython (Requester)
|
||||
#### Julia (Requester)
|
||||
|
||||
```python
|
||||
from nats_bridge import smartsend
|
||||
```julia
|
||||
using NATSBridge
|
||||
|
||||
# Send command with reply-to
|
||||
data = [("command", {"action": "read_sensor"}, "dictionary")]
|
||||
data = [("command", Dict("action" => "read_sensor"), "dictionary")]
|
||||
env, env_json_str = smartsend(
|
||||
"/device/command",
|
||||
data,
|
||||
@@ -300,44 +163,43 @@ env, env_json_str = smartsend(
|
||||
reply_to="/device/response",
|
||||
reply_to_msg_id="cmd-001"
|
||||
)
|
||||
# env: MessageEnvelope object
|
||||
# env: msg_envelope_v1 object
|
||||
# env_json_str: JSON string for publishing to NATS
|
||||
```
|
||||
|
||||
#### JavaScript (Responder)
|
||||
#### Julia (Responder)
|
||||
|
||||
```javascript
|
||||
const { smartreceive, smartsend } = require('./src/NATSBridge');
|
||||
```julia
|
||||
using NATS, NATSBridge
|
||||
|
||||
// Subscribe to command topic
|
||||
const sub = nc.subscribe("/device/command");
|
||||
# Configuration
|
||||
const SUBJECT = "/device/command"
|
||||
const NATS_URL = "nats://localhost:4222"
|
||||
|
||||
for await (const msg of sub) {
|
||||
const env = await smartreceive(msg);
|
||||
function test_responder()
|
||||
conn = NATS.connect(NATS_URL)
|
||||
NATS.subscribe(conn, SUBJECT) do msg
|
||||
env = smartreceive(msg, fileserver_download_handler=_fetch_with_backoff)
|
||||
|
||||
# Extract reply_to from the envelope metadata
|
||||
reply_to = env["reply_to"]
|
||||
|
||||
for (dataname, data, type) in env["payloads"]
|
||||
if dataname == "command" && data["action"] == "read_sensor"
|
||||
response = Dict("sensor_id" => "sensor-001", "value" => 42.5)
|
||||
# Send response to the reply_to subject from the request
|
||||
if !isempty(reply_to)
|
||||
smartsend(reply_to, [("data", response, "dictionary")])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
// Process command
|
||||
for (const payload of env.payloads) {
|
||||
if (payload.dataname === "command") {
|
||||
const command = payload.data;
|
||||
|
||||
if (command.action === "read_sensor") {
|
||||
// Read sensor and send response
|
||||
const response = {
|
||||
sensor_id: "sensor-001",
|
||||
value: 42.5,
|
||||
timestamp: new Date().toISOString()
|
||||
};
|
||||
|
||||
await smartsend("/device/response", [
|
||||
{ dataname: "sensor_data", data: response, type: "dictionary" }
|
||||
], {
|
||||
reply_to: env.replyTo,
|
||||
reply_to_msg_id: env.msgId
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
sleep(120)
|
||||
NATS.drain(conn)
|
||||
end
|
||||
|
||||
test_responder()
|
||||
```
|
||||
|
||||
---
|
||||
@@ -348,47 +210,6 @@ for await (const msg of sub) {
|
||||
|
||||
For payloads larger than 1MB, NATSBridge automatically uses the file server:
|
||||
|
||||
#### Python/Micropython
|
||||
|
||||
```python
|
||||
from nats_bridge import smartsend
|
||||
import os
|
||||
|
||||
# Create large data (> 1MB)
|
||||
large_data = os.urandom(2_000_000) # 2MB of random data
|
||||
|
||||
# Send with file server URL
|
||||
env, env_json_str = smartsend(
|
||||
"/data/large",
|
||||
[("large_file", large_data, "binary")],
|
||||
broker_url="nats://localhost:4222",
|
||||
fileserver_url="http://localhost:8080",
|
||||
size_threshold=1_000_000
|
||||
)
|
||||
|
||||
# The envelope will contain the download URL
|
||||
print(f"File uploaded to: {env.payloads[0].data}")
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
|
||||
```javascript
|
||||
const { smartsend } = require('./src/NATSBridge');
|
||||
|
||||
// Create large data (> 1MB)
|
||||
const largeData = new ArrayBuffer(2_000_000);
|
||||
const view = new Uint8Array(largeData);
|
||||
view.fill(42); // Fill with some data
|
||||
|
||||
const { env, env_json_str } = await smartsend("/data/large", [
|
||||
{ dataname: "large_file", data: largeData, type: "binary" }
|
||||
], {
|
||||
brokerUrl: "nats://localhost:4222",
|
||||
fileserverUrl: "http://localhost:8080",
|
||||
sizeThreshold: 1_000_000
|
||||
});
|
||||
```
|
||||
|
||||
#### Julia
|
||||
|
||||
```julia
|
||||
@@ -412,45 +233,6 @@ println("File uploaded to: $(env.payloads[1].data)")
|
||||
|
||||
NATSBridge supports sending multiple payloads with different types in a single message:
|
||||
|
||||
#### Python/Micropython
|
||||
|
||||
```python
|
||||
from nats_bridge import smartsend
|
||||
|
||||
# Read image file
|
||||
with open("avatar.png", "rb") as f:
|
||||
image_data = f.read()
|
||||
|
||||
# Send mixed content
|
||||
data = [
|
||||
("message_text", "Hello with image!", "text"),
|
||||
("user_avatar", image_data, "image")
|
||||
]
|
||||
|
||||
env, env_json_str = smartsend("/chat/mixed", data, broker_url="nats://localhost:4222")
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
|
||||
```javascript
|
||||
const { smartsend } = require('./src/NATSBridge');
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
const { env, env_json_str } = await smartsend("/chat/mixed", [
|
||||
{
|
||||
dataname: "message_text",
|
||||
data: "Hello with image!",
|
||||
type: "text"
|
||||
},
|
||||
{
|
||||
dataname: "user_avatar",
|
||||
data: fs.readFileSync("avatar.png"),
|
||||
type: "image"
|
||||
}
|
||||
], { brokerUrl: "nats://localhost:4222" });
|
||||
```
|
||||
|
||||
#### Julia
|
||||
|
||||
```julia
|
||||
@@ -470,24 +252,6 @@ env, env_json_str = smartsend("/chat/mixed", data, broker_url="nats://localhost:
|
||||
|
||||
For tabular data, NATSBridge uses Apache Arrow IPC format:
|
||||
|
||||
#### Python/Micropython
|
||||
|
||||
```python
|
||||
from nats_bridge import smartsend
|
||||
import pandas as pd
|
||||
|
||||
# Create DataFrame
|
||||
df = pd.DataFrame({
|
||||
"id": [1, 2, 3],
|
||||
"name": ["Alice", "Bob", "Charlie"],
|
||||
"score": [95, 88, 92]
|
||||
})
|
||||
|
||||
# Send as table type
|
||||
data = [("students", df, "table")]
|
||||
env, env_json_str = smartsend("/data/students", data, broker_url="nats://localhost:4222")
|
||||
```
|
||||
|
||||
#### Julia
|
||||
|
||||
```julia
|
||||
@@ -507,92 +271,10 @@ env, env_json_str = smartsend("/data/students", data, broker_url="nats://localho
|
||||
|
||||
---
|
||||
|
||||
## Cross-Platform Communication
|
||||
|
||||
NATSBridge enables seamless communication between different platforms:
|
||||
|
||||
### Julia ↔ JavaScript
|
||||
|
||||
#### Julia Sender
|
||||
|
||||
```julia
|
||||
using NATSBridge
|
||||
|
||||
# Send dictionary from Julia to JavaScript
|
||||
config = Dict("step_size" => 0.01, "iterations" => 1000)
|
||||
data = [("config", config, "dictionary")]
|
||||
env, env_json_str = smartsend("/analysis/config", data, broker_url="nats://localhost:4222")
|
||||
```
|
||||
|
||||
#### JavaScript Receiver
|
||||
|
||||
```javascript
|
||||
const { smartreceive } = require('./src/NATSBridge');
|
||||
|
||||
// Receive dictionary from Julia
|
||||
const env = await smartreceive(msg);
|
||||
for (const payload of env.payloads) {
|
||||
if (payload.type === "dictionary") {
|
||||
console.log("Received config:", payload.data);
|
||||
// payload.data = { step_size: 0.01, iterations: 1000 }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### JavaScript ↔ Python
|
||||
|
||||
#### JavaScript Sender
|
||||
|
||||
```javascript
|
||||
const { smartsend } = require('./src/NATSBridge');
|
||||
|
||||
const { env, env_json_str } = await smartsend("/data/transfer", [
|
||||
{ dataname: "message", data: "Hello from JS!", type: "text" }
|
||||
], { brokerUrl: "nats://localhost:4222" });
|
||||
```
|
||||
|
||||
#### Python Receiver
|
||||
|
||||
```python
|
||||
from nats_bridge import smartreceive
|
||||
|
||||
env = smartreceive(msg)
|
||||
for dataname, data, type in env["payloads"]:
|
||||
if type == "text":
|
||||
print(f"Received from JS: {data}")
|
||||
```
|
||||
|
||||
### Python ↔ Julia
|
||||
|
||||
#### Python Sender
|
||||
|
||||
```python
|
||||
from nats_bridge import smartsend
|
||||
|
||||
data = [("message", "Hello from Python!", "text")]
|
||||
env, env_json_str = smartsend("/chat/python", data, broker_url="nats://localhost:4222")
|
||||
```
|
||||
|
||||
#### Julia Receiver
|
||||
|
||||
```julia
|
||||
using NATSBridge
|
||||
|
||||
env = smartreceive(msg; fileserver_download_handler=_fetch_with_backoff)
|
||||
for (dataname, data, type) in env["payloads"]
|
||||
if type == "text"
|
||||
println("Received from Python: $data")
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Next Steps
|
||||
|
||||
1. **Explore the test directory** for more examples
|
||||
2. **Check the documentation** for advanced configuration options
|
||||
3. **Join the community** to share your use cases
|
||||
|
||||
---
|
||||
|
||||
@@ -613,7 +295,7 @@ end
|
||||
### Serialization Errors
|
||||
|
||||
- Verify data type matches the specified type
|
||||
- Check that binary data is in the correct format (bytes/Vector{UInt8})
|
||||
- Check that binary data is in the correct format (Vector{UInt8})
|
||||
|
||||
---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user