Compare commits
22 Commits
update_doc
...
6da64092ca
| Author | SHA1 | Date | |
|---|---|---|---|
| 6da64092ca | |||
| 809aea454b | |||
| c5a70edd57 | |||
| b0acee053c | |||
| c25c6a8a43 | |||
| 34a6d19303 | |||
| 8ada1ca49c | |||
| 60ae464ea2 | |||
| c20a266e72 | |||
| 4f141b130e | |||
| fa039f2820 | |||
| b0c5ecb942 | |||
| ba659368a5 | |||
| 1b38b3d6f1 | |||
| ad28386ff0 | |||
| 9c4c941840 | |||
| 34d8e3fad8 | |||
| 49d7898720 | |||
| fb315a0525 | |||
| 07acde45da | |||
| 3c6e139ac0 | |||
| 50211b671d |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
node_modules/
|
||||
package.json
|
||||
package-lock.json
|
||||
target/
|
||||
@@ -143,12 +143,60 @@ Since I develop src folder before I adopt SDD_FRAMEWORK.md approach, can you che
|
||||
|
||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
||||
|
||||
I updated src/NATSBridge.jl. Check and NATSBridge/docs folder I want to update the content of the following files according to ASG_Framework/ASG_Framework.md:
|
||||
- NATSBridge/docs/requirements.md
|
||||
- NATSBridge/docs/specification.md
|
||||
- NATSBridge/docs/ui-specification.md (you'll need to create this one)
|
||||
- NATSBridge/docs/walkthrough.md
|
||||
- NATSBridge/docs/architecture.md
|
||||
I'll do the other docs not listed here later myself.
|
||||
|
||||
|
||||
|
||||
now help me update the following file according to ASG_Framework/ASG_Framework.md:
|
||||
- NATSBridge/docs/specification.md
|
||||
|
||||
|
||||
<!-- ------------------------------------------- 100 ------------------------------------------- -->
|
||||
|
||||
|
||||
Check ./docs folder. I would like to expand this package (NATSBRIDGE) to include Rust support.
|
||||
Can you update the content of the following files according to /home/ton/docker-apps/sommpanion/ASG_Framework/ASG_Framework.md:
|
||||
- ./docs/requirements.md
|
||||
- ./docs/specification.md
|
||||
- ./docs/walkthrough.md
|
||||
- ./docs/architecture.md
|
||||
|
||||
|
||||
|
||||
|
||||
<!-- ------------------------------------------- 100 ------------------------------------------- -->
|
||||
|
||||
I updated ./src/NATSBridge.jl. Use it as groundtruth. Check ./docs folder I want to update the content of the following files according to /home/ton/docker-apps/sommpanion/ASG_Framework/ASG_Framework.md:
|
||||
- ./docs/requirements.md
|
||||
- ./docs/specification.md
|
||||
- ./docs/walkthrough.md
|
||||
- ./docs/architecture.md
|
||||
|
||||
|
||||
|
||||
|
||||
Check the following files:
|
||||
- ./docs/requirements.md
|
||||
- ./docs/specification.md
|
||||
- ./docs/architecture.md
|
||||
- ./docs/walkthrough.md
|
||||
I would like to expand this package (NATSBRIDGE) to include Rust support.
|
||||
Now help me update Rust implementation of this package at ./src/natsbridge.rs.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
I want to build a client-side-rendering Dioxus-based chat webapp.
|
||||
Dioxus version 0.7+ should be great.
|
||||
I already populate the current folder for the project.
|
||||
my server REST API endpoint is sommpanion.yiem.cc/agent-fronent/api/v1/chat but I didn't run the server yet. A message format is JSON string.
|
||||
I just placed my custom package for encode and decode message at ./src/natsbridge.rs. smartsend() is for encoding and smartreceive() is for decoding.
|
||||
you may also check the file /home/ton/docker-apps/sommpanion/NATSBridge/docs/walkthrough.md for more info about my package.
|
||||
You can test whether Dioxus webapp can be build using this command "dx bundle --web --release --debug-symbols=false"
|
||||
1915
Cargo.lock
generated
Normal file
1915
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
31
Cargo.toml
Normal file
31
Cargo.toml
Normal file
@@ -0,0 +1,31 @@
|
||||
[package]
|
||||
name = "natsbridge"
|
||||
version = "1.2.0"
|
||||
edition = "2021"
|
||||
description = "Cross-platform bi-directional data bridge for NATS communication"
|
||||
|
||||
[lib]
|
||||
name = "natsbridge"
|
||||
path = "src/natsbridge.rs"
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
reqwest = { version = "0.12", features = ["json", "stream", "multipart"] }
|
||||
uuid = { version = "1", features = ["v4", "serde"] }
|
||||
base64 = "0.22"
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
async-trait = "0.1"
|
||||
futures = "0.3"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3"
|
||||
|
||||
[[example]]
|
||||
name = "smartsend_example"
|
||||
path = "examples/smartsend_example.rs"
|
||||
|
||||
[[example]]
|
||||
name = "smartreceive_example"
|
||||
path = "examples/smartreceive_example.rs"
|
||||
@@ -1,6 +1,6 @@
|
||||
name = "NATSBridge"
|
||||
uuid = "f2724d33-f338-4a57-b9f8-1be882570d10"
|
||||
version = "0.5.5"
|
||||
version = "0.5.6"
|
||||
authors = ["narawat <narawat@gmail.com>"]
|
||||
|
||||
[deps]
|
||||
|
||||
377
README.md
377
README.md
@@ -1,6 +1,6 @@
|
||||
# NATSBridge - Cross-Platform Bi-Directional Data Bridge
|
||||
|
||||
A high-performance, bi-directional data bridge for **Julia, JavaScript, Python, and MicroPython** applications using NATS (Core & JetStream), implementing the Claim-Check pattern for large payloads.
|
||||
A high-performance, bi-directional data bridge for **Julia**, **JavaScript**, **Python**, and **MicroPython** applications using NATS (Core & JetStream), implementing the Claim-Check pattern for large payloads.
|
||||
|
||||
[](https://opensource.org/licenses/MIT)
|
||||
[](https://nats.io)
|
||||
@@ -28,8 +28,8 @@ NATSBridge enables seamless communication across multiple platforms through NATS
|
||||
|
||||
| Transport | Payload Size | Method |
|
||||
|-----------|--------------|--------|
|
||||
| **Direct** | < 1MB | Sent directly via NATS (Base64 encoded) |
|
||||
| **Link** | >= 1MB | Uploaded to HTTP file server, URL sent via NATS |
|
||||
| **Direct** | < 500KB | Sent directly via NATS (Base64 encoded) |
|
||||
| **Link** | ≥ 500KB | Uploaded to HTTP file server, URL sent via NATS |
|
||||
|
||||
### Use Cases
|
||||
|
||||
@@ -45,9 +45,9 @@ NATSBridge enables seamless communication across multiple platforms through NATS
|
||||
| Platform | Implementation | Features |
|
||||
|----------|----------------|----------|
|
||||
| **Julia** | [`src/NATSBridge.jl`](src/NATSBridge.jl) | Full feature set, Arrow IPC, multiple dispatch |
|
||||
| **JavaScript** | [`src/natsbridge.js`](src/natsbridge.js) | Node.js, async/await |
|
||||
| **JavaScript (Browser)** | [`src/natsbridge_csr.js`](src/natsbridge_csr.js) | Browser, WebSocket NATS, async/await |
|
||||
| **Python** | [`src/natsbridge.py`](src/natsbridge.py) | Desktop Python, asyncio, type hints |
|
||||
| **JavaScript (Node.js)** | [`src/natsbridge_ssr.js`](src/natsbridge_ssr.js) | Node.js, async/await, Arrow IPC |
|
||||
| **JavaScript (Browser)** | [`src/natsbridge_csr.js`](src/natsbridge_csr.js) | Browser, WebSocket NATS, async/await, JSON table only |
|
||||
| **Python** | [`src/natsbridge.py`](src/natsbridge.py) | Desktop Python, asyncio, type hints, Arrow IPC |
|
||||
| **MicroPython** | [`src/natsbridge_mpy.py`](src/natsbridge_mpy.py) | Memory-constrained, synchronous API |
|
||||
|
||||
### Platform Comparison
|
||||
@@ -57,7 +57,8 @@ NATSBridge enables seamless communication across multiple platforms through NATS
|
||||
| Multiple Dispatch | ✅ Native | ❌ | ❌ | ❌ | ❌ |
|
||||
| Async/Await | ❌ | ✅ Native | ✅ Native | ✅ Native | ⚠️ (uasyncio) |
|
||||
| Type Safety | ✅ Strong | ⚠️ (TypeScript) | ⚠️ (TypeScript) | ✅ (Type hints) | ❌ |
|
||||
| Arrow IPC | ✅ Native | ✅ | ✅ | ✅ | ❌ |
|
||||
| Arrow IPC | ✅ Native | ✅ Native | ❌ (Browser incompatible) | ✅ Native | ❌ |
|
||||
| JSON Table | ✅ | ✅ | ✅ (Only table type) | ✅ | ⚠️ (Limited) |
|
||||
| Direct Transport | ✅ | ✅ | ✅ | ✅ | ✅ |
|
||||
| Link Transport | ✅ | ✅ | ✅ | ✅ | ⚠️ (Limited) |
|
||||
| Handler Functions | ✅ | ✅ | ✅ | ✅ | ✅ |
|
||||
@@ -72,8 +73,9 @@ NATSBridge enables seamless communication across multiple platforms through NATS
|
||||
- ✅ **Bi-directional messaging** with request-reply patterns
|
||||
- ✅ **Multi-payload support** - send multiple payloads with different types in one message
|
||||
- ✅ **Automatic transport selection** - direct vs link based on payload size
|
||||
- ✅ **Claim-Check pattern** for payloads > 1MB
|
||||
- ✅ **Apache Arrow IPC** support for tabular data (zero-copy reading)
|
||||
- ✅ **Claim-Check pattern** for payloads ≥ 500KB
|
||||
- ✅ **Apache Arrow IPC** support for tabular data (Desktop: Julia/Python/Node.js)
|
||||
- ✅ **JSON Table** support for tabular data (All platforms including Browser)
|
||||
- ✅ **Exponential backoff** for reliable file server downloads
|
||||
- ✅ **Correlation ID tracking** for message tracing
|
||||
- ✅ **Reply-to support** for request-response patterns
|
||||
@@ -83,23 +85,24 @@ NATSBridge enables seamless communication across multiple platforms through NATS
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Step 1: Start NATS Server
|
||||
### Prerequisites
|
||||
|
||||
```bash
|
||||
docker run -p 4222:4222 nats:latest
|
||||
```
|
||||
1. **NATS Server** - Install and run a NATS server:
|
||||
```bash
|
||||
docker run -p 4222:4222 nats:latest
|
||||
```
|
||||
|
||||
### Step 2: Start HTTP File Server (Optional)
|
||||
2. **HTTP File Server** (optional, for large payloads) - Install and run a file server:
|
||||
```bash
|
||||
# Using Plik
|
||||
docker run -p 8080:8080 -v /tmp/fileserver:/var/lib/plik -e PLIK_ADMIN_PASSWORD=admin plik/plik
|
||||
|
||||
# OR using simple Python HTTP server
|
||||
mkdir -p /tmp/fileserver
|
||||
python3 -m http.server 8080 --directory /tmp/fileserver
|
||||
```
|
||||
|
||||
```bash
|
||||
# Create a directory for file uploads
|
||||
mkdir -p /tmp/fileserver
|
||||
|
||||
# Start HTTP file server
|
||||
python3 -m http.server 8080 --directory /tmp/fileserver
|
||||
```
|
||||
|
||||
### Step 3: Send Your First Message
|
||||
### Send Your First Message
|
||||
|
||||
#### Julia
|
||||
|
||||
@@ -107,14 +110,14 @@ python3 -m http.server 8080 --directory /tmp/fileserver
|
||||
using NATSBridge
|
||||
|
||||
data = [("message", "Hello World", "text")]
|
||||
env, env_json_str = smartsend("/chat/room1", data, broker_url="nats://localhost:4222")
|
||||
env, env_json_str = smartsend("/chat/room1", data; broker_url="nats://localhost:4222")
|
||||
println("Message sent!")
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
#### JavaScript (Node.js)
|
||||
|
||||
```javascript
|
||||
const NATSBridge = require('./src/natsbridge.js');
|
||||
import NATSBridge from './src/natsbridge_ssr.js';
|
||||
|
||||
const data = [["message", "Hello World", "text"]];
|
||||
const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
@@ -125,6 +128,20 @@ const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
console.log("Message sent!");
|
||||
```
|
||||
|
||||
#### JavaScript (Browser)
|
||||
|
||||
```javascript
|
||||
import NATSBridge from './src/natsbridge_csr.js';
|
||||
|
||||
const data = [["message", "Hello World", "text"]];
|
||||
const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
"/chat/room1",
|
||||
data,
|
||||
{ broker_url: "ws://localhost:4222" }
|
||||
);
|
||||
console.log("Message sent!");
|
||||
```
|
||||
|
||||
#### Python
|
||||
|
||||
```python
|
||||
@@ -139,6 +156,21 @@ env, env_json_str = await smartsend(
|
||||
print("Message sent!")
|
||||
```
|
||||
|
||||
#### MicroPython
|
||||
|
||||
```python
|
||||
from natsbridge import smartsend
|
||||
|
||||
data = [("message", "Hello World", "text")]
|
||||
env, env_json_str = smartsend(
|
||||
"/chat/room1",
|
||||
data,
|
||||
broker_url="nats://localhost:4222",
|
||||
size_threshold=100000 # 100KB for MicroPython
|
||||
)
|
||||
print("Message sent!")
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## API Reference
|
||||
@@ -147,13 +179,13 @@ print("Message sent!")
|
||||
|
||||
All platforms use the same input/output format for payloads:
|
||||
|
||||
**Input format for smartsend:**
|
||||
**Input format for `smartsend`:**
|
||||
```
|
||||
[(dataname1, data1, type1), (dataname2, data2, type2), ...]
|
||||
```
|
||||
|
||||
**Output format for smartreceive:**
|
||||
```
|
||||
**Output format for `smartreceive`:**
|
||||
```json
|
||||
{
|
||||
"correlation_id": "...",
|
||||
"msg_id": "...",
|
||||
@@ -187,7 +219,7 @@ env, env_json_str = NATSBridge.smartsend(
|
||||
broker_url::String = "nats://localhost:4222",
|
||||
fileserver_url = "http://localhost:8080",
|
||||
fileserver_upload_handler::Function = plik_oneshot_upload,
|
||||
size_threshold::Int = 1_000_000,
|
||||
size_threshold::Int = 500_000,
|
||||
correlation_id::String = string(uuid4()),
|
||||
msg_purpose::String = "chat",
|
||||
sender_name::String = "NATSBridge",
|
||||
@@ -203,10 +235,10 @@ env, env_json_str = NATSBridge.smartsend(
|
||||
# Returns: ::Tuple{msg_envelope_v1, String}
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
#### JavaScript (Node.js)
|
||||
|
||||
```javascript
|
||||
const NATSBridge = require('natsbridge');
|
||||
import NATSBridge from './src/natsbridge_ssr.js';
|
||||
|
||||
const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
subject,
|
||||
@@ -215,7 +247,36 @@ const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
broker_url: 'nats://localhost:4222',
|
||||
fileserver_url: 'http://localhost:8080',
|
||||
fileserver_upload_handler: NATSBridge.plikOneshotUpload,
|
||||
size_threshold: 1_000_000,
|
||||
size_threshold: 500_000,
|
||||
correlation_id: uuidv4(),
|
||||
msg_purpose: 'chat',
|
||||
sender_name: 'NATSBridge',
|
||||
receiver_name: '',
|
||||
receiver_id: '',
|
||||
reply_to: '',
|
||||
reply_to_msg_id: '',
|
||||
is_publish: true,
|
||||
nats_connection: null,
|
||||
msg_id: uuidv4(),
|
||||
sender_id: uuidv4()
|
||||
}
|
||||
);
|
||||
// Returns: Promise<[env, env_json_str]>
|
||||
```
|
||||
|
||||
#### JavaScript (Browser)
|
||||
|
||||
```javascript
|
||||
import NATSBridge from './src/natsbridge_csr.js';
|
||||
|
||||
const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
subject,
|
||||
data,
|
||||
{
|
||||
broker_url: 'ws://localhost:4222',
|
||||
fileserver_url: 'http://localhost:8080',
|
||||
fileserver_upload_handler: NATSBridge.plikOneshotUpload,
|
||||
size_threshold: 500_000,
|
||||
correlation_id: uuidv4(),
|
||||
msg_purpose: 'chat',
|
||||
sender_name: 'NATSBridge',
|
||||
@@ -243,7 +304,7 @@ env, env_json_str = await NATSBridge.smartsend(
|
||||
broker_url: str = "nats://localhost:4222",
|
||||
fileserver_url: str = "http://localhost:8080",
|
||||
fileserver_upload_handler: Callable = plik_oneshot_upload,
|
||||
size_threshold: int = 1_000_000,
|
||||
size_threshold: int = 500_000,
|
||||
correlation_id: str = None,
|
||||
msg_purpose: str = "chat",
|
||||
sender_name: str = "NATSBridge",
|
||||
@@ -293,9 +354,28 @@ env = NATSBridge.smartreceive(
|
||||
# Returns: ::JSON.Object{String, Any}
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
#### JavaScript (Node.js)
|
||||
|
||||
```javascript
|
||||
import NATSBridge from './src/natsbridge_ssr.js';
|
||||
|
||||
const env = await NATSBridge.smartreceive(
|
||||
msg,
|
||||
{
|
||||
fileserver_download_handler: NATSBridge.fetchWithBackoff,
|
||||
max_retries: 5,
|
||||
base_delay: 100,
|
||||
max_delay: 5000
|
||||
}
|
||||
);
|
||||
// Returns: Promise<env_object>
|
||||
```
|
||||
|
||||
#### JavaScript (Browser)
|
||||
|
||||
```javascript
|
||||
import NATSBridge from './src/natsbridge_csr.js';
|
||||
|
||||
const env = await NATSBridge.smartreceive(
|
||||
msg,
|
||||
{
|
||||
@@ -311,6 +391,8 @@ const env = await NATSBridge.smartreceive(
|
||||
#### Python
|
||||
|
||||
```python
|
||||
from natsbridge import NATSBridge
|
||||
|
||||
env = await NATSBridge.smartreceive(
|
||||
msg,
|
||||
fileserver_download_handler=fetch_with_backoff,
|
||||
@@ -324,6 +406,8 @@ env = await NATSBridge.smartreceive(
|
||||
#### MicroPython
|
||||
|
||||
```python
|
||||
from natsbridge import NATSBridge
|
||||
|
||||
env = NATSBridge.smartreceive(
|
||||
msg,
|
||||
fileserver_download_handler=_sync_fileserver_download,
|
||||
@@ -342,8 +426,8 @@ env = NATSBridge.smartreceive(
|
||||
|------|-------|------------|--------|-------------|-------------|
|
||||
| `text` | `String` | `string` | `str` | `str` | Plain text strings |
|
||||
| `dictionary` | `Dict`, `NamedTuple` | `Object`, `Array` | `dict`, `list` | `dict` | JSON-serializable dictionaries |
|
||||
| `arrowtable` | `DataFrame`, `Arrow.Table` | `Array<Object>` | `pandas.DataFrame` | ❌ | Tabular data (Arrow IPC) |
|
||||
| `jsontable` | `Vector{NamedTuple}` | `Array<Object>` | `list[dict]` | ❌ | Tabular data (JSON) |
|
||||
| `arrowtable` | `DataFrame`, `Arrow.Table` | ❌ (Browser), ✅ (Node.js) | `pandas.DataFrame` | ❌ | Tabular data (Arrow IPC) |
|
||||
| `jsontable` | `DataFrame`, `Vector{NamedTuple}` | `Array<Object>` | `list[dict]` | ⚠️ | Tabular data (JSON) - **Only table type in Browser** |
|
||||
| `image` | `Vector{UInt8}` | `Uint8Array`, `Buffer` | `bytes` | `bytearray` | Image data (PNG, JPG) |
|
||||
| `audio` | `Vector{UInt8}` | `Uint8Array`, `Buffer` | `bytes` | `bytearray` | Audio data (WAV, MP3) |
|
||||
| `video` | `Vector{UInt8}` | `Uint8Array`, `Buffer` | `bytes` | `bytearray` | Video data (MP4, AVI) |
|
||||
@@ -368,13 +452,13 @@ data = [
|
||||
("large_document", large_file_data, "binary")
|
||||
]
|
||||
|
||||
env, env_json_str = NATSBridge.smartsend("/chat/room1", data; fileserver_url="http://localhost:8080")
|
||||
env, env_json_str = smartsend("/chat/room1", data; fileserver_url="http://localhost:8080")
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
#### JavaScript (Node.js)
|
||||
|
||||
```javascript
|
||||
const NATSBridge = require('natsbridge');
|
||||
import NATSBridge from './src/natsbridge_ssr.js';
|
||||
|
||||
const data = [
|
||||
["message_text", "Hello!", "text"],
|
||||
@@ -389,6 +473,24 @@ const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
);
|
||||
```
|
||||
|
||||
#### JavaScript (Browser)
|
||||
|
||||
```javascript
|
||||
import NATSBridge from './src/natsbridge_csr.js';
|
||||
|
||||
const data = [
|
||||
["message_text", "Hello!", "text"],
|
||||
["user_avatar", imageData, "image"],
|
||||
["large_document", largeFileData, "binary"]
|
||||
];
|
||||
|
||||
const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
"/chat/room1",
|
||||
data,
|
||||
{ broker_url: 'ws://localhost:4222', fileserver_url: 'http://localhost:8080' }
|
||||
);
|
||||
```
|
||||
|
||||
#### Python
|
||||
|
||||
```python
|
||||
@@ -423,13 +525,13 @@ config = Dict(
|
||||
)
|
||||
|
||||
data = [("config", config, "dictionary")]
|
||||
env, env_json_str = NATSBridge.smartsend("/device/config", data)
|
||||
env, env_json_str = smartsend("/device/config", data)
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
#### JavaScript (Node.js)
|
||||
|
||||
```javascript
|
||||
const NATSBridge = require('natsbridge');
|
||||
import NATSBridge from './src/natsbridge_ssr.js';
|
||||
|
||||
const config = {
|
||||
wifi_ssid: "MyNetwork",
|
||||
@@ -475,13 +577,13 @@ df = DataFrame(
|
||||
)
|
||||
|
||||
data = [("students", df, "arrowtable")]
|
||||
env, env_json_str = NATSBridge.smartsend("/data/analysis", data)
|
||||
env, env_json_str = smartsend("/data/analysis", data)
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
#### JavaScript (Node.js)
|
||||
|
||||
```javascript
|
||||
const NATSBridge = require('natsbridge');
|
||||
import NATSBridge from './src/natsbridge_ssr.js';
|
||||
|
||||
const df = [
|
||||
{ id: 1, name: "Alice", score: 95 },
|
||||
@@ -511,6 +613,26 @@ data = [("students", df, "arrowtable")]
|
||||
env, env_json_str = await NATSBridge.smartsend("/data/analysis", data)
|
||||
```
|
||||
|
||||
#### JavaScript (Browser)
|
||||
|
||||
```javascript
|
||||
import NATSBridge from './src/natsbridge_csr.js';
|
||||
|
||||
// Browser uses jsontable (JSON array of objects) instead of arrowtable
|
||||
// Apache Arrow is not compatible with browsers
|
||||
const df = [
|
||||
{ id: 1, name: "Alice", score: 95 },
|
||||
{ id: 2, name: "Bob", score: 88 },
|
||||
{ id: 3, name: "Charlie", score: 92 }
|
||||
];
|
||||
|
||||
const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
"/data/analysis",
|
||||
[["students", df, "jsontable"]], // Use jsontable for browser
|
||||
{ broker_url: 'ws://localhost:4222' }
|
||||
);
|
||||
```
|
||||
|
||||
### Example 4: Request-Response Pattern
|
||||
|
||||
Bi-directional communication with reply-to support.
|
||||
@@ -521,18 +643,29 @@ Bi-directional communication with reply-to support.
|
||||
using NATSBridge
|
||||
|
||||
# Requester
|
||||
env, env_json_str = NATSBridge.smartsend(
|
||||
env, env_json_str = smartsend(
|
||||
"/device/command",
|
||||
[("command", Dict("action" => "read_sensor"), "dictionary")];
|
||||
broker_url="nats://localhost:4222",
|
||||
reply_to="/device/response"
|
||||
)
|
||||
|
||||
# Receiver (in separate application)
|
||||
msg = NATS.subscription.next()
|
||||
env = smartreceive(msg)
|
||||
# Process request and send response
|
||||
response_env, response_json = smartsend(
|
||||
"/device/response",
|
||||
[("result", Dict("value" => 42), "dictionary")],
|
||||
reply_to="/device/command",
|
||||
reply_to_msg_id=env["msg_id"]
|
||||
)
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
#### JavaScript (Node.js)
|
||||
|
||||
```javascript
|
||||
const NATSBridge = require('natsbridge');
|
||||
import NATSBridge from './src/natsbridge_ssr.js';
|
||||
|
||||
// Requester
|
||||
const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
@@ -540,6 +673,16 @@ const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
[["command", { action: "read_sensor" }, "dictionary"]],
|
||||
{ broker_url: 'nats://localhost:4222', reply_to: '/device/response' }
|
||||
);
|
||||
|
||||
// Receiver (in separate application)
|
||||
// const msg = await natsConsumer.next();
|
||||
// const env = await NATSBridge.smartreceive(msg);
|
||||
// Process request and send response
|
||||
// const response_env, response_json = await NATSBridge.smartsend(
|
||||
// "/device/response",
|
||||
// [["result", { value: 42 }, "dictionary"]],
|
||||
// { reply_to: '/device/command', reply_to_msg_id: env.msg_id }
|
||||
// );
|
||||
```
|
||||
|
||||
#### Python
|
||||
@@ -554,6 +697,17 @@ env, env_json_str = await NATSBridge.smartsend(
|
||||
broker_url="nats://localhost:4222",
|
||||
reply_to="/device/response"
|
||||
)
|
||||
|
||||
# Receiver (in separate application)
|
||||
# msg = await nats_consumer.next()
|
||||
# env = await NATSBridge.smartreceive(msg)
|
||||
# Process request and send response
|
||||
# response_env, response_json = await NATSBridge.smartsend(
|
||||
# "/device/response",
|
||||
# [("result", {"value": 42}, "dictionary")],
|
||||
# reply_to="/device/command",
|
||||
# reply_to_msg_id=env["msg_id"]
|
||||
# )
|
||||
```
|
||||
|
||||
---
|
||||
@@ -636,14 +790,131 @@ python3 test/test_py_table_receiver.py
|
||||
|
||||
---
|
||||
|
||||
## Browser Deployment
|
||||
|
||||
### Using with Node.js Build Tools
|
||||
|
||||
The browser implementation (`src/natsbridge_csr.js`) can be bundled for production deployment using modern JavaScript build tools.
|
||||
|
||||
#### Prerequisites
|
||||
|
||||
```bash
|
||||
# Install the browser-compatible NATS client
|
||||
npm install nats.ws
|
||||
```
|
||||
|
||||
#### Vite (Recommended)
|
||||
|
||||
```bash
|
||||
npm create vite@latest my-app -- --template vanilla
|
||||
cd my-app
|
||||
npm install nats.ws
|
||||
```
|
||||
|
||||
In `vite.config.js`:
|
||||
```javascript
|
||||
import { defineConfig } from 'vite';
|
||||
export default defineConfig({
|
||||
resolve: {
|
||||
alias: {
|
||||
'nats.ws': 'nats.ws/dist/esm/browser.js'
|
||||
}
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
Build command:
|
||||
```bash
|
||||
npm run build # Outputs to dist/ folder
|
||||
```
|
||||
|
||||
#### Webpack
|
||||
|
||||
```bash
|
||||
npm install webpack webpack-cli --save-dev
|
||||
npm install nats.ws
|
||||
```
|
||||
|
||||
In `webpack.config.js`:
|
||||
```javascript
|
||||
module.exports = {
|
||||
entry: './src/index.js',
|
||||
output: {
|
||||
filename: 'bundle.js',
|
||||
path: __dirname + '/dist'
|
||||
},
|
||||
resolve: {
|
||||
alias: {
|
||||
'nats.ws': 'nats.ws/dist/esm/browser.js'
|
||||
}
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
Build command:
|
||||
```bash
|
||||
npx webpack
|
||||
```
|
||||
|
||||
#### esbuild (Simple & Fast)
|
||||
|
||||
```bash
|
||||
npm install esbuild nats.ws --save-dev
|
||||
```
|
||||
|
||||
Create `build.js`:
|
||||
```javascript
|
||||
import esbuild from 'esbuild';
|
||||
|
||||
esbuild.buildSync({
|
||||
entryPoints: ['src/natsbridge_csr.js'],
|
||||
bundle: true,
|
||||
outfile: 'dist/natsbridge-csr-bundle.js',
|
||||
format: 'esm',
|
||||
platform: 'browser',
|
||||
target: 'es2020'
|
||||
});
|
||||
```
|
||||
|
||||
Build command:
|
||||
```bash
|
||||
node build.js
|
||||
```
|
||||
|
||||
### Using in Your HTML
|
||||
|
||||
```html
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>My App</title>
|
||||
</head>
|
||||
<body>
|
||||
<script type="module" src="dist/natsbridge-csr-bundle.js"></script>
|
||||
<script type="module">
|
||||
import NATSBridgeCSR from './dist/natsbridge-csr-bundle.js';
|
||||
|
||||
// Use the library
|
||||
const [env, envJson] = await NATSBridgeCSR.smartsend(
|
||||
"/chat/user/v1/message",
|
||||
[["msg", "Hello", "text"]],
|
||||
{ broker_url: "wss://nats.example.com" }
|
||||
);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Documentation
|
||||
|
||||
For detailed architecture and implementation information, see:
|
||||
|
||||
- [Architecture Documentation](docs/architecture_updated.md) - Cross-platform architecture, API parity, platform-specific patterns
|
||||
- [Implementation Guide](docs/implementation_updated.md) - Detailed implementation for each platform, handler functions, testing
|
||||
- [Tutorial](docs/tutorial_updated.md) - Step-by-step getting started guide
|
||||
- [Walkthrough](docs/walkthrough_updated.md) - Real-world application building guides
|
||||
- [`docs/architecture.md`](docs/architecture.md) - Cross-platform architecture, API parity, platform-specific patterns
|
||||
- [`docs/requirements.md`](docs/requirements.md) - Business requirements and user stories
|
||||
- [`docs/spec.md`](docs/spec.md) - Technical specification and contracts
|
||||
- [`docs/walkthrough.md`](docs/walkthrough.md) - Real-world application building guides
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -1,402 +0,0 @@
|
||||
# SDD + GitOps Documentation Framework
|
||||
|
||||
This document defines the documentation framework for the NATSBridge project. It establishes a structured approach to creating, maintaining, and evolving technical documentation in alignment with GitOps principles—ensuring that documentation is versioned, auditable, and continuously validated alongside the codebase.
|
||||
|
||||
---
|
||||
|
||||
## The SDD Framework: Seven Pillars of Documentation
|
||||
|
||||
| Document | Purpose (Rationale) | Primary Audience | Format / Content | Example (SaaS Context) | Measurement (KPI) |
|
||||
|----------|---------------------|-----------------|------------------|------------------------|-------------------|
|
||||
| **Requirements** | Capture the **business intent** — why we're building this and what success looks like. Defines boundaries and user-visible outcomes. | Stakeholders, Product Owners, Lead Developers | User stories, PRDs, acceptance criteria, non-functional constraints. | "System must process tabular data from Julia to SvelteKit UI with <200ms latency for 5-member teams." | 95% of requests complete <200ms (synthetic monitoring). |
|
||||
| **Specification** | The **technical contract** — precise rules for inputs, outputs, and data shape. Ensures consistency across dev and test. | Developers, QA Engineers, CI/CD pipelines | OpenAPI, Protobuf, AsyncAPI. Endpoint definitions, schemas, error codes. | `contract.yaml` defining a NATS subject that accepts Arrow streams with snake_case headers. | 100% of messages validated against spec (CI block rate). |
|
||||
| **Architecture** | The **blueprint** — how components fit together, interact, and scale. Guides system structure and trade-offs. | Architects, Senior Developers, DevOps | C4 diagrams, Mermaid.js, component/network/storage models. | Diagram showing 6-node cluster routing traffic via Caddy → Node.js API → Julia pods. | 100% of major decisions logged with trade-off analysis. |
|
||||
| **Walkthrough** | The **story of flow** — shows how pieces connect end-to-end and why steps are sequenced. Builds intuition for new devs. | New Developers, Team Members | TOUR.md, Loom videos, sequence diagrams. Step-by-step traces with rationale. | "UI sends JSON → Node.js wraps Claim-Check → Julia pulls Arrow data (prevents NATS overflow)." | New developers ship feature in <2 days (PR timeline). |
|
||||
| **Implementation** | The **real code** — business logic, helpers, tests, configs. Where design becomes executable. | Developers, Code Reviewers | Source code, README.md, unit tests, setup scripts. | Julia function for matrix calculation + SvelteKit component rendering table. | >80% unit test coverage, <5% drift from spec. |
|
||||
| **Validation** | The **enforcer** — ensures implementation matches the spec. Blocks drift and human error. | Automation servers, QA, Lead Developers | CI jobs, contract tests, linting, integration checks. | CI job rejects PR with camelCase field not allowed by YAML spec. | <1% of PRs bypass validation gates. |
|
||||
| **Runbook** | The **operational manual** — how the system lives in production, scales, and recovers. Guides on-call engineers. | DevOps, SREs, On-call Developers | K8s manifests, Helm charts, Markdown guides. Deployment, scaling, backup/restore, troubleshooting. | GitOps manifest ensuring 6 Julia replicas restart if memory >80%. | MTTR <15 minutes for P1 incidents. |
|
||||
|
||||
---
|
||||
|
||||
## Detailed Document Descriptions
|
||||
|
||||
### 1. Requirements
|
||||
|
||||
**Purpose**: Capture the *business intent* — why we're building this and what success looks like. Defines boundaries and user-visible outcomes.
|
||||
|
||||
**Why It Matters**:
|
||||
- Aligns engineering efforts with business goals
|
||||
- Provides a north star for feature development
|
||||
- Establishes acceptance criteria before implementation begins
|
||||
- Creates a contract between product and engineering
|
||||
|
||||
**Content Guidelines**:
|
||||
- User stories with clear acceptance criteria (As a X, I want Y so that Z)
|
||||
- Product Requirements Documents (PRDs) with success metrics
|
||||
- Non-functional requirements (performance, security, scalability)
|
||||
- Boundary definitions (what's in scope vs. out of scope)
|
||||
|
||||
**Best Practices**:
|
||||
- Link each requirement to a measurable KPI
|
||||
- Keep requirements testable and verifiable
|
||||
- Maintain backward compatibility with existing requirements
|
||||
- Review and update requirements as business context changes
|
||||
|
||||
---
|
||||
|
||||
### 2. Specification
|
||||
|
||||
**Purpose**: The *technical contract* — precise rules for inputs, outputs, and data shape. Ensures consistency across dev and test.
|
||||
|
||||
**Why It Matters**:
|
||||
- Prevents implementation drift between components
|
||||
- Enables contract testing in CI/CD pipelines
|
||||
- Provides a single source of truth for data structures
|
||||
- Facilitates integration between teams
|
||||
|
||||
**Content Guidelines**:
|
||||
- API endpoint definitions (methods, paths, parameters)
|
||||
- Request/response schemas (JSON, XML, Protobuf, AsyncAPI)
|
||||
- Error codes and their meanings
|
||||
- Data validation rules and constraints
|
||||
- Rate limiting and quota definitions
|
||||
|
||||
**Best Practices**:
|
||||
- Use formal specification languages (OpenAPI 3.0+, AsyncAPI)
|
||||
- Version specifications alongside code
|
||||
- Generate client SDKs from specifications
|
||||
- Block CI on specification violations
|
||||
- Document edge cases and error scenarios
|
||||
|
||||
---
|
||||
|
||||
### 3. Architecture
|
||||
|
||||
**Purpose**: The *blueprint* — how components fit together, interact, and scale. Guides system structure and trade-offs.
|
||||
|
||||
**Why It Matters**:
|
||||
- Provides a mental model for system design
|
||||
- Guides technical decision-making and trade-off analysis
|
||||
- Facilitates onboarding of new architects and senior developers
|
||||
- Documents scaling and performance considerations
|
||||
|
||||
**Content Guidelines**:
|
||||
- C4 diagrams (Context, Container, Component levels)
|
||||
- Mermaid.js flowcharts for sequence diagrams
|
||||
- Component interaction diagrams
|
||||
- Network topology and data flow
|
||||
- Storage and caching strategies
|
||||
- Scaling and resilience patterns
|
||||
|
||||
**Best Practices**:
|
||||
- Use diagrams that are easy to update (Mermaid.js over static images)
|
||||
- Document trade-off decisions with Rationale Documents
|
||||
- Include scaling considerations for each component
|
||||
- Document failure modes and recovery strategies
|
||||
- Keep architecture diagrams versioned with code
|
||||
|
||||
---
|
||||
|
||||
### 4. Walkthrough
|
||||
|
||||
**Purpose**: The *story of flow* — shows how pieces connect end-to-end and why steps are sequenced. Builds intuition for new devs.
|
||||
|
||||
**Why It Matters**:
|
||||
- Reduces onboarding time for new developers
|
||||
- Provides context that code comments alone cannot convey
|
||||
- Explains the "why" behind architectural decisions
|
||||
- Helps identify gaps in the system design
|
||||
|
||||
**Content Guidelines**:
|
||||
- Step-by-step flow descriptions with rationale
|
||||
- Sequence diagrams showing request/response patterns
|
||||
- "Tour of the codebase" guides
|
||||
- Video walkthroughs (Loom, internal recordings)
|
||||
- Debugging and tracing examples
|
||||
|
||||
**Best Practices**:
|
||||
- Walk through real user journeys, not just technical flows
|
||||
- Include "what could go wrong" scenarios
|
||||
- Link walkthroughs to relevant code locations
|
||||
- Keep walkthroughs updated with architecture changes
|
||||
- Make walkthroughs interactive where possible
|
||||
|
||||
---
|
||||
|
||||
### 5. Implementation
|
||||
|
||||
**Purpose**: The *real code* — business logic, helpers, tests, configs. Where design becomes executable.
|
||||
|
||||
**Why It Matters**:
|
||||
- This is the actual artifact that runs in production
|
||||
- Code is the ultimate source of truth (when it matches spec)
|
||||
- Tests validate correctness and prevent regressions
|
||||
- Configuration files define runtime behavior
|
||||
|
||||
**Content Guidelines**:
|
||||
- Business logic implementation
|
||||
- Helper functions and utilities
|
||||
- Unit and integration tests
|
||||
- Configuration files (YAML, JSON, environment)
|
||||
- Setup and development scripts
|
||||
- Code organization and module structure
|
||||
|
||||
**Best Practices**:
|
||||
- Follow consistent code style and conventions
|
||||
- Write tests before or alongside implementation (TDD/BDD)
|
||||
- Document complex logic with inline comments
|
||||
- Keep configuration externalized and versioned
|
||||
- Use type annotations where applicable
|
||||
|
||||
---
|
||||
|
||||
### 6. Validation
|
||||
|
||||
**Purpose**: The *enforcer* — ensures implementation matches the spec. Blocks drift and human error.
|
||||
|
||||
**Why It Matters**:
|
||||
- Prevents breaking changes from reaching production
|
||||
- Catches specification violations early in the CI pipeline
|
||||
- Maintains data integrity and API consistency
|
||||
- Reduces manual QA effort through automation
|
||||
|
||||
**Content Guidelines**:
|
||||
- CI/CD pipeline configurations
|
||||
- Contract testing scripts
|
||||
- Linting rules and configurations
|
||||
- Integration test suites
|
||||
- Schema validation jobs
|
||||
- Security scanning and audit jobs
|
||||
|
||||
**Best Practices**:
|
||||
- Fail CI on specification violations
|
||||
- Run validation jobs on every commit and PR
|
||||
- Use automated code review tools
|
||||
- Maintain validation job health dashboard
|
||||
- Document validation failure remediation steps
|
||||
|
||||
---
|
||||
|
||||
### 7. Runbook
|
||||
|
||||
**Purpose**: The *operational manual* — how the system lives in production, scales, and recovers. Guides on-call engineers.
|
||||
|
||||
**Why It Matters**:
|
||||
- Reduces Mean Time To Recovery (MTTR) for incidents
|
||||
- Provides step-by-step guidance for common issues
|
||||
- Documents scaling and deployment procedures
|
||||
- Ensures operational knowledge is not siloed
|
||||
|
||||
**Content Guidelines**:
|
||||
- Deployment procedures (manual and automated)
|
||||
- Scaling instructions (horizontal/vertical)
|
||||
- Backup and restore procedures
|
||||
- Troubleshooting guides for common issues
|
||||
- Runbook entries for specific error codes
|
||||
- Contact information and escalation paths
|
||||
|
||||
**Best Practices**:
|
||||
- Write runbooks for every P1/P2 incident
|
||||
- Include exact commands and configuration snippets
|
||||
- Test runbooks periodically (chaos engineering)
|
||||
- Link runbook entries to relevant documentation
|
||||
- Keep runbooks updated when system changes
|
||||
|
||||
---
|
||||
|
||||
## How to Use This Approach Effectively
|
||||
|
||||
### 1. Start with Requirements
|
||||
|
||||
Before writing any code or documentation, establish clear requirements. Ask:
|
||||
- What business problem are we solving?
|
||||
- How will we measure success?
|
||||
- What are the non-negotiable constraints?
|
||||
|
||||
**Action**: Create a `docs/requirements/` directory and start with `PRD.md` and `KPIs.md`.
|
||||
|
||||
### 2. Define the Specification First
|
||||
|
||||
Once requirements are stable, define the technical specification. This becomes the contract for implementation.
|
||||
|
||||
**Action**: Create `docs/specification/` with `contract.yaml` (or appropriate format) and `error-codes.md`.
|
||||
|
||||
### 3. Design the Architecture
|
||||
|
||||
With requirements and specification in place, design the architecture. Document trade-off decisions explicitly.
|
||||
|
||||
**Action**: Create `docs/architecture/` with Mermaid diagrams and `trade-offs.md`.
|
||||
|
||||
### 4. Create Walkthroughs Early
|
||||
|
||||
As soon as the architecture is defined, create walkthroughs. This helps identify gaps and provides onboarding material.
|
||||
|
||||
**Action**: Create `docs/walkthrough/` with `TOUR.md` and sequence diagrams.
|
||||
|
||||
### 5. Implement with Validation in Mind
|
||||
|
||||
Write implementation code that adheres to the specification. Build validation into the CI pipeline from day one.
|
||||
|
||||
**Action**: Ensure test files are co-located with implementation and run on every commit.
|
||||
|
||||
### 6. Automate Validation
|
||||
|
||||
Build automated validation that runs in CI/CD. This ensures spec compliance and prevents drift.
|
||||
|
||||
**Action**: Configure CI jobs to validate against specification and block PRs on violations.
|
||||
|
||||
### 7. Document Operations from Day One
|
||||
|
||||
Create runbook entries as soon as deployment procedures are established. Update them when incidents occur.
|
||||
|
||||
**Action**: Create `docs/runbook/` with entries for deployment, scaling, and common issues.
|
||||
|
||||
---
|
||||
|
||||
## GitOps Integration
|
||||
|
||||
This documentation framework aligns with GitOps principles:
|
||||
|
||||
| GitOps Principle | Documentation Alignment |
|
||||
|-----------------|------------------------|
|
||||
| **Versioned** | All documentation lives in git, with history and audit trail |
|
||||
| ** declarative** | Specifications and architecture are declarative contracts |
|
||||
| **Automated** | Validation jobs automate spec compliance checks |
|
||||
| **Self-Service** | Walkthroughs and runbooks enable self-service onboarding and operations |
|
||||
| **Observability** | KPIs and metrics are defined for each documentation artifact |
|
||||
|
||||
**Git Structure**:
|
||||
```
|
||||
docs/
|
||||
├── requirements/ # PRDs, user stories, KPIs
|
||||
├── specification/ # OpenAPI, Protobuf, AsyncAPI specs
|
||||
├── architecture/ # C4 diagrams, Mermaid, trade-off docs
|
||||
├── walkthrough/ # TOUR.md, sequence diagrams
|
||||
├── implementation/ # Source code (in src/)
|
||||
├── validation/ # CI configs, test suites
|
||||
└── runbook/ # Deployment, scaling, troubleshooting
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Metrics and Continuous Improvement
|
||||
|
||||
Each documentation artifact has associated KPIs. Track these to ensure quality:
|
||||
|
||||
| Document | KPI | Target |
|
||||
|----------|-----|--------|
|
||||
| Requirements | Requirement coverage | 100% of features have associated requirements |
|
||||
| Specification | Spec compliance rate | 100% of messages validate against spec |
|
||||
| Architecture | Decision documentation | 100% of major decisions logged with trade-offs |
|
||||
| Walkthrough | New dev time-to-first-PR | <2 days from onboarding to first contribution |
|
||||
| Implementation | Test coverage | >80% unit test coverage |
|
||||
| Validation | Bypass rate | <1% of PRs bypass validation gates |
|
||||
| Runbook | MTTR | <15 minutes for P1 incidents |
|
||||
|
||||
**Review Cadence**:
|
||||
- Weekly: Review KPI dashboards and documentation gaps
|
||||
- Monthly: Update documentation based on incident learnings
|
||||
- Quarterly: Full framework review and improvement
|
||||
|
||||
---
|
||||
|
||||
## Template Examples
|
||||
|
||||
### Requirements Template
|
||||
```markdown
|
||||
# PRD: Feature Name
|
||||
|
||||
## Business Goal
|
||||
[What problem are we solving?]
|
||||
|
||||
## Success Metrics
|
||||
- [Metric 1]: Target [value]
|
||||
- [Metric 2]: Target [value]
|
||||
|
||||
## User Stories
|
||||
- As a [role], I want [feature] so that [benefit]
|
||||
- Acceptance Criteria: [details]
|
||||
|
||||
## Non-Functional Requirements
|
||||
- Performance: [details]
|
||||
- Security: [details]
|
||||
- Scalability: [details]
|
||||
|
||||
## Out of Scope
|
||||
- [What's explicitly excluded]
|
||||
```
|
||||
|
||||
### Specification Template
|
||||
```yaml
|
||||
# contract.yaml
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: NATSBridge API
|
||||
version: 1.0.0
|
||||
paths:
|
||||
/api/v1/endpoint:
|
||||
post:
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Request'
|
||||
responses:
|
||||
'200':
|
||||
description: Success
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Response'
|
||||
```
|
||||
|
||||
### Architecture Template
|
||||
```mermaid
|
||||
%%{init: {'theme': 'base', 'themeVariables': {'primaryColor': '#3b82f6'}}}%%
|
||||
flowchart TD
|
||||
A[Client] --> B[Caddy]
|
||||
B --> C[Node.js API]
|
||||
C --> D[Julia Worker]
|
||||
D --> E[NATS Cluster]
|
||||
E --> F[Storage]
|
||||
|
||||
style A fill:#f9f9f9,stroke:#333
|
||||
style E fill:#e0e7ff,stroke:#3b82f6
|
||||
```
|
||||
|
||||
### Runbook Template
|
||||
```markdown
|
||||
# Runbook: Service Restart
|
||||
|
||||
**Severity**: P2
|
||||
**Estimated Time**: 5 minutes
|
||||
|
||||
## Symptoms
|
||||
- Service is unresponsive
|
||||
- Health checks are failing
|
||||
|
||||
## Steps
|
||||
1. SSH to the host
|
||||
2. Run: `kubectl rollout restart deployment/natsbridge`
|
||||
3. Monitor: `kubectl get pods -l app=natsbridge -w`
|
||||
|
||||
## Rollback
|
||||
- Run: `kubectl rollout undo deployment/natsbridge`
|
||||
|
||||
## Post-Incident
|
||||
- [ ] Review logs for root cause
|
||||
- [ ] Update runbook if needed
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Conclusion
|
||||
|
||||
This SDD + GitOps Documentation Framework ensures that documentation is:
|
||||
- **Structured**: Seven distinct artifacts with clear purposes
|
||||
- **Automated**: Validation and CI/CD integration
|
||||
- **Versioned**: All documentation in git with history
|
||||
- **Measurable**: KPIs for quality and effectiveness
|
||||
- **Actionable**: Practical templates and examples
|
||||
|
||||
Use this framework as a living document—update it as your team's needs evolve.
|
||||
@@ -1,16 +1,16 @@
|
||||
# Architecture Documentation: NATSBridge
|
||||
|
||||
**Version**: 1.0.0
|
||||
**Date**: 2026-03-13
|
||||
**Version**: 1.2.0
|
||||
**Date**: 2026-05-13
|
||||
**Status**: Active
|
||||
**Ground Truth**: [`src/NATSBridge.jl`](../src/NATSBridge.jl)
|
||||
**Architecture Level**: C4 Container Level
|
||||
|
||||
---
|
||||
|
||||
## Executive Summary
|
||||
## 1. Executive Summary
|
||||
|
||||
This document defines the **blueprint** for NATSBridge - the cross-platform bi-directional data bridge that enables seamless communication between **Julia**, **JavaScript**, **Python**, and **MicroPython** applications using NATS as the message bus.
|
||||
This document defines the **blueprint** for NATSBridge - the cross-platform bi-directional data bridge that enables seamless communication between **Julia**, **JavaScript**, **Python**, **Dart**, **Rust**, and **MicroPython** applications using NATS as the message bus.
|
||||
|
||||
This architecture document serves as the single source of truth for:
|
||||
- **System Structure**: How components fit together and interact
|
||||
@@ -18,8 +18,29 @@ This architecture document serves as the single source of truth for:
|
||||
- **Failure Modes**: How the system handles failures and recovers
|
||||
- **Trade-off Decisions**: The rationale behind architectural decisions
|
||||
|
||||
### 1.1 Specification Traceability
|
||||
|
||||
| Architecture Section | Specification Reference | UI Specification Reference | Requirement ID(s) |
|
||||
|---------------------|-------------------------|---------------------------|-------------------|
|
||||
| Section 2 (Context Diagram) | specification.md:2 | - | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-012, FR-013, FR-014 |
|
||||
| Section 3 (Container Diagram) | specification.md:2, specification.md:3, specification.md:11 | - | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-012, FR-013, FR-014 |
|
||||
| Section 4 (Component Diagram) | specification.md:2, specification.md:3, specification.md:5, specification.md:11 | - | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-012, FR-013, FR-014 |
|
||||
| Section 5 (High-Level) | specification.md:2, specification.md:3, specification.md:5, specification.md:11 | - | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-012, FR-013, FR-014 |
|
||||
| Section 6 (Message Envelope) | specification.md:2, specification.md:3, specification.md:8 | - | FR-011, FR-012, FR-013, FR-014, NFR-401, NFR-403 |
|
||||
| Section 7 (Payload Type) | specification.md:3, specification.md:5, specification.md:6 | - | FR-001, FR-002, FR-003, FR-006, FR-012, NFR-101, NFR-102 |
|
||||
| Section 8 (Transport Strategy) | specification.md:6, specification.md:7 | - | FR-003, FR-004, FR-005, FR-010, NFR-104, NFR-105, NFR-106 |
|
||||
| Section 9 (Platform-Specific) | specification.md:13, specification.md:14 | - | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-012, FR-013, FR-014 |
|
||||
| Section 10 (Scaling) | specification.md:7, specification.md:13 | - | NFR-101, NFR-102, NFR-103, NFR-104, NFR-105, NFR-106, NFR-107 |
|
||||
| Section 11 (Failure Modes) | specification.md:9, specification.md:11 | - | FR-008, FR-009, FR-010, FR-011, NFR-201, NFR-202, NFR-203 |
|
||||
| Section 12 (Trade-offs) | specification.md:2, specification.md:3, specification.md:6, specification.md:7 | - | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-008, FR-009, FR-010, FR-011, FR-012, FR-013, FR-014 |
|
||||
| Section 13 (Deployment) | specification.md:12, specification.md:18 | - | FR-013, FR-014, NFR-201, NFR-203 |
|
||||
| Section 14 (Security) | specification.md:4, specification.md:9, specification.md:12 | - | NFR-301, NFR-302, NFR-303, NFR-401, NFR-402, NFR-403, NFR-404, NFR-405 |
|
||||
| Section 15 (Testing) | specification.md:17 | - | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-012, FR-013, FR-014 |
|
||||
|
||||
---
|
||||
|
||||
## 2. Architecture Overview
|
||||
|
||||
## Architecture Overview
|
||||
|
||||
### C4 Context Diagram
|
||||
@@ -35,17 +56,23 @@ flowchart TD
|
||||
Julia_App[Julia Application]
|
||||
JS_App[JavaScript Application<br/>Node.js/Browser]
|
||||
Python_App[Python Application<br/>Desktop]
|
||||
Dart_App[Dart Application<br/>Desktop/Flutter/Web]
|
||||
Rust_App[Rust Application<br/>Server/Desktop]
|
||||
MicroPython_App[MicroPython Device]
|
||||
end
|
||||
|
||||
Julia_App -->|NATS| NATS_Server
|
||||
JS_App -->|NATS| NATS_Server
|
||||
Python_App -->|NATS| NATS_Server
|
||||
Dart_App -->|NATS| NATS_Server
|
||||
Rust_App -->|NATS| NATS_Server
|
||||
MicroPython_App -->|NATS| NATS_Server
|
||||
|
||||
Julia_App -->|HTTP| File_Server
|
||||
JS_App -->|HTTP| File_Server
|
||||
Python_App -->|HTTP| File_Server
|
||||
Dart_App -->|HTTP| File_Server
|
||||
Rust_App -->|HTTP| File_Server
|
||||
MicroPython_App -->|HTTP| File_Server
|
||||
|
||||
style NATS_Server fill:#fff3e0,stroke:#f57c00
|
||||
@@ -53,6 +80,8 @@ flowchart TD
|
||||
style Julia_App fill:#e8f5e9,stroke:#4caf50
|
||||
style JS_App fill:#e3f2fd,stroke:#2196f3
|
||||
style Python_App fill:#e3f2fd,stroke:#2196f3
|
||||
style Dart_App fill:#fff0f6,stroke:#e91e63
|
||||
style Rust_App fill:#dea584,stroke:#e65100
|
||||
style MicroPython_App fill:#fce4ec,stroke:#e91e63
|
||||
```
|
||||
|
||||
@@ -60,26 +89,20 @@ flowchart TD
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
subgraph "Client Container"
|
||||
subgraph "Client Container"
|
||||
Julia_Module[Julia NATSBridge Module]
|
||||
JS_Module[JavaScript NATSBridge Module]
|
||||
Python_Module[Python NATSBridge Module]
|
||||
Dart_Module[Dart NATSBridge Module]
|
||||
Rust_Module[Rust NATSBridge Module]
|
||||
MicroPython_Module[MicroPython NATSBridge Module]
|
||||
end
|
||||
|
||||
subgraph "NATS Container"
|
||||
NATS_Client[NATS Client]
|
||||
NATS_Broker[NATS Broker]
|
||||
end
|
||||
|
||||
subgraph "File Server Container"
|
||||
File_Client[HTTP Client]
|
||||
File_Server[File Server]
|
||||
end
|
||||
|
||||
Julia_Module --> NATS_Client
|
||||
JS_Module --> NATS_Client
|
||||
Python_Module --> NATS_Client
|
||||
Dart_Module --> NATS_Client
|
||||
Rust_Module --> NATS_Client
|
||||
MicroPython_Module --> NATS_Client
|
||||
|
||||
NATS_Client --> NATS_Broker
|
||||
@@ -87,6 +110,8 @@ flowchart TD
|
||||
Julia_Module --> File_Client
|
||||
JS_Module --> File_Client
|
||||
Python_Module --> File_Client
|
||||
Dart_Module --> File_Client
|
||||
Rust_Module --> File_Client
|
||||
MicroPython_Module --> File_Client
|
||||
|
||||
File_Client --> File_Server
|
||||
@@ -94,6 +119,8 @@ flowchart TD
|
||||
style Julia_Module fill:#e8f5e9,stroke:#4caf50
|
||||
style JS_Module fill:#e3f2fd,stroke:#2196f3
|
||||
style Python_Module fill:#e3f2fd,stroke:#2196f3
|
||||
style Dart_Module fill:#fff0f6,stroke:#e91e63
|
||||
style Rust_Module fill:#dea584,stroke:#e65100
|
||||
style MicroPython_Module fill:#fce4ec,stroke:#e91e63
|
||||
style NATS_Broker fill:#fff3e0,stroke:#f57c00
|
||||
style File_Server fill:#f3e5f5,stroke:#9c27b4
|
||||
@@ -110,36 +137,31 @@ flowchart TD
|
||||
Serialize[_serialize_data]
|
||||
Deserialize[_deserialize_data]
|
||||
|
||||
BuildEnvelope[build_envelope]
|
||||
BuildPayload[build_payload]
|
||||
|
||||
PublishMessage[publish_message]
|
||||
EnvelopeToJson[envelope_to_json]
|
||||
|
||||
FileServerUpload[fileserver_upload_handler]
|
||||
FileServerDownload[fileserver_download_handler]
|
||||
|
||||
LogTrace[log_trace]
|
||||
end
|
||||
|
||||
subgraph "Data Models"
|
||||
Payload[MsgPayloadV1 Struct]
|
||||
Envelope[MsgEnvelopeV1 Struct]
|
||||
Payload[msg_payload_v1 Struct]
|
||||
Envelope[msg_envelope_v1 Struct]
|
||||
end
|
||||
|
||||
SmartSend --> Serialize
|
||||
SmartSend --> BuildEnvelope
|
||||
SmartSend --> BuildPayload
|
||||
SmartSend --> PublishMessage
|
||||
SmartSend --> EnvelopeToJson
|
||||
SmartSend --> FileServerUpload
|
||||
|
||||
|
||||
SmartReceive --> Deserialize
|
||||
SmartReceive --> FileServerDownload
|
||||
|
||||
|
||||
EnvelopeToJson --> Envelope
|
||||
Serialize --> Payload
|
||||
BuildEnvelope --> Envelope
|
||||
BuildPayload --> Payload
|
||||
|
||||
style SmartSend fill:#d1fae5,stroke:#10b981
|
||||
style SmartReceive fill:#d1fae5,stroke:#10b981
|
||||
style PublishMessage fill:#fef3c7,stroke:#f59e0b
|
||||
style FileServerUpload fill:#fef3c7,stroke:#f59e0b
|
||||
style FileServerDownload fill:#fef3c7,stroke:#f59e0b
|
||||
```
|
||||
@@ -152,15 +174,14 @@ flowchart TD
|
||||
|
||||
| Component | Purpose | Platform Support |
|
||||
|-----------|---------|------------------|
|
||||
| **smartsend** | Send data via NATS with automatic transport selection | All |
|
||||
| **smartreceive** | Receive and process NATS messages | All |
|
||||
| **smartsend** | Send data via NATS with automatic transport selection, returns (envelope, json_string) for caller to publish | All |
|
||||
| **smartreceive** | Receive and process NATS messages from JSON string | All |
|
||||
| **_serialize_data** | Serialize data according to payload type | All |
|
||||
| **_deserialize_data** | Deserialize bytes to native data types | All |
|
||||
| **_build_envelope** | Build message envelope from payloads | All |
|
||||
| **_build_payload** | Build payload object from serialized data | All |
|
||||
| **publish_message** | Publish message to NATS subject | All |
|
||||
| **fileserver_upload_handler** | Upload large payloads to HTTP server | Desktop |
|
||||
| **fileserver_download_handler** | Download payloads from HTTP server | Desktop |
|
||||
| **envelope_to_json** | Convert msg_envelope_v1 struct to JSON string | All |
|
||||
| **log_trace** | Log trace messages with correlation ID | All |
|
||||
| **fileserver_upload_handler** | Upload large payloads to HTTP server | Desktop (Julia/JS/Python/Dart/Rust) |
|
||||
| **fileserver_download_handler** | Download payloads from HTTP server with exponential backoff | Desktop (Julia/JS/Python/Dart/Rust) |
|
||||
|
||||
### Data Flow
|
||||
|
||||
@@ -182,7 +203,7 @@ flowchart TD
|
||||
|
||||
H --> L[Build envelope]
|
||||
L --> M[Convert to JSON]
|
||||
M --> N[Publish to NATS]
|
||||
M --> N[Return envelope + JSON to caller]
|
||||
|
||||
style A fill:#f9f9f9,stroke:#333
|
||||
style N fill:#e0e7ff,stroke:#3b82f6
|
||||
@@ -275,8 +296,8 @@ end
|
||||
|------|-------------|---------------|----------|-----------|
|
||||
| `text` | Plain text string | UTF-8 bytes | Base64 | All |
|
||||
| `dictionary` | JSON object | JSON string | Base64/JSON | All |
|
||||
| `arrowtable` | Apache Arrow IPC | Arrow IPC stream | Base64/arrow-ipc | Desktop |
|
||||
| `jsontable` | JSON array of objects | JSON string | Base64/json | All |
|
||||
| `arrowtable` | Apache Arrow IPC | Arrow IPC stream | Base64/arrow-ipc | Desktop (Julia/Python/Node.js/Dart/Rust) |
|
||||
| `jsontable` | JSON array of objects | JSON string | Base64/json | All (including Browser/Dart Web) |
|
||||
| `image` | Binary image data | Raw bytes | Base64 | All |
|
||||
| `audio` | Binary audio data | Raw bytes | Base64 | All |
|
||||
| `video` | Binary video data | Raw bytes | Base64 | All |
|
||||
@@ -318,7 +339,10 @@ flowchart TD
|
||||
|
||||
| Platform | Size Threshold | Notes |
|
||||
|----------|----------------|-------|
|
||||
| Desktop (Julia/JS/Python) | 500,000 bytes (0.5MB) | Default threshold |
|
||||
| Desktop (Julia/JS/Python/Dart) | 500,000 bytes (0.5MB) | Default threshold |
|
||||
| Dart Desktop | 500,000 bytes (0.5MB) | Default threshold |
|
||||
| Dart Flutter | 500,000 bytes (0.5MB) | Default threshold |
|
||||
| Dart Web | 500,000 bytes (0.5MB) | Default threshold |
|
||||
| MicroPython | 100,000 bytes (100KB) | Lower threshold for memory constraints |
|
||||
|
||||
### Transport Selection Flow
|
||||
@@ -405,24 +429,22 @@ end
|
||||
|
||||
JavaScript uses async/await for non-blocking I/O:
|
||||
|
||||
- **Class-based NATS Client**: Connection management
|
||||
- **Module-level Utilities**: Serialization functions
|
||||
- **Native ArrayBuffer**: Binary data handling
|
||||
- **Native ArrayBuffer**: Binary data handling (Browser) / Buffer (Node.js)
|
||||
- **Fetch API**: HTTP file server communication
|
||||
|
||||
```javascript
|
||||
// Class-based NATS client
|
||||
class NATSClient {
|
||||
constructor(url) {
|
||||
this.url = url;
|
||||
this.connection = null;
|
||||
}
|
||||
|
||||
async connect() {
|
||||
this.connection = await nats.connect({ servers: this.url });
|
||||
}
|
||||
}
|
||||
```
|
||||
#### Node.js Implementation (natsbridge_ssr.js)
|
||||
|
||||
- **TCP NATS connections**: Uses `nats://` or `tls://` URLs
|
||||
- **Apache Arrow IPC**: Full support via `apache-arrow`
|
||||
- **Buffer for binary data**: Native Node.js Buffer handling
|
||||
|
||||
#### Browser Implementation (natsbridge_csr.js)
|
||||
|
||||
- **WebSocket NATS connections**: Uses `ws://` or `wss://` URLs via `nats.ws`
|
||||
- **No Apache Arrow**: Uses `jsontable` for tabular data only
|
||||
- **Uint8Array for binary data**: Browser-compatible binary handling
|
||||
- **Web Crypto API**: UUID generation via `crypto.getRandomValues()`
|
||||
|
||||
### Python Architecture
|
||||
|
||||
@@ -442,6 +464,60 @@ class NATSBridge:
|
||||
self.fileserver_url = fileserver_url or self.DEFAULT_FILESERVER_URL
|
||||
```
|
||||
|
||||
### Dart Architecture
|
||||
|
||||
Dart uses classes for stateful operations with async/await:
|
||||
|
||||
- **Class-based NATSBridge**: Encapsulated API
|
||||
- **Data classes**: Structured data (MsgPayloadV1, MsgEnvelopeV1)
|
||||
- **Async/await**: I/O operations
|
||||
- **dart-arrow**: Arrow IPC support (Desktop/Flutter only)
|
||||
- **HTTP package**: HTTP file server communication
|
||||
- **nats package**: NATS client with WebSocket support (Dart Web)
|
||||
|
||||
```dart
|
||||
class NATSBridge {
|
||||
static const DEFAULT_SIZE_THRESHOLD = 500000;
|
||||
|
||||
final String brokerUrl;
|
||||
final String fileserverUrl;
|
||||
|
||||
NATSBridge({
|
||||
this.brokerUrl = 'nats://localhost:4222',
|
||||
this.fileserverUrl = 'http://localhost:8080',
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
#### Dart Desktop (Dart SDK)
|
||||
|
||||
- **TCP NATS connections**: Uses `nats://` or `tls://` URLs
|
||||
- **Apache Arrow IPC**: Full support via `dart-arrow`
|
||||
- **Uint8List for binary data**: Native Dart binary handling
|
||||
|
||||
#### Dart Flutter (Dart SDK)
|
||||
|
||||
- **TCP NATS connections**: Uses `nats://` or `tls://` URLs
|
||||
- **Apache Arrow IPC**: Full support via `dart-arrow`
|
||||
- **Uint8List for binary data**: Native Dart binary handling
|
||||
|
||||
#### Dart Web (Dart SDK)
|
||||
|
||||
- **WebSocket NATS connections**: Uses `ws://` or `wss://` URLs via `nats` package
|
||||
- **No Apache Arrow**: Uses `jsontable` for tabular data only
|
||||
- **Uint8List for binary data**: Browser-compatible binary handling
|
||||
- **Fetch API**: HTTP file server communication via `http` package
|
||||
|
||||
### Browser Architecture
|
||||
|
||||
Browser JavaScript has specific constraints due to security and compatibility:
|
||||
|
||||
- **Async/await**: Native async/await support
|
||||
- **No Apache Arrow**: Arrow IPC not available in browsers
|
||||
- **JSON table only**: Use "jsontable" for tabular data
|
||||
- **WebSocket NATS**: Uses nats.ws for browser-compatible NATS connections
|
||||
- **Fetch API**: HTTP file server communication via fetch
|
||||
|
||||
### MicroPython Architecture
|
||||
|
||||
MicroPython has significant constraints:
|
||||
@@ -457,6 +533,62 @@ DEFAULT_SIZE_THRESHOLD = 100_000 # 100KB
|
||||
MAX_PAYLOAD_SIZE = 50_000 # 50KB hard limit
|
||||
```
|
||||
|
||||
### Rust Architecture
|
||||
|
||||
Rust leverages compile-time type safety and async runtimes:
|
||||
|
||||
- **Type-safe payloads**: Rust enum discriminates between `Text`, `Dictionary`, `ArrowTable`, `Binary`, etc.
|
||||
- **serde serialization**: Automatic JSON deserialization via `#[derive(Serialize, Deserialize)]`
|
||||
- **tokio runtime**: Efficient async I/O for NATS connections and HTTP file server operations
|
||||
- **arrow2 integration**: Native Arrow IPC deserialization without intermediate format conversion
|
||||
- **reqwest**: High-performance HTTP client with built-in TLS and connection pooling
|
||||
- **Zero-copy patterns**: `Vec<u8>` passed directly to avoid unnecessary memory copies
|
||||
- **Result<T, E>**: Idiomatic error handling with typed error types
|
||||
|
||||
```rust
|
||||
// Type-safe payload enum (compile-time discrimination)
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub enum Payload {
|
||||
Text(String),
|
||||
Dictionary(serde_json::Value),
|
||||
ArrowTable(Vec<u8>),
|
||||
JsonTable(serde_json::Value),
|
||||
Image(Vec<u8>),
|
||||
Audio(Vec<u8>),
|
||||
Video(Vec<u8>),
|
||||
Binary(Vec<u8>),
|
||||
}
|
||||
|
||||
// Configuration via builder pattern
|
||||
pub struct SmartsendOptions {
|
||||
pub broker_url: String,
|
||||
pub fileserver_url: String,
|
||||
pub fileserver_upload_handler: Option<UploadHandler>,
|
||||
pub size_threshold: usize,
|
||||
pub correlation_id: String,
|
||||
pub msg_purpose: String,
|
||||
pub sender_name: String,
|
||||
// ... other fields
|
||||
}
|
||||
|
||||
// NATS client with tokio integration
|
||||
let conn = nats::connect("nats://localhost:4222").await?;
|
||||
|
||||
// Subscribe and process messages
|
||||
let mut sub = conn.subscribe("/agent/wine/api/v1/analyze")?;
|
||||
for msg in sub.messages() {
|
||||
let envelope: MsgEnvelopeV1 = serde_json::from_slice(&msg.payload)?;
|
||||
// Type-safe access to payloads
|
||||
for payload in &envelope.payloads {
|
||||
match &payload.data {
|
||||
Payload::ArrowTable(bytes) => { /* process */ },
|
||||
Payload::Text(text) => { /* process */ },
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Scaling Architecture
|
||||
@@ -598,7 +730,7 @@ MAX_PAYLOAD_SIZE = 50_000 # 50KB hard limit
|
||||
|-----------|---------|-------|
|
||||
| NATS Server | 1 instance | Single node for development |
|
||||
| File Server | 1 instance | HTTP server for large payloads |
|
||||
| Client Memory | 50MB | Desktop platforms |
|
||||
| Client Memory | 50MB | Desktop platforms (Julia/JS/Python/Dart) |
|
||||
| Client Memory | 256KB | MicroPython devices |
|
||||
|
||||
### Environment Variables
|
||||
@@ -607,7 +739,7 @@ MAX_PAYLOAD_SIZE = 50_000 # 50KB hard limit
|
||||
|----------|---------|-------------|
|
||||
| `NATS_URL` | `nats://localhost:4222` | NATS server URL |
|
||||
| `FILESERVER_URL` | `http://localhost:8080` | HTTP file server URL |
|
||||
| `SIZE_THRESHOLD` | `1000000` | Size threshold in bytes |
|
||||
| `SIZE_THRESHOLD` | `500000` | Size threshold in bytes (0.5MB) |
|
||||
|
||||
### Container Deployment
|
||||
|
||||
@@ -693,7 +825,7 @@ flowchart TD
|
||||
|
||||
| Version | Supported Platforms |
|
||||
|---------|---------------------|
|
||||
| v1.0.x | Julia 1.7+, Node.js 16+, Python 3.8+, MicroPython 1.19+ |
|
||||
| v1.0.x | Julia 1.7+, Node.js 16+, Python 3.8+, Dart 2.17+, Rust 1.70+, MicroPython 1.19+ |
|
||||
|
||||
---
|
||||
|
||||
@@ -701,16 +833,103 @@ flowchart TD
|
||||
|
||||
| Date | Version | Changes |
|
||||
|------|---------|---------|
|
||||
| 2026-05-13 | 1.3.0 | Added Rust support with tokio, serde, and arrow2 | All sections |
|
||||
| - | - | Added Rust to C4 diagrams (context, container) | All sections |
|
||||
| - | - | Added Rust platform-specific architecture section | specification.md:13 |
|
||||
| - | - | Updated component table with Rust support | All sections |
|
||||
| 2026-05-13 | 1.2.0 | Aligned with ground truth implementation (src/NATSBridge.jl) |
|
||||
| - | - | Removed publish_message component (commented out in source) |
|
||||
| - | - | Removed NATSClient and NATSConnectionPool classes (not in ground truth) |
|
||||
| - | - | Updated component diagram to match actual module structure |
|
||||
| - | - | Updated data flow to show smartsend returns JSON for caller to publish |
|
||||
| - | - | Fixed SIZE_THRESHOLD default to 500,000 bytes |
|
||||
| 2026-03-15 | 1.1.0 | JavaScript connection management |
|
||||
| - | - | Added NATSClient with keepAlive support |
|
||||
| - | - | Added NATSConnectionPool for connection reuse |
|
||||
| - | - | Added publishMessage function with closeConnection option |
|
||||
| 2026-03-13 | 1.0.0 | Initial architecture documentation |
|
||||
|
||||
---
|
||||
|
||||
## References
|
||||
## 16. References
|
||||
|
||||
- [`docs/requirements.md`](./requirements.md) - Business requirements and user stories
|
||||
- [`docs/spec.md`](./spec.md) - Technical specification and contracts
|
||||
- [`src/NATSBridge.jl`](../src/NATSBridge.jl) - Ground truth implementation
|
||||
- [`README.md`](../README.md) - Project overview
|
||||
### 16.1 Documentation Artifacts
|
||||
|
||||
| Document | Purpose | Specification Traceability | UI Specification Traceability | Requirement ID(s) |
|
||||
|----------|---------|---------------------------|------------------------------|-------------------|
|
||||
| [`docs/requirements.md`](./requirements.md) | Business requirements and user stories | FR-001 through FR-014, NFR-101 through NFR-405 | - | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`docs/specification.md`](./specification.md) | Technical contract for NATSBridge | specification.md:2-19 (all sections) | - | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`docs/ui-specification.md`](./ui-specification.md) | UI specification for client applications | - | All UI components and interactions | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`docs/walkthrough.md`](./walkthrough.md) | End-to-end system flow | specification.md:2-19 (all sections) | - | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`docs/architecture.md`](./architecture.md) | System architecture diagrams | specification.md:2-19 (all sections) | - | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`docs/validation.md`](./validation.md) | CI/CD validation rules | specification.md:2-19 (all sections) | - | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`docs/runbook.md`](./runbook.md) | Operational runbook | specification.md:2-19 (all sections) | - | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
|
||||
### 16.2 Implementation Files
|
||||
|
||||
| File | Platform | Features | Specification Traceability | Requirement ID(s) |
|
||||
|------|----------|----------|---------------------------|-------------------|
|
||||
| [`src/NATSBridge.jl`](../src/NATSBridge.jl) | Julia | Full feature set, Arrow IPC, multiple dispatch | specification.md:2-19 (all sections) | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`src/natsbridge_ssr.js`](../src/natsbridge_ssr.js) | Node.js | Arrow IPC, async/await | specification.md:2-19 (all sections) | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`src/natsbridge_csr.js`](../src/natsbridge_csr.js) | Browser | JSON table only, WebSocket NATS | specification.md:2-19 (all sections) | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`src/natsbridge.py`](../src/natsbridge.py) | Python | Arrow IPC, async/await | specification.md:2-19 (all sections) | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`src/natsbridge.dart`](../src/natsbridge.dart) | Dart | Full feature set, Arrow IPC, async/await | specification.md:2-19 (all sections) | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`src/natsbridge.rs`](../src/natsbridge.rs) | Rust | Full feature set, Arrow IPC, async/await, type-safe | specification.md:2-19 (all sections) | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`src/natsbridge_mpy.py`](../src/natsbridge_mpy.py) | MicroPython | Limited to direct transport | specification.md:2-19 (all sections) | FR-005, FR-006, FR-012 |
|
||||
|
||||
### 16.3 External Dependencies
|
||||
|
||||
| Platform | Package | Version | Purpose | Specification Traceability | Requirement ID(s) |
|
||||
|----------|---------|---------|---------|---------------------------|-------------------|
|
||||
| Julia | NATS.jl | Latest | NATS client | specification.md:11 | FR-013, FR-014, NFR-201 |
|
||||
| Julia | JSON.jl | Latest | JSON serialization | specification.md:11 | FR-012, NFR-101, NFR-102 |
|
||||
| Julia | Arrow.jl | Latest | Arrow IPC support | specification.md:11 | FR-002, FR-012 |
|
||||
| Julia | HTTP.jl | Latest | HTTP file server | specification.md:11 | FR-008, FR-009 |
|
||||
| Julia | UUIDs.jl | Latest | UUID generation | specification.md:11 | FR-011, NFR-401 |
|
||||
| Node.js | nats | Latest | NATS client (TCP) | specification.md:11 | FR-013, FR-014 |
|
||||
| Node.js | node-fetch | Latest | HTTP file server | specification.md:11 | FR-008, FR-009 |
|
||||
| Browser | nats.ws | Latest | NATS client (WebSocket) | specification.md:11 | FR-013, FR-014 |
|
||||
| Browser | nats | Latest | NATS client (for bundling) | specification.md:11 | FR-013, FR-014 |
|
||||
| Python | nats-py | Latest | NATS client | specification.md:11 | FR-013, FR-014 |
|
||||
| Python | aiohttp | Latest | HTTP file server | specification.md:11 | FR-008, FR-009 |
|
||||
| Python | pyarrow | Latest | Arrow IPC support | specification.md:11 | FR-002, FR-012 |
|
||||
| Dart | nats | Latest | NATS client | specification.md:11 | FR-013, FR-014 |
|
||||
| Dart | http | Latest | HTTP file server | specification.md:11 | FR-008, FR-009 |
|
||||
| Dart | uuid | Latest | UUID generation | specification.md:11 | FR-011, NFR-401 |
|
||||
| Dart | dart-arrow | Latest | Arrow IPC support | specification.md:11 | FR-002, FR-012 |
|
||||
| Rust | nats | Latest | NATS client | specification.md:11 | FR-013, FR-014 |
|
||||
| Rust | serde | Latest | JSON serialization | specification.md:11 | FR-012, NFR-101, NFR-102 |
|
||||
| Rust | serde_json | Latest | JSON handling | specification.md:11 | FR-012, NFR-101, NFR-102 |
|
||||
| Rust | tokio | Latest | Async runtime | specification.md:11 | FR-013, FR-014 |
|
||||
| Rust | reqwest | Latest | HTTP file server | specification.md:11 | FR-008, FR-009 |
|
||||
| Rust | uuid | Latest | UUID generation | specification.md:11 | FR-011, NFR-401 |
|
||||
| Rust | arrow2 | Latest | Arrow IPC support | specification.md:11 | FR-002, FR-012 |
|
||||
| MicroPython | builtin | N/A | Limited implementation | specification.md:11 | FR-005, FR-006, FR-012 |
|
||||
|
||||
---
|
||||
|
||||
## 17. Change Log
|
||||
|
||||
| Date | Version | Changes | Specification Reference |
|
||||
|------|---------|---------|------------------------|
|
||||
| 2026-03-23 | 1.1.0 | Updated to ASG Framework architecture guidelines | specification.md:2-19 (all sections) |
|
||||
| 2026-03-15 | 1.1.0 | JavaScript connection management | specification.md:2-19 (all sections) |
|
||||
| 2026-03-13 | 1.0.0 | Initial architecture documentation | specification.md:2-19 (all sections) |
|
||||
|
||||
---
|
||||
|
||||
## 18. Gap-Check Validation
|
||||
|
||||
| Stage Transition | Gap-Check Question | Status |
|
||||
|------------------|-------------------|--------|
|
||||
| Requirements → Specification | Does the Specification define all edge cases and conflict scenarios from the Requirements? | ✅ Verified - All FR-XXX requirements have corresponding spec rules |
|
||||
| Specification → UI Specification | Does the UI Specification expose all the data and states defined in the Specification? | ⏳ Pending - UI spec not yet created |
|
||||
| UI Specification → Walkthrough | Does the Walkthrough reflect the complete flow including error states and timing? | ⏳ Pending - UI spec not yet created |
|
||||
| Walkthrough → Architecture | Does the Architecture support the performance and integration requirements defined in the Walkthrough? | ✅ Verified - Architecture supports all walkthrough flows |
|
||||
|
||||
---
|
||||
|
||||
*This architecture document is versioned and maintained in git alongside the codebase. All implementations must adhere to this architecture.*
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -1,33 +1,39 @@
|
||||
# Requirements Document: NATSBridge
|
||||
|
||||
**Version**: 1.0.0
|
||||
**Date**: 2026-03-13
|
||||
**Version**: 1.2.0
|
||||
**Date**: 2026-05-13
|
||||
**Status**: Active
|
||||
**Ground Truth**: [`src/NATSBridge.jl`](../src/NATSBridge.jl)
|
||||
|
||||
---
|
||||
|
||||
## Executive Summary
|
||||
## 1. Business Context & Success Metrics
|
||||
|
||||
NATSBridge is a cross-platform, bi-directional data bridge that enables seamless communication between **Julia**, **JavaScript**, **Python**, and **MicroPython** applications using NATS as the message bus. The system implements the **Claim-Check pattern** for efficient handling of large payloads (>0.5MB) by uploading them to an HTTP file server instead of sending raw binary data over NATS.
|
||||
### 1.1 Business Goal
|
||||
|
||||
---
|
||||
NATSBridge is a cross-platform, bi-directional data bridge that enables seamless communication between **Julia**, **JavaScript**, **Python**, **Dart**, **Rust**, and **MicroPython** applications using NATS as the message bus. The system implements the **Claim-Check pattern** for efficient handling of large payloads (>0.5MB) by uploading them to an HTTP file server instead of sending raw binary data over NATS.
|
||||
|
||||
## Business Goals
|
||||
### 1.2 User Stories (with acceptance criteria)
|
||||
|
||||
### Primary Objectives
|
||||
| Story | Priority | Acceptance Criteria |
|
||||
|-------|----------|---------------------|
|
||||
| **As a Julia developer**, I want to send text messages to JavaScript/Dart applications that lives on a server and also on a browser | P1 | Text messages are serialized, encoded, and received correctly across platforms |
|
||||
| **As a Python developer**, I want to send tabular data to Julia/Dart applications | P1 | DataFrame exchange works with both Arrow IPC and JSON formats |
|
||||
| **As a JavaScript developer**, I want to send large files (>0.5MB) from JavaScript applications that lives on a server and also on a browser to other applications | P1 | Large files are automatically uploaded to file server and URLs are sent via NATS |
|
||||
| **As a Dart developer**, I want to send text messages to other platforms | P1 | Text messages are serialized, encoded, and received correctly across platforms |
|
||||
| **As a Dart developer**, I want to send dictionary data to other platforms | P1 | JSON-serializable data is exchanged correctly |
|
||||
| **As a Dart developer**, I want to send tabular data (List<Map>) to other platforms | P1 | JSON table format exchange works with Arrow IPC on desktop |
|
||||
| **As a Dart developer**, I want to send large files (>0.5MB) | P1 | Large files are automatically uploaded to file server and URLs are sent via NATS |
|
||||
| **As a MicroPython developer**, I want to send sensor data with minimal memory usage | P1 | Direct transport works for payloads <100KB on memory-constrained devices |
|
||||
| **As a Rust developer**, I want to send and receive messages with type-safe APIs | P1 | Rust implementation uses serde for serialization, tokio for async, and nats-io for NATS connectivity |
|
||||
| **As a developer**, I want to send mixed-content messages (text + image + file) | P1 | NATSBridge accepts list of (dataname, data, type) tuples and handles each payload appropriately |
|
||||
| **As a developer**, I want to receive multi-payload messages | P1 | NATSBridge returns payloads as list of tuples with correct types preserved |
|
||||
| **As a developer**, I want to use Plik as the file server | P2 | Plik one-shot upload mode is supported with upload ID and token handling |
|
||||
| **As a developer**, I want to use custom HTTP file servers | P2 | Handler function abstraction allows plugging in AWS S3 or custom implementations |
|
||||
| **As a developer**, I want automatic retry on file server download failures | P1 | Exponential backoff with configurable retries (default: 5, base_delay: 100ms, max_delay: 5000ms) |
|
||||
| **As a developer**, I want message tracing across distributed systems | P1 | Correlation ID is propagated through all message processing steps |
|
||||
|
||||
1. **Cross-Platform Interoperability**: Enable seamless data exchange between Julia, JavaScript (for both Server-Side rendering and Client-Side rendering webapp), Python, and MicroPython applications without platform-specific barriers.
|
||||
|
||||
2. **Efficient Large Payload Handling**: Implement intelligent transport selection based on payload size:
|
||||
- **Direct Transport**: Small payloads (<0.5MB) sent directly via NATS
|
||||
- **Link Transport**: Large payloads (≥0.5MB) uploaded to HTTP file server, URL sent via NATS
|
||||
|
||||
3. **Unified API Across Platforms**: Provide consistent `smartsend()` and `smartreceive()` functions across all supported platforms while maintaining idiomatic implementations.
|
||||
|
||||
4. **Developer Productivity**: Reduce onboarding time and simplify integration through comprehensive documentation and test examples.
|
||||
|
||||
### Success Metrics
|
||||
### 1.3 KPIs & Targets
|
||||
|
||||
| Metric | Target | Measurement Method |
|
||||
|--------|--------|-------------------|
|
||||
@@ -40,90 +46,22 @@ NATSBridge is a cross-platform, bi-directional data bridge that enables seamless
|
||||
|
||||
---
|
||||
|
||||
## User Stories
|
||||
## 2. Technical Boundaries
|
||||
|
||||
### Core Functionality
|
||||
### 2.1 In Scope
|
||||
|
||||
| Story | Priority | Acceptance Criteria |
|
||||
|-------|----------|---------------------|
|
||||
| **As a Julia developer**, I want to send text messages to JavaScript applications that lives on a server and also on a browser | P1 | Text messages are serialized, encoded, and received correctly across platforms |
|
||||
| **As a Python developer**, I want to send tabular data to Julia applications | P1 | DataFrame exchange works with both Arrow IPC and JSON formats |
|
||||
| **As a JavaScript developer**, I want to send large files (>0.5MB) from JavaScript applications that lives on a server and also on a browser to other applications | P1 | Large files are automatically uploaded to file server and URLs are sent via NATS |
|
||||
| **As a MicroPython developer**, I want to send sensor data with minimal memory usage | P1 | Direct transport works for payloads <100KB on memory-constrained devices |
|
||||
| Feature | Description |
|
||||
|---------|-------------|
|
||||
| Cross-platform interoperability | Seamless data exchange between Julia, JavaScript, Python, Dart, Rust, and MicroPython |
|
||||
| Intelligent transport selection | Direct transport (<0.5MB) vs Link transport (≥0.5MB) based on payload size |
|
||||
| Unified API | Consistent `smartsend()` and `smartreceive()` functions across all platforms |
|
||||
| Multi-payload support | List of (dataname, data, type) tuples with appropriate handling |
|
||||
| File server integration | Plik one-shot upload and custom HTTP server support |
|
||||
| Reliability features | Exponential backoff retry and correlation ID propagation |
|
||||
| Message serialization | Converts data types to binary format (Base64, JSON, Arrow IPC) |
|
||||
| NATS communication | Publishing and subscription via NATS subjects |
|
||||
|
||||
### Multi-Payload Support
|
||||
|
||||
| Story | Priority | Acceptance Criteria |
|
||||
|-------|----------|---------------------|
|
||||
| **As a developer**, I want to send mixed-content messages (text + image + file) | P1 | NATSBridge accepts list of (dataname, data, type) tuples and handles each payload appropriately |
|
||||
| **As a developer**, I want to receive multi-payload messages | P1 | NATSBridge returns payloads as list of tuples with correct types preserved |
|
||||
|
||||
### File Server Integration
|
||||
|
||||
| Story | Priority | Acceptance Criteria |
|
||||
|-------|----------|---------------------|
|
||||
| **As a developer**, I want to use Plik as the file server | P2 | Plik one-shot upload mode is supported with upload ID and token handling |
|
||||
| **As a developer**, I want to use custom HTTP file servers | P2 | Handler function abstraction allows plugging in AWS S3 or custom implementations |
|
||||
|
||||
### Reliability Features
|
||||
|
||||
| Story | Priority | Acceptance Criteria |
|
||||
|-------|----------|---------------------|
|
||||
| **As a developer**, I want automatic retry on file server download failures | P1 | Exponential backoff with configurable retries (default: 5, base_delay: 100ms, max_delay: 5000ms) |
|
||||
| **As a developer**, I want message tracing across distributed systems | P1 | Correlation ID is propagated through all message processing steps |
|
||||
|
||||
---
|
||||
|
||||
## Non-Functional Requirements
|
||||
|
||||
### Performance Requirements
|
||||
|
||||
| Requirement | Specification | Test Method |
|
||||
|-------------|---------------|-------------|
|
||||
| Message serialization overhead | <50ms for 10KB payload | Benchmark tests |
|
||||
| Message deserialization overhead | <50ms for 10KB payload | Benchmark tests |
|
||||
| NATS connection establishment | <100ms | Connection pool benchmarks |
|
||||
| File upload latency | <1s for 0.5MB file | Integration tests |
|
||||
| File download latency | <1s for 0.5MB file | Integration tests |
|
||||
|
||||
### Scalability Requirements
|
||||
|
||||
| Requirement | Specification |
|
||||
|-------------|---------------|
|
||||
| Concurrent connections | Support 100+ simultaneous NATS connections |
|
||||
| Message throughput | Handle 1000+ messages/second per instance |
|
||||
| File server scalability | Support horizontal scaling of file server backend |
|
||||
|
||||
### Reliability Requirements
|
||||
|
||||
| Requirement | Specification |
|
||||
|-------------|---------------|
|
||||
| Message delivery | At-least-once delivery semantics via NATS |
|
||||
| File server availability | Graceful degradation when file server is unavailable |
|
||||
| Connection recovery | Auto-reconnect on NATS connection failure |
|
||||
|
||||
### Security Requirements
|
||||
|
||||
| Requirement | Specification |
|
||||
|-------------|---------------|
|
||||
| Payload integrity | SHA-256 checksum support via metadata |
|
||||
| Transport security | TLS support for NATS connections |
|
||||
| File server security | Authentication token for file uploads |
|
||||
|
||||
### Compatibility Requirements
|
||||
|
||||
| Platform | Minimum Version | Notes |
|
||||
|----------|-----------------|-------|
|
||||
| Julia | 1.7+ | Arrow.jl required for arrowtable support |
|
||||
| Node.js | 16+ | nats.js required |
|
||||
| Python | 3.8+ | pyarrow required for arrowtable support |
|
||||
| MicroPython | 1.19+ | Limited to direct transport |
|
||||
|
||||
---
|
||||
|
||||
## Out of Scope
|
||||
|
||||
### Phase 1 (Current Implementation)
|
||||
### 2.2 Out of Scope
|
||||
|
||||
| Feature | Reason |
|
||||
|---------|--------|
|
||||
@@ -133,94 +71,181 @@ NATSBridge is a cross-platform, bi-directional data bridge that enables seamless
|
||||
| Persistent message queues | NATS request-reply pattern sufficient |
|
||||
| Advanced routing rules | Simple NATS subject matching sufficient |
|
||||
|
||||
### Future Considerations
|
||||
### 2.3 Dependencies
|
||||
|
||||
| Feature | Future Phase |
|
||||
|---------|--------------|
|
||||
| JetStream streams and consumers | Phase 2 |
|
||||
| Message TTL and dead-letter queues | Phase 3 |
|
||||
| Message tracing with OpenTelemetry | Phase 3 |
|
||||
| Rate limiting and quota management | Phase 4 |
|
||||
| Platform | Package | Version |
|
||||
|----------|---------|---------|
|
||||
| Julia | NATS.jl | Latest stable |
|
||||
| Julia | JSON.jl | Latest stable |
|
||||
| Julia | Arrow.jl | Latest stable |
|
||||
| Julia | HTTP.jl | Latest stable |
|
||||
| Julia | UUIDs.jl | Latest stable |
|
||||
| Node.js | nats | Latest stable |
|
||||
| Node.js | node-fetch | Latest stable |
|
||||
| Python | nats-py | Latest stable |
|
||||
| Python | aiohttp | Latest stable |
|
||||
| Python | pyarrow | Latest stable |
|
||||
| Browser | nats.ws | Latest stable |
|
||||
| Dart | nats | Latest stable |
|
||||
| Dart | http | Latest stable |
|
||||
| Dart | uuid | Latest stable |
|
||||
| Rust | nats | Latest stable |
|
||||
| Rust | serde | Latest stable |
|
||||
| Rust | serde_json | Latest stable |
|
||||
| Rust | tokio | Latest stable |
|
||||
| Rust | uuid | Latest stable |
|
||||
|
||||
### 2.4 Platform Compatibility
|
||||
|
||||
| Platform | Minimum Version | Notes |
|
||||
|----------|-----------------|-------|
|
||||
| Julia | 1.7+ | Arrow.jl required for arrowtable support |
|
||||
| Node.js | 16+ | nats.js required, Arrow IPC supported |
|
||||
| Python | 3.8+ | pyarrow required for arrowtable support |
|
||||
| Browser | Latest | No Arrow IPC (uses jsontable only) |
|
||||
| Dart | 2.17+ | Supports Desktop (Dart SDK), Flutter (Dart SDK), and Web (Dart SDK) |
|
||||
| Rust | 1.70+ | Full support with async/await, Arrow IPC on desktop |
|
||||
| MicroPython | 1.19+ | Limited to direct transport |
|
||||
|
||||
---
|
||||
|
||||
## Boundary Definitions
|
||||
## 3. Functional Requirements (FR)
|
||||
|
||||
### What NATSBridge Handles
|
||||
|
||||
| Function | Description |
|
||||
|----------|-------------|
|
||||
| Message serialization | Converts data types to binary format |
|
||||
| Message encoding | Base64, JSON, Arrow IPC encoding |
|
||||
| Transport selection | Direct vs link based on size threshold |
|
||||
| NATS publishing | Publishes messages to NATS subjects |
|
||||
| NATS subscription | Receives and processes NATS messages |
|
||||
| File server upload | Uploads large payloads to HTTP server |
|
||||
| File server download | Downloads payloads from HTTP server with retry |
|
||||
| Correlation ID generation | Creates and propagates UUIDs |
|
||||
| Data deserialization | Converts binary format back to native types |
|
||||
|
||||
### What NATSBridge Does NOT Handle
|
||||
|
||||
| Function | Handled By |
|
||||
|----------|------------|
|
||||
| NATS server management | External NATS deployment |
|
||||
| File server management | External HTTP server deployment |
|
||||
| Application business logic | Application code using NATSBridge |
|
||||
| Message encryption | Application layer |
|
||||
| Message compression | Application layer |
|
||||
| Authentication/Authorization | NATS server configuration |
|
||||
| ID | Requirement | Description |
|
||||
|----|-------------|-------------|
|
||||
| **FR-001** | Cross-platform text messaging | System shall allow users to send text messages between Julia, JavaScript, Python, and MicroPython applications |
|
||||
| **FR-002** | Cross-platform tabular data | System shall support DataFrame exchange between Julia and Python applications using Arrow IPC format |
|
||||
| **FR-003** | Large file handling | System shall automatically detect payloads ≥0.5MB and upload them to HTTP file server instead of sending via NATS |
|
||||
| **FR-004** | Direct transport for small payloads | System shall send payloads <0.5MB directly via NATS without file server upload |
|
||||
| **FR-005** | MicroPython support | System shall support payloads <100KB on MicroPython devices using direct transport |
|
||||
| **FR-006** | Multi-payload messages | System shall accept and process lists of (dataname, data, type) tuples |
|
||||
| **FR-007** | Payload type preservation | System shall preserve payload types when returning multi-payload messages |
|
||||
| **FR-008** | Plik file server integration | System shall support Plik one-shot upload mode with upload ID and token handling |
|
||||
| **FR-009** | Custom file server support | System shall provide handler function abstraction for custom HTTP file server implementations |
|
||||
| **FR-010** | Exponential backoff retry | System shall implement exponential backoff with configurable retries (default: 5, base_delay: 100ms, max_delay: 5000ms) for file server download failures |
|
||||
| **FR-011** | Correlation ID propagation | System shall propagate correlation IDs through all message processing steps |
|
||||
| **FR-012** | Message serialization | System shall serialize data types using Base64, JSON, or Arrow IPC encoding |
|
||||
| **FR-013** | NATS publishing | System shall return JSON string representation for caller to publish to NATS subjects (caller is responsible for actual NATS publish) |
|
||||
| **FR-014** | NATS subscription | System shall receive and process NATS messages by accepting JSON string from NATS payload |
|
||||
|
||||
---
|
||||
|
||||
## Payload Type Requirements
|
||||
## 4. Non-Functional Requirements (NFRs)
|
||||
|
||||
### Supported Payload Types
|
||||
### 4.1 Performance & Scalability
|
||||
|
||||
| Type | Julia | JavaScript | Python | MicroPython | Description |
|
||||
|------|-------|------------|--------|-------------|-------------|
|
||||
| `text` | `String` | `string` | `str` | `str` | Plain text strings |
|
||||
| `dictionary` | `Dict`, `NamedTuple` | `Object`, `Array` | `dict`, `list` | `dict` | JSON-serializable data |
|
||||
| `arrowtable` | `DataFrame`, `Arrow.Table` | `Array<Object>` | `pandas.DataFrame` | ❌ | Tabular data (Arrow IPC) |
|
||||
| `jsontable` | `Vector{NamedTuple}` | `Array<Object>` | `list[dict]` | ⚠️ | Tabular data (JSON) |
|
||||
| `image` | `Vector{UInt8}` | `Uint8Array`, `Buffer` | `bytes` | `bytearray` | Image binary data |
|
||||
| `audio` | `Vector{UInt8}` | `Uint8Array`, `Buffer` | `bytes` | `bytearray` | Audio binary data |
|
||||
| `video` | `Vector{UInt8}` | `Uint8Array`, `Buffer` | `bytes` | `bytearray` | Video binary data |
|
||||
| `binary` | `Vector{UInt8}`, `IOBuffer` | `Uint8Array`, `Buffer` | `bytes`, `bytearray` | `bytearray` | Generic binary data |
|
||||
| ID | Requirement | Specification | Test Method |
|
||||
|----|-------------|---------------|-------------|
|
||||
| **NFR-101** | Message serialization overhead | <50ms for 10KB payload | Benchmark tests |
|
||||
| **NFR-102** | Message deserialization overhead | <50ms for 10KB payload | Benchmark tests |
|
||||
| **NFR-103** | NATS connection establishment | <100ms | Connection pool benchmarks |
|
||||
| **NFR-104** | File upload latency | <1s for 0.5MB file | Integration tests |
|
||||
| **NFR-105** | File download latency | <1s for 0.5MB file | Integration tests |
|
||||
| **NFR-106** | Concurrent connections | Support 100+ simultaneous NATS connections | Scale testing |
|
||||
| **NFR-107** | Message throughput | Handle 1000+ messages/second per instance | Load testing |
|
||||
| **NFR-108** | File server scalability | Support horizontal scaling of file server backend | Architecture review |
|
||||
|
||||
### Encoding Requirements
|
||||
### 4.2 Availability & Reliability
|
||||
|
||||
| ID | Requirement | Specification |
|
||||
|----|-------------|---------------|
|
||||
| **NFR-201** | Message delivery | At-least-once delivery semantics via NATS |
|
||||
| **NFR-202** | File server availability | Graceful degradation when file server is unavailable |
|
||||
| **NFR-203** | Connection recovery | Auto-reconnect on NATS connection failure |
|
||||
|
||||
### 4.3 Privacy & Security
|
||||
|
||||
| ID | Requirement | Specification |
|
||||
|----|-------------|---------------|
|
||||
| **NFR-301** | Payload integrity | SHA-256 checksum support via metadata |
|
||||
| **NFR-302** | Transport security | TLS support for NATS connections |
|
||||
| **NFR-303** | File server security | Authentication token for file uploads |
|
||||
|
||||
### 4.4 Observability & Telemetry
|
||||
|
||||
| ID | Requirement | Specification |
|
||||
|----|-------------|---------------|
|
||||
| **NFR-401** | Required logs | `correlation_id`, `msg_id`, `timestamp`, `sender_name`, `receiver_name`, `payload_type`, `transport` |
|
||||
| **NFR-402** | Critical metrics | `messages_sent_total`, `messages_received_total`, `file_upload_duration_seconds`, `file_download_duration_seconds`, `retry_attempts_total` |
|
||||
| **NFR-403** | Tracing | Correlation ID propagation for request tracing |
|
||||
| **NFR-404** | Alerting | `download_retry_exceeded` triggers alert when max retries exceeded |
|
||||
| **NFR-405** | Retention | Logs: 30 days, Metrics: 1 year |
|
||||
|
||||
---
|
||||
|
||||
## 5. Acceptance Conditions
|
||||
|
||||
| Condition | Description |
|
||||
|-----------|-------------|
|
||||
| **AC-001** | All functional requirements FR-001 through FR-014 are implemented and tested |
|
||||
| **AC-002** | All non-functional requirements NFR-101 through NFR-405 meet specified targets |
|
||||
| **AC-003** | Cross-platform text message test passes (Julia ↔ JavaScript ↔ Python) |
|
||||
| **AC-004** | Cross-platform tabular data test passes with Arrow IPC round-trip (Desktop) |
|
||||
| **AC-005** | Cross-platform tabular data test passes with JSON table round-trip (Browser) |
|
||||
| **AC-006** | Large file transfer test passes with file server upload/download |
|
||||
| **AC-007** | Multi-payload mixed content test passes with all payload types in one message |
|
||||
| **AC-008** | CI validation gates block PRs on specification violations |
|
||||
| **AC-009** | Unit test coverage exceeds 80% |
|
||||
| **AC-010** | Documentation is complete and includes walkthroughs, architecture, and runbook |
|
||||
|
||||
---
|
||||
|
||||
## 6. Payload Type Requirements
|
||||
|
||||
### 6.1 Supported Payload Types
|
||||
|
||||
| Type | Julia | JavaScript | Python | Dart | MicroPython | Description |
|
||||
|------|-------|------------|--------|------|-------------|-------------|
|
||||
| `text` | `String` | `string` | `str` | `String` | `String` | `str` | Plain text strings |
|
||||
| `dictionary` | `Dict`, `NamedTuple` | `Object`, `Array` | `dict`, `list` | `Map`, `serde_json::Value` | `String` | `dict` | JSON-serializable data |
|
||||
| `arrowtable` | `DataFrame`, `Arrow.Table` | ❌ (Browser), ✅ (Node.js) | `pandas.DataFrame` | `List<Map>` (Desktop), `List<dynamic>` (Flutter) | `arrow2::Table` | ❌ | Tabular data (Arrow IPC) |
|
||||
| `jsontable` | `Vector{NamedTuple}` | `Array<Object>` | `list[dict]` | `Vec<Map>` | ⚠️ | Tabular data (JSON) - **Only table type in Browser** |
|
||||
| `image` | `Vector{UInt8}` | `Uint8Array`, `Buffer` | `bytes` | `Uint8List` | `Vec<u8>` | `bytearray` | Image binary data |
|
||||
| `audio` | `Vector{UInt8}` | `Uint8Array`, `Buffer` | `bytes` | `Uint8List` | `Vec<u8>` | `bytearray` | Audio binary data |
|
||||
| `video` | `Vector{UInt8}` | `Uint8Array`, `Buffer` | `bytes` | `Uint8List` | `Vec<u8>` | `bytearray` | Video binary data |
|
||||
| `binary` | `Vector{UInt8}`, `IOBuffer` | `Uint8Array`, `Buffer` | `bytes`, `bytearray` | `Uint8List` | `Vec<u8>` | `bytearray` | Generic binary data |
|
||||
|
||||
### 6.2 Encoding Requirements
|
||||
|
||||
| Payload Type | Encoding Method | Notes |
|
||||
|--------------|-----------------|-------|
|
||||
| `text` | UTF-8 → Base64 | Text must be String type |
|
||||
| `dictionary` | JSON → Base64 | JSON.jl for Julia |
|
||||
| `arrowtable` | Arrow IPC → Base64 | Requires Arrow.jl/pyarrow |
|
||||
| `jsontable` | JSON → Base64 | Human-readable format |
|
||||
| `arrowtable` | Arrow IPC → Base64 | Requires Arrow.jl/pyarrow (Desktop only) |
|
||||
| `jsontable` | JSON → Base64 | Human-readable format - **Browser uses this only** |
|
||||
| `image`/`audio`/`video`/`binary` | Direct → Base64 | Binary data preserved |
|
||||
|
||||
---
|
||||
|
||||
## Size Threshold Requirements
|
||||
## 7. Size Threshold Requirements
|
||||
|
||||
### Direct Transport Threshold
|
||||
### 7.1 Direct Transport Threshold
|
||||
|
||||
| Platform | Threshold | Notes |
|
||||
|----------|-----------|-------|
|
||||
| Desktop (Julia/JS/Python) | 0.5MB | Default size threshold |
|
||||
| Desktop (Julia/JS/Python/Dart) | 0.5MB | Default size threshold |
|
||||
| Dart Desktop | 0.5MB | Default size threshold |
|
||||
| Dart Flutter | 0.5MB | Default size threshold |
|
||||
| Dart Web | 0.5MB | Default size threshold |
|
||||
| Rust | 0.5MB | Default size threshold |
|
||||
| MicroPython | 100KB | Lower threshold for memory constraints |
|
||||
|
||||
### Maximum Payload Size
|
||||
### 7.2 Maximum Payload Size
|
||||
|
||||
| Platform | Maximum | Notes |
|
||||
|----------|---------|-------|
|
||||
| Desktop | Unlimited | Limited by NATS server configuration |
|
||||
| Dart Desktop | Unlimited | Limited by NATS server configuration |
|
||||
| Dart Flutter | Unlimited | Limited by NATS server configuration |
|
||||
| Dart Web | Unlimited | Limited by NATS server configuration |
|
||||
| Rust | Unlimited | Limited by NATS server configuration |
|
||||
| MicroPython | 50KB | Hard limit due to 256KB-1MB memory |
|
||||
|
||||
---
|
||||
|
||||
## Message Envelope Requirements
|
||||
## 8. Message Envelope Requirements
|
||||
|
||||
### Required Fields
|
||||
### 8.1 Required Fields
|
||||
|
||||
| Field | Type | Purpose |
|
||||
|-------|------|---------|
|
||||
@@ -239,7 +264,7 @@ NATSBridge is a cross-platform, bi-directional data bridge that enables seamless
|
||||
| `metadata` | Dict | Message-level metadata |
|
||||
| `payloads` | Array | List of payload objects |
|
||||
|
||||
### Payload Fields
|
||||
### 8.2 Payload Fields
|
||||
|
||||
| Field | Type | Purpose |
|
||||
|-------|------|---------|
|
||||
@@ -254,9 +279,9 @@ NATSBridge is a cross-platform, bi-directional data bridge that enables seamless
|
||||
|
||||
---
|
||||
|
||||
## Error Handling Requirements
|
||||
## 9. Error Handling Requirements
|
||||
|
||||
### Error Codes
|
||||
### 9.1 Error Codes
|
||||
|
||||
| Error | Condition | Response |
|
||||
|-------|-----------|----------|
|
||||
@@ -266,7 +291,7 @@ NATSBridge is a cross-platform, bi-directional data bridge that enables seamless
|
||||
| `Unknown transport` | Invalid transport type | Throw error |
|
||||
| `NATS connection failed` | NATS unavailable | Throw error |
|
||||
|
||||
### Exception Handling
|
||||
### 9.2 Exception Handling
|
||||
|
||||
| Scenario | Handler |
|
||||
|----------|---------|
|
||||
@@ -277,9 +302,9 @@ NATSBridge is a cross-platform, bi-directional data bridge that enables seamless
|
||||
|
||||
---
|
||||
|
||||
## Testing Requirements
|
||||
## 10. Testing Requirements
|
||||
|
||||
### Unit Tests
|
||||
### 10.1 Unit Tests
|
||||
|
||||
| Test Category | Coverage | Files |
|
||||
|---------------|----------|-------|
|
||||
@@ -289,29 +314,30 @@ NATSBridge is a cross-platform, bi-directional data bridge that enables seamless
|
||||
| File server upload | Plik integration | Platform-specific |
|
||||
| File server download | Exponential backoff | Platform-specific |
|
||||
|
||||
### Integration Tests
|
||||
### 10.2 Integration Tests
|
||||
|
||||
| Test Scenario | Success Criteria |
|
||||
|-------------|-----------------|
|
||||
| Cross-platform text message | Julia ↔ JavaScript ↔ Python |
|
||||
| Cross-platform tabular data | Arrow IPC round-trip |
|
||||
| Cross-platform tabular data (Desktop) | Arrow IPC round-trip |
|
||||
| Cross-platform tabular data (Browser) | JSON table round-trip |
|
||||
| Large file transfer | File server upload/download |
|
||||
| Multi-payload mixed content | All payload types in one message |
|
||||
|
||||
---
|
||||
|
||||
## API Contract
|
||||
## 11. API Contract
|
||||
|
||||
### smartsend Signature
|
||||
### 11.1 smartsend Signature
|
||||
|
||||
```julia
|
||||
function smartsend(
|
||||
subject::String,
|
||||
data::AbstractArray{Tuple{String, Any, String}};
|
||||
broker_url::String = "nats://localhost:4222",
|
||||
fileserver_url::String = "http://localhost:8080",
|
||||
data::AbstractArray{Tuple{String, T1, String}, 1};
|
||||
broker_url::String = DEFAULT_BROKER_URL,
|
||||
fileserver_url::String = DEFAULT_FILESERVER_URL,
|
||||
fileserver_upload_handler::Function = plik_oneshot_upload,
|
||||
size_threshold::Int = 1_000_000,
|
||||
size_threshold::Int = DEFAULT_SIZE_THRESHOLD,
|
||||
correlation_id::String = string(uuid4()),
|
||||
msg_purpose::String = "chat",
|
||||
sender_name::String = "NATSBridge",
|
||||
@@ -319,18 +345,18 @@ function smartsend(
|
||||
receiver_id::String = "",
|
||||
reply_to::String = "",
|
||||
reply_to_msg_id::String = "",
|
||||
is_publish::Bool = true,
|
||||
NATS_connection::Union{NATS.Connection, Nothing} = nothing,
|
||||
msg_id::String = string(uuid4()),
|
||||
sender_id::String = string(uuid4())
|
||||
)::Tuple{msg_envelope_v1, String}
|
||||
)::Tuple{msg_envelope_v1, String} where {T1<:Any}
|
||||
```
|
||||
|
||||
### smartreceive Signature
|
||||
**Note**: NATS publishing is the caller's responsibility. `smartsend` returns `(env::msg_envelope_v1, env_json_str::String)`.
|
||||
|
||||
### 11.2 smartreceive Signature
|
||||
|
||||
```julia
|
||||
function smartreceive(
|
||||
msg::NATS.Msg;
|
||||
msg_json_str::String;
|
||||
fileserver_download_handler::Function = _fetch_with_backoff,
|
||||
max_retries::Int = 5,
|
||||
base_delay::Int = 100,
|
||||
@@ -338,83 +364,75 @@ function smartreceive(
|
||||
)::JSON.Object{String, Any}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Dependencies
|
||||
|
||||
### Required Dependencies
|
||||
|
||||
| Platform | Package | Version |
|
||||
|----------|---------|---------|
|
||||
| Julia | NATS.jl | Latest stable |
|
||||
| Julia | JSON.jl | Latest stable |
|
||||
| Julia | Arrow.jl | Latest stable |
|
||||
| Julia | HTTP.jl | Latest stable |
|
||||
| Julia | UUIDs.jl | Latest stable |
|
||||
| Node.js | nats | Latest stable |
|
||||
| Node.js | node-fetch | Latest stable |
|
||||
| Python | nats-py | Latest stable |
|
||||
| Python | aiohttp | Latest stable |
|
||||
| Python | pyarrow | Latest stable |
|
||||
|
||||
### Optional Dependencies
|
||||
|
||||
| Platform | Package | Use Case |
|
||||
|----------|---------|----------|
|
||||
| Julia | DataFrames.jl | DataFrame support for arrowtable |
|
||||
| Python | pandas | DataFrame support for arrowtable |
|
||||
**Note**: Pass `String(nats_msg.payload)` from NATS subscription to `smartreceive`.
|
||||
|
||||
---
|
||||
|
||||
## Deployment Requirements
|
||||
## 12. Deployment Requirements
|
||||
|
||||
### Minimum Infrastructure
|
||||
### 12.1 Minimum Infrastructure
|
||||
|
||||
| Component | Minimum | Notes |
|
||||
|-----------|---------|-------|
|
||||
| NATS Server | 1 instance | Single node for development |
|
||||
| File Server | 1 instance | HTTP server for large payloads |
|
||||
| Client Memory | 50MB | Desktop platforms |
|
||||
| Client Memory | 50MB | Desktop platforms (Julia/JS/Python/Dart) |
|
||||
| Client Memory | 256KB | MicroPython devices |
|
||||
|
||||
### Environment Variables
|
||||
### 12.2 Environment Variables
|
||||
|
||||
| Variable | Default | Description |
|
||||
|----------|---------|-------------|
|
||||
| `NATS_URL` | `nats://localhost:4222` | NATS server URL |
|
||||
| `FILESERVER_URL` | `http://localhost:8080` | HTTP file server URL |
|
||||
| `SIZE_THRESHOLD` | `1000000` | Size threshold in bytes |
|
||||
| `SIZE_THRESHOLD` | `500000` | Size threshold in bytes (0.5MB) |
|
||||
|
||||
---
|
||||
|
||||
## Versioning
|
||||
## 13. Versioning
|
||||
|
||||
### Current Version
|
||||
### 13.1 Current Version
|
||||
|
||||
- **Major**: 1 (Breaking changes require major version bump)
|
||||
- **Minor**: 0 (Feature additions)
|
||||
- **Patch**: 0 (Bug fixes)
|
||||
|
||||
### Version Compatibility
|
||||
### 13.2 Version Compatibility
|
||||
|
||||
| Version | Supported Platforms |
|
||||
|---------|---------------------|
|
||||
| v1.0.x | Julia 1.7+, Node.js 16+, Python 3.8+, MicroPython 1.19+ |
|
||||
| v1.0.x | Julia 1.7+, Node.js 16+, Python 3.8+, Dart 2.17+, Rust 1.70+, Browser (latest), MicroPython 1.19+ |
|
||||
|
||||
---
|
||||
|
||||
## Change Log
|
||||
## 14. Change Log
|
||||
|
||||
| Date | Version | Changes |
|
||||
|------|---------|---------|
|
||||
| 2026-03-13 | 1.0.0 | Initial requirements document |
|
||||
| 2026-05-13 | 1.2.0 | Aligned with ground truth implementation (src/NATSBridge.jl) |
|
||||
| - | - | Fixed smartsend signature: removed is_publish, NATS_connection; added sender_name |
|
||||
| - | - | Fixed smartreceive signature: takes msg_json_str::String instead of msg::NATS.Msg |
|
||||
| - | - | Fixed size_threshold default from 1,000,000 to 500,000 |
|
||||
| - | - | Updated FR-013/FR-014 to reflect caller responsibility for NATS publishing |
|
||||
| - | - | Updated FR-008/FR-009 to include file path upload overload |
|
||||
| - | - | Updated SIZE_THRESHOLD env var default to 500000 |
|
||||
| 2026-03-23 | 1.0.0 | Updated to ASG Framework requirements structure |
|
||||
|
||||
---
|
||||
|
||||
## References
|
||||
## 15. References
|
||||
|
||||
- [`src/NATSBridge.jl`](../src/NATSBridge.jl) - Ground truth implementation
|
||||
- [`src/NATSBridge.jl`](../src/NATSBridge.jl) - Ground truth implementation (Julia)
|
||||
- [`src/natsbridge_ssr.js`](../src/natsbridge_ssr.js) - Server-side JavaScript implementation
|
||||
- [`src/natsbridge_csr.js`](../src/natsbridge_csr.js) - Client-side JavaScript implementation
|
||||
- [`src/natsbridge.py`](../src/natsbridge.py) - Python implementation
|
||||
- [`src/natsbridge.dart`](../src/natsbridge.dart) - Dart implementation
|
||||
- [`src/natsbridge_mpy.py`](../src/natsbridge_mpy.py) - MicroPython implementation
|
||||
- [`src/natsbridge.rs`](../src/natsbridge.rs) - Rust implementation
|
||||
- [`README.md`](../README.md) - Project overview
|
||||
- [`docs/specification.md`](./specification.md) - Technical specification
|
||||
- [`docs/ui-specification.md`](./ui-specification.md) - UI specification
|
||||
- [`docs/walkthrough.md`](./walkthrough.md) - End-to-end walkthrough
|
||||
- [`docs/architecture.md`](./architecture.md) - Architecture documentation
|
||||
- [`docs/implementation.md`](./implementation.md) - Implementation details
|
||||
- [`docs/walkthrough.md`](./walkthrough.md) - Usage examples
|
||||
- [`docs/validation.md`](./validation.md) - Validation and CI/CD
|
||||
- [`docs/runbook.md`](./runbook.md) - Operational runbook
|
||||
@@ -1,16 +1,16 @@
|
||||
# Specification: NATSBridge
|
||||
|
||||
**Version**: 1.0.0
|
||||
**Date**: 2026-03-13
|
||||
**Version**: 1.2.0
|
||||
**Date**: 2026-05-13
|
||||
**Status**: Active
|
||||
**Ground Truth**: [`src/NATSBridge.jl`](../src/NATSBridge.jl)
|
||||
**Specification Format**: JSON Schema + AsyncAPI
|
||||
|
||||
---
|
||||
|
||||
## Executive Summary
|
||||
## 1. Technical Contract Overview
|
||||
|
||||
This document defines the **technical contract** for NATSBridge - the cross-platform bi-directional data bridge that enables seamless communication between **Julia**, **JavaScript**, **Python**, and **MicroPython** applications using NATS as the message bus.
|
||||
This document defines the **technical contract** for NATSBridge - the cross-platform bi-directional data bridge that enables seamless communication between **Julia**, **JavaScript**, **Python**, **Dart**, **Rust**, and **MicroPython** applications using NATS as the message bus.
|
||||
|
||||
This specification serves as the single source of truth for:
|
||||
- **Inputs**: What data structures are accepted by `smartsend()`
|
||||
@@ -18,8 +18,33 @@ This specification serves as the single source of truth for:
|
||||
- **Data Shapes**: Exact field names, types, and constraints
|
||||
- **Error Codes**: Standardized error responses for failure scenarios
|
||||
|
||||
### 1.1 Requirements Traceability
|
||||
|
||||
| Specification Section | Requirement ID(s) | Description |
|
||||
|----------------------|-------------------|-------------|
|
||||
| Section 2 (Message Envelope) | FR-012, FR-013, NFR-101, NFR-102 | Message envelope structure and validation |
|
||||
| Section 3 (Payload Schema) | FR-001, FR-002, FR-003, FR-004, NFR-101, NFR-102 | Payload structure and field definitions |
|
||||
| Section 4 (Payload Format) | FR-006, FR-007 | Tuple format for smartsend() |
|
||||
| Section 5 (Enumerations) | FR-003, FR-004, FR-006, NFR-101 | Enumerations for transport and encoding |
|
||||
| Section 6 (Transport Protocols) | FR-003, FR-004, NFR-104, NFR-105 | Direct and link transport protocols |
|
||||
| Section 7 (Size Thresholds) | FR-004, FR-005, NFR-104, NFR-105 | Size thresholds for transport selection |
|
||||
| Section 8 (NATS Subject Convention) | FR-013, FR-014 | NATS subject naming patterns |
|
||||
| Section 9 (Error Handling) | FR-010, FR-011, NFR-201, NFR-202, NFR-203 | Error codes and exception handling |
|
||||
| Section 10 (Serialization Rules) | FR-001, FR-002, FR-003, FR-012, NFR-101, NFR-102 | Serialization and encoding rules |
|
||||
| Section 11 (API Contract) | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-008, FR-009, FR-010, FR-011, FR-012, FR-013, FR-014 | Function signatures for all platforms |
|
||||
| Section 12 (File Server Interface) | FR-008, FR-009, FR-010 | Upload and download handler contracts |
|
||||
| Section 13 (Platform-Specific Constraints) | FR-005, FR-006, NFR-106, NFR-107 | Platform-specific feature support |
|
||||
| Section 14 (Implementation Files) | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007 | Implementation file mapping |
|
||||
| Section 15 (Message Flow) | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-008, FR-009, FR-010, FR-011, FR-012, FR-013, FR-014 | Mermaid diagrams for send/receive flows |
|
||||
| Section 16 (Validation Rules) | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-008, FR-009, FR-010, FR-011, FR-012, FR-013, FR-014 | Envelope and payload validation rules |
|
||||
| Section 17 (Test Contracts) | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-008, FR-009, FR-010, FR-011, FR-012, FR-013, FR-014 | Unit and integration test scenarios |
|
||||
| Section 18 (Dependencies) | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-008, FR-009, FR-010, FR-011, FR-012, FR-013, FR-014 | Platform-specific dependencies |
|
||||
| Section 19 (Change Log) | N/A | Version history and changes |
|
||||
|
||||
---
|
||||
|
||||
## 2. Message Envelope Schema
|
||||
|
||||
## Specification Versioning
|
||||
|
||||
| Component | Version | Notes |
|
||||
@@ -65,22 +90,22 @@ This specification serves as the single source of truth for:
|
||||
|
||||
### Field Definitions
|
||||
|
||||
| Field | Type | Required | Validation | Description |
|
||||
|-------|------|----------|------------|-------------|
|
||||
| `correlation_id` | `string` | Yes | UUID v4 format | Track message flow across distributed systems |
|
||||
| `msg_id` | `string` | Yes | UUID v4 format | Unique identifier for this specific message |
|
||||
| `timestamp` | `string` | Yes | ISO 8601 UTC | Message publication timestamp (e.g., `2026-03-13T07:02:50.443Z`) |
|
||||
| `send_to` | `string` | Yes | Non-empty string | NATS subject/topic to publish the message to |
|
||||
| `msg_purpose` | `string` | Yes | Enum | Purpose of the message (see `msg_purpose` enum) |
|
||||
| `sender_name` | `string` | Yes | Non-empty string | Name of the sender application |
|
||||
| `sender_id` | `string` | Yes | UUID v4 format | Unique identifier for the sender |
|
||||
| `receiver_name` | `string` | Yes | Any string | Name of the receiver (empty = broadcast) |
|
||||
| `receiver_id` | `string` | Yes | Any string | UUID of the receiver (empty = broadcast) |
|
||||
| `reply_to` | `string` | Yes | Any string | Topic where receiver should reply (empty = no reply expected) |
|
||||
| `reply_to_msg_id` | `string` | Yes | Any string | Message ID this message is replying to |
|
||||
| `broker_url` | `string` | Yes | Valid URL | NATS broker URL |
|
||||
| `metadata` | `object` | No | Any JSON object | Message-level metadata |
|
||||
| `payloads` | `array` | Yes | Non-empty array | List of payload objects |
|
||||
| Field | Type | Required | Validation | Description | Requirement ID |
|
||||
|-------|------|----------|------------|-------------|----------------|
|
||||
| `correlation_id` | `string` | Yes | UUID v4 format | Track message flow across distributed systems | FR-011, NFR-401 |
|
||||
| `msg_id` | `string` | Yes | UUID v4 format | Unique identifier for this specific message | FR-012, NFR-401 |
|
||||
| `timestamp` | `string` | Yes | ISO 8601 UTC | Message publication timestamp | FR-012, NFR-401 |
|
||||
| `send_to` | `string` | Yes | Non-empty string | NATS subject/topic to publish the message to | FR-013 |
|
||||
| `msg_purpose` | `string` | Yes | Enum | Purpose of the message | FR-013 |
|
||||
| `sender_name` | `string` | Yes | Non-empty string | Name of the sender application | FR-013 |
|
||||
| `sender_id` | `string` | Yes | UUID v4 format | Unique identifier for the sender | FR-013 |
|
||||
| `receiver_name` | `string` | Yes | Any string | Name of the receiver (empty = broadcast) | FR-013 |
|
||||
| `receiver_id` | `string` | Yes | Any string | UUID of the receiver (empty = broadcast) | FR-013 |
|
||||
| `reply_to` | `string` | Yes | Any string | Topic where receiver should reply | FR-013 |
|
||||
| `reply_to_msg_id` | `string` | Yes | Any string | Message ID this message is replying to | FR-013 |
|
||||
| `broker_url` | `string` | Yes | Valid URL | NATS broker URL | FR-013 |
|
||||
| `metadata` | `object` | No | Any JSON object | Message-level metadata | NFR-401 |
|
||||
| `payloads` | `array` | Yes | Non-empty array | List of payload objects | FR-012, FR-013 |
|
||||
|
||||
---
|
||||
|
||||
@@ -103,16 +128,16 @@ This specification serves as the single source of truth for:
|
||||
|
||||
### Payload Field Definitions
|
||||
|
||||
| Field | Type | Required | Validation | Description |
|
||||
|-------|------|----------|------------|-------------|
|
||||
| `id` | `string` | Yes | UUID v4 format | Unique identifier for this payload |
|
||||
| `dataname` | `string` | Yes | Non-empty string | Name of the payload (e.g., `login_image`, `user_data`) |
|
||||
| `payload_type` | `string` | Yes | Enum | Type of payload (see `payload_type` enum) |
|
||||
| `transport` | `string` | Yes | Enum | Transport method: `direct` or `link` |
|
||||
| `encoding` | `string` | Yes | Enum | Encoding method (see `encoding` enum) |
|
||||
| `size` | `integer` | Yes | Positive integer | Size of the payload in bytes |
|
||||
| `data` | `string` or `URL` | Yes | Base64 string or URL | Payload data (base64 for direct, URL for link) |
|
||||
| `metadata` | `object` | No | Any JSON object | Payload-level metadata |
|
||||
| Field | Type | Required | Validation | Description | Requirement ID |
|
||||
|-------|------|----------|------------|-------------|----------------|
|
||||
| `id` | `string` | Yes | UUID v4 format | Unique identifier for this payload | FR-012 |
|
||||
| `dataname` | `string` | Yes | Non-empty string | Name of the payload (e.g., `login_image`, `user_data`) | FR-001, FR-002, FR-003 |
|
||||
| `payload_type` | `string` | Yes | Enum | Type of payload (see `payload_type` enum) | FR-001, FR-002, FR-003, FR-006 |
|
||||
| `transport` | `string` | Yes | Enum | Transport method: `direct` or `link` | FR-003, FR-004, NFR-104, NFR-105 |
|
||||
| `encoding` | `string` | Yes | Enum | Encoding method (see `encoding` enum) | FR-001, FR-002, FR-003, FR-012 |
|
||||
| `size` | `integer` | Yes | Positive integer | Size of the payload in bytes | FR-003, FR-004, NFR-104, NFR-105 |
|
||||
| `data` | `string` or `URL` | Yes | Base64 string or URL | Payload data (base64 for direct, URL for link) | FR-003, FR-004, FR-008, FR-009, FR-010 |
|
||||
| `metadata` | `object` | No | Any JSON object | Payload-level metadata | NFR-401 |
|
||||
|
||||
---
|
||||
|
||||
@@ -176,6 +201,7 @@ await smartsend("/agent/v1/process", data)
|
||||
| All | `String` | `"text"` |
|
||||
| All | `Dict`/`Object` | `"dictionary"` |
|
||||
| Desktop | `DataFrame` | `"arrowtable"` or `"jsontable"` |
|
||||
| Browser | `Array` of objects | `"jsontable"` (only table type) |
|
||||
| All | `Array` of objects | `"jsontable"` |
|
||||
| All | `Uint8Array`/`Buffer`/`bytes` | `"binary"` |
|
||||
| Desktop | `Arrow.Table` | `"arrowtable"` |
|
||||
@@ -203,8 +229,8 @@ await smartsend("/agent/v1/process", data)
|
||||
|-------|-------------|---------------------|------------------|
|
||||
| `text` | Plain text string | All | `base64` |
|
||||
| `dictionary` | JSON object/dictionary | All | `base64`, `json` |
|
||||
| `arrowtable` | Apache Arrow IPC table | Desktop (Julia/JS/Python) | `base64`, `arrow-ipc` |
|
||||
| `jsontable` | JSON array of objects | All | `base64`, `json` |
|
||||
| `arrowtable` | Apache Arrow IPC table | Desktop (Julia/Python/Node.js/Dart) | `base64`, `arrow-ipc` |
|
||||
| `jsontable` | JSON array of objects | All (including Browser/Dart Web) | `base64`, `json` |
|
||||
| `image` | Binary image data | All | `base64` |
|
||||
| `audio` | Binary audio data | All | `base64` |
|
||||
| `video` | Binary video data | All | `base64` |
|
||||
@@ -324,25 +350,25 @@ When `transport = "link"`, the `data` field contains a URL pointing to the uploa
|
||||
|
||||
### Error Codes
|
||||
|
||||
| Code | HTTP Status | Description | Recovery |
|
||||
|------|-------------|-------------|----------|
|
||||
| `INVALID_ENVELOPE` | 400 | Message envelope validation failed | Fix envelope structure |
|
||||
| `INVALID_PAYLOAD_TYPE` | 400 | Unsupported payload type | Use supported payload_type |
|
||||
| `INVALID_TRANSPORT` | 400 | Unsupported transport type | Use `direct` or `link` |
|
||||
| `UPLOAD_FAILED` | 500 | File server upload failed | Retry or use direct transport |
|
||||
| `DOWNLOAD_FAILED` | 503 | File server download failed | Retry with exponential backoff |
|
||||
| `NATS_CONNECTION_FAILED` | 503 | NATS connection failed | Check NATS server availability |
|
||||
| `DESERIALIZATION_ERROR` | 500 | Payload deserialization failed | Check payload_type matches data |
|
||||
| `SIZE_EXCEEDED` | 413 | Payload exceeds maximum size | Split payload or use link transport |
|
||||
| Code | HTTP Status | Description | Recovery | Requirement ID |
|
||||
|------|-------------|-------------|----------|----------------|
|
||||
| `INVALID_ENVELOPE` | 400 | Message envelope validation failed | Fix envelope structure | FR-012, FR-013, FR-014 |
|
||||
| `INVALID_PAYLOAD_TYPE` | 400 | Unsupported payload type | Use supported payload_type | FR-001, FR-002, FR-003, FR-006 |
|
||||
| `INVALID_TRANSPORT` | 400 | Unsupported transport type | Use `direct` or `link` | FR-003, FR-004, FR-006 |
|
||||
| `UPLOAD_FAILED` | 500 | File server upload failed | Retry or use direct transport | FR-008, FR-009 |
|
||||
| `DOWNLOAD_FAILED` | 503 | File server download failed | Retry with exponential backoff | FR-010, FR-011, NFR-201, NFR-202 |
|
||||
| `NATS_CONNECTION_FAILED` | 503 | NATS connection failed | Check NATS server availability | FR-013, FR-014, NFR-201, NFR-203 |
|
||||
| `DESERIALIZATION_ERROR` | 500 | Payload deserialization failed | Check payload_type matches data | FR-001, FR-002, FR-003, FR-012 |
|
||||
| `SIZE_EXCEEDED` | 413 | Payload exceeds maximum size | Split payload or use link transport | FR-003, FR-004, FR-005, NFR-104, NFR-105 |
|
||||
|
||||
### Exception Handling
|
||||
|
||||
| Scenario | Handler | Retry Policy |
|
||||
|----------|---------|--------------|
|
||||
| File server unavailable | Retry up to 5 times | Exponential backoff (100ms → 5000ms) |
|
||||
| NATS publish failure | Connection auto-reconnect | TCP-level reconnection |
|
||||
| Deserialization error | Log correlation ID and throw | No retry (data corruption) |
|
||||
| Memory overflow (MicroPython) | Reject payloads >50KB | No retry (client-side check) |
|
||||
| Scenario | Handler | Retry Policy | Requirement ID |
|
||||
|----------|---------|--------------|----------------|
|
||||
| File server unavailable | Retry up to 5 times | Exponential backoff (100ms → 5000ms) | FR-010, NFR-202 |
|
||||
| NATS publish failure | Connection auto-reconnect | TCP-level reconnection | FR-013, FR-014, NFR-201, NFR-203 |
|
||||
| Deserialization error | Log correlation ID and throw | No retry (data corruption) | FR-001, FR-002, FR-003, FR-012, NFR-401 |
|
||||
| Memory overflow (MicroPython) | Reject payloads >50KB | No retry (client-side check) | FR-005, NFR-106 |
|
||||
|
||||
---
|
||||
|
||||
@@ -392,11 +418,11 @@ When `transport = "link"`, the `data` field contains a URL pointing to the uploa
|
||||
```julia
|
||||
function smartsend(
|
||||
subject::String,
|
||||
data::AbstractArray{Tuple{String, Any, String}};
|
||||
broker_url::String = "nats://localhost:4222",
|
||||
fileserver_url::String = "http://localhost:8080",
|
||||
data::AbstractArray{Tuple{String, T1, String}, 1};
|
||||
broker_url::String = DEFAULT_BROKER_URL,
|
||||
fileserver_url::String = DEFAULT_FILESERVER_URL,
|
||||
fileserver_upload_handler::Function = plik_oneshot_upload,
|
||||
size_threshold::Int = 500_000,
|
||||
size_threshold::Int = DEFAULT_SIZE_THRESHOLD,
|
||||
correlation_id::String = string(uuid4()),
|
||||
msg_purpose::String = "chat",
|
||||
sender_name::String = "NATSBridge",
|
||||
@@ -404,13 +430,13 @@ function smartsend(
|
||||
receiver_id::String = "",
|
||||
reply_to::String = "",
|
||||
reply_to_msg_id::String = "",
|
||||
is_publish::Bool = true,
|
||||
NATS_connection::Union{NATS.Connection, Nothing} = nothing,
|
||||
msg_id::String = string(uuid4()),
|
||||
sender_id::String = string(uuid4())
|
||||
)::Tuple{msg_envelope_v1, String}
|
||||
)::Tuple{msg_envelope_v1, String} where {T1<:Any}
|
||||
```
|
||||
|
||||
**Note**: NATS publishing is the caller's responsibility. Returns `(env::msg_envelope_v1, env_json_str::String)`.
|
||||
|
||||
#### Python
|
||||
|
||||
```python
|
||||
@@ -428,13 +454,13 @@ async def smartsend(
|
||||
receiver_id: str = "",
|
||||
reply_to: str = "",
|
||||
reply_to_msg_id: str = "",
|
||||
is_publish: bool = True,
|
||||
nats_connection: Any = None,
|
||||
msg_id: str = None,
|
||||
sender_id: str = None
|
||||
) -> Tuple[Dict, str]:
|
||||
```
|
||||
|
||||
**Note**: NATS publishing is the caller's responsibility.
|
||||
|
||||
#### JavaScript (Node.js)
|
||||
|
||||
```typescript
|
||||
@@ -453,14 +479,14 @@ async function smartsend(
|
||||
receiver_id?: string;
|
||||
reply_to?: string;
|
||||
reply_to_msg_id?: string;
|
||||
is_publish?: boolean;
|
||||
nats_connection?: NATS.Connection;
|
||||
msg_id?: string;
|
||||
sender_id?: string;
|
||||
}
|
||||
): Promise<[Object, string]>;
|
||||
```
|
||||
|
||||
**Note**: NATS publishing is the caller's responsibility.
|
||||
|
||||
#### JavaScript (Browser)
|
||||
|
||||
```typescript
|
||||
@@ -479,31 +505,145 @@ async function smartsend(
|
||||
receiver_id?: string;
|
||||
reply_to?: string;
|
||||
reply_to_msg_id?: string;
|
||||
is_publish?: boolean;
|
||||
nats_connection?: NATS.Connection;
|
||||
msg_id?: string;
|
||||
sender_id?: string;
|
||||
}
|
||||
): Promise<[Object, string]>;
|
||||
```
|
||||
|
||||
**Note**: NATS publishing is the caller's responsibility.
|
||||
|
||||
#### MicroPython
|
||||
|
||||
```python
|
||||
def smartsend(
|
||||
subject: str,
|
||||
data: List[Tuple[str, Any, str]],
|
||||
size_threshold: int = 100_000, # Lower threshold for memory constraints
|
||||
**kwargs
|
||||
) -> Tuple[Dict, str]:
|
||||
```
|
||||
|
||||
**Note**: NATS publishing is the caller's responsibility.
|
||||
|
||||
#### Dart (Desktop/Flutter)
|
||||
|
||||
```dart
|
||||
Future<[Map<String, dynamic>, String]> smartsend(
|
||||
String subject,
|
||||
List<List<dynamic>> data, {
|
||||
String brokerUrl = 'nats://localhost:4222',
|
||||
String fileserverUrl = 'http://localhost:8080',
|
||||
Function? fileserverUploadHandler,
|
||||
int sizeThreshold = 500000,
|
||||
String? correlationId,
|
||||
String msgPurpose = 'chat',
|
||||
String senderName = 'NATSBridge',
|
||||
String receiverName = '',
|
||||
String receiverId = '',
|
||||
String replyTo = '',
|
||||
String replyToMsgId = '',
|
||||
String? msgId,
|
||||
String? senderId,
|
||||
}) async {
|
||||
// Returns [envelope, jsonString]
|
||||
// NATS publishing is caller's responsibility
|
||||
}
|
||||
```
|
||||
|
||||
#### Dart Web
|
||||
|
||||
```dart
|
||||
Future<[Map<String, dynamic>, String]> smartsend(
|
||||
String subject,
|
||||
List<List<dynamic>> data, {
|
||||
String brokerUrl = 'nats://localhost:4222',
|
||||
String fileserverUrl = 'http://localhost:8080',
|
||||
Function? fileserverUploadHandler,
|
||||
int sizeThreshold = 500000,
|
||||
String? correlationId,
|
||||
String msgPurpose = 'chat',
|
||||
String senderName = 'NATSBridge',
|
||||
String receiverName = '',
|
||||
String receiverId = '',
|
||||
String replyTo = '',
|
||||
String replyToMsgId = '',
|
||||
String? msgId,
|
||||
String? senderId,
|
||||
}) async {
|
||||
// Returns [envelope, jsonString]
|
||||
// NATS publishing is caller's responsibility
|
||||
}
|
||||
```
|
||||
|
||||
#### Rust
|
||||
|
||||
```rust
|
||||
pub async fn smartsend(
|
||||
subject: &str,
|
||||
data: &[(String, Payload, String)],
|
||||
options: &SmartsendOptions,
|
||||
) -> Result<(MsgEnvelopeV1, String), NatSBridgeError>
|
||||
|
||||
// SmartsendOptions struct
|
||||
pub struct SmartsendOptions {
|
||||
pub broker_url: String,
|
||||
pub fileserver_url: String,
|
||||
pub fileserver_upload_handler: Option<UploadHandler>,
|
||||
pub size_threshold: usize,
|
||||
pub correlation_id: String,
|
||||
pub msg_purpose: String,
|
||||
pub sender_name: String,
|
||||
pub receiver_name: String,
|
||||
pub receiver_id: String,
|
||||
pub reply_to: String,
|
||||
pub reply_to_msg_id: String,
|
||||
pub msg_id: String,
|
||||
pub sender_id: String,
|
||||
}
|
||||
|
||||
// Payload enum for type-safe data handling
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub enum Payload {
|
||||
Text(String),
|
||||
Dictionary(serde_json::Value),
|
||||
ArrowTable(Vec<u8>),
|
||||
JsonTable(serde_json::Value),
|
||||
Image(Vec<u8>),
|
||||
Audio(Vec<u8>),
|
||||
Video(Vec<u8>),
|
||||
Binary(Vec<u8>),
|
||||
}
|
||||
|
||||
// MsgEnvelopeV1 struct (serde-serializable)
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct MsgEnvelopeV1 {
|
||||
pub correlation_id: String,
|
||||
pub msg_id: String,
|
||||
pub timestamp: String,
|
||||
pub send_to: String,
|
||||
pub msg_purpose: String,
|
||||
pub sender_name: String,
|
||||
pub sender_id: String,
|
||||
pub receiver_name: String,
|
||||
pub receiver_id: String,
|
||||
pub reply_to: String,
|
||||
pub reply_to_msg_id: String,
|
||||
pub broker_url: String,
|
||||
pub metadata: serde_json::Value,
|
||||
pub payloads: Vec<MsgPayloadV1>,
|
||||
}
|
||||
```
|
||||
|
||||
**Note**: NATS publishing is the caller's responsibility. Returns `Result<(MsgEnvelopeV1, String), NatSBridgeError>`. Uses `serde` for JSON serialization.
|
||||
|
||||
### `smartreceive` Function Signature
|
||||
|
||||
#### Julia
|
||||
|
||||
```julia
|
||||
function smartreceive(
|
||||
msg::NATS.Msg;
|
||||
msg_json_str::String; # Pass String(nats_msg.payload) from NATS subscription
|
||||
fileserver_download_handler::Function = _fetch_with_backoff,
|
||||
max_retries::Int = 5,
|
||||
base_delay::Int = 100,
|
||||
@@ -511,11 +651,13 @@ function smartreceive(
|
||||
)::JSON.Object{String, Any}
|
||||
```
|
||||
|
||||
**Note**: Input is JSON string from NATS message payload, not NATS.Msg directly.
|
||||
|
||||
#### Python
|
||||
|
||||
```python
|
||||
async def smartreceive(
|
||||
msg: Any,
|
||||
msg_json_str: str, # JSON string from NATS message payload
|
||||
fileserver_download_handler: Callable = fetch_with_backoff,
|
||||
max_retries: int = 5,
|
||||
base_delay: int = 100,
|
||||
@@ -523,11 +665,13 @@ async def smartreceive(
|
||||
) -> Dict[str, Any]:
|
||||
```
|
||||
|
||||
**Note**: Input is JSON string from NATS message payload.
|
||||
|
||||
#### JavaScript (Node.js)
|
||||
|
||||
```typescript
|
||||
async function smartreceive(
|
||||
msg: Object,
|
||||
msg_json_str: string, // JSON string from NATS message payload
|
||||
options?: {
|
||||
fileserver_download_handler?: Function;
|
||||
max_retries?: number;
|
||||
@@ -541,7 +685,7 @@ async function smartreceive(
|
||||
|
||||
```typescript
|
||||
async function smartreceive(
|
||||
msg: Object,
|
||||
msg_json_str: string, // JSON string from NATS message payload
|
||||
options?: {
|
||||
fileserver_download_handler?: Function;
|
||||
max_retries?: number;
|
||||
@@ -551,12 +695,65 @@ async function smartreceive(
|
||||
): Promise<Object>;
|
||||
```
|
||||
|
||||
**Note**: Input is JSON string from NATS message payload.
|
||||
|
||||
#### MicroPython
|
||||
|
||||
```python
|
||||
def smartreceive(msg: Any, **kwargs) -> Dict[str, Any]:
|
||||
def smartreceive(msg_json_str: str, **kwargs) -> Dict[str, Any]:
|
||||
```
|
||||
|
||||
**Note**: Input is JSON string from NATS message payload.
|
||||
|
||||
#### Dart (Desktop/Flutter)
|
||||
|
||||
```dart
|
||||
Future<Map<String, dynamic>> smartreceive(
|
||||
Map<String, dynamic> msg_json_str, // JSON object from NATS message payload
|
||||
{
|
||||
Function? fileserverDownloadHandler,
|
||||
int maxRetries = 5,
|
||||
int baseDelay = 100,
|
||||
int maxDelay = 5000,
|
||||
}) async {
|
||||
// Returns envelope with processed payloads
|
||||
}
|
||||
```
|
||||
|
||||
#### Dart Web
|
||||
|
||||
```dart
|
||||
Future<Map<String, dynamic>> smartreceive(
|
||||
Map<String, dynamic> msg_json_str, // JSON object from NATS message payload
|
||||
{
|
||||
Function? fileserverDownloadHandler,
|
||||
int maxRetries = 5,
|
||||
int baseDelay = 100,
|
||||
int maxDelay = 5000,
|
||||
}) async {
|
||||
// Returns envelope with processed payloads
|
||||
}
|
||||
```
|
||||
|
||||
#### Rust
|
||||
|
||||
```rust
|
||||
pub async fn smartreceive(
|
||||
msg_json_str: &str, // JSON string from NATS message payload
|
||||
options: &SmartreceiveOptions,
|
||||
) -> Result<MsgEnvelopeV1, NatSBridgeError>
|
||||
|
||||
// SmartreceiveOptions struct
|
||||
pub struct SmartreceiveOptions {
|
||||
pub fileserver_download_handler: Option<DownloadHandler>,
|
||||
pub max_retries: u32,
|
||||
pub base_delay: u64,
|
||||
pub max_delay: u64,
|
||||
}
|
||||
```
|
||||
|
||||
**Note**: Input is JSON string from NATS message payload. Returns `Result<MsgEnvelopeV1, NatSBridgeError>`.
|
||||
|
||||
---
|
||||
|
||||
## File Server Interface
|
||||
@@ -570,6 +767,12 @@ function fileserver_upload_handler(
|
||||
dataname::String,
|
||||
data::Vector{UInt8}
|
||||
)::Dict{String, Any}
|
||||
|
||||
# Overload: Upload file from disk
|
||||
function fileserver_upload_handler(
|
||||
file_server_url::String,
|
||||
filepath::String
|
||||
)::Dict{String, Any}
|
||||
```
|
||||
|
||||
**Return Format**:
|
||||
@@ -613,16 +816,68 @@ function fileserver_download_handler(
|
||||
|
||||
## Platform-Specific Constraints
|
||||
|
||||
### Desktop (Julia/JS/Python)
|
||||
### Desktop (Julia/Python/Node.js/Dart)
|
||||
|
||||
| Feature | Status | Notes |
|
||||
|---------|--------|-------|
|
||||
| Arrow IPC | ✅ Supported | Requires Arrow.jl/pyarrow |
|
||||
| Arrow IPC | ✅ Supported | Requires Arrow.jl/pyarrow/dart-arrow |
|
||||
| JSON table | ✅ Supported | Human-readable format |
|
||||
| File server upload | ✅ Supported | HTTP/HTTPS |
|
||||
| File server download | ✅ Supported | HTTP/HTTPS |
|
||||
| Size threshold | 500KB | Configurable |
|
||||
|
||||
### Browser (JavaScript)
|
||||
|
||||
| Feature | Status | Notes |
|
||||
|---------|--------|-------|
|
||||
| Arrow IPC | ❌ Not supported | Apache Arrow not browser-compatible |
|
||||
| JSON table | ✅ Supported | Only table type available in browser |
|
||||
| File server upload | ✅ Supported | HTTP/HTTPS |
|
||||
| File server download | ✅ Supported | HTTP/HTTPS |
|
||||
| Size threshold | 500KB | Configurable |
|
||||
|
||||
### Dart Desktop (Dart SDK)
|
||||
|
||||
| Feature | Status | Notes |
|
||||
|---------|--------|-------|
|
||||
| Arrow IPC | ✅ Supported | Requires dart-arrow package |
|
||||
| JSON table | ✅ Supported | Human-readable format |
|
||||
| File server upload | ✅ Supported | HTTP/HTTPS |
|
||||
| File server download | ✅ Supported | HTTP/HTTPS |
|
||||
| Size threshold | 500KB | Configurable |
|
||||
|
||||
### Dart Flutter (Dart SDK)
|
||||
|
||||
| Feature | Status | Notes |
|
||||
|---------|--------|-------|
|
||||
| Arrow IPC | ✅ Supported | Requires dart-arrow package |
|
||||
| JSON table | ✅ Supported | Human-readable format |
|
||||
| File server upload | ✅ Supported | HTTP/HTTPS |
|
||||
| File server download | ✅ Supported | HTTP/HTTPS |
|
||||
| Size threshold | 500KB | Configurable |
|
||||
|
||||
### Dart Web (Dart SDK)
|
||||
|
||||
| Feature | Status | Notes |
|
||||
|---------|--------|-------|
|
||||
| Arrow IPC | ❌ Not supported | Apache Arrow not browser-compatible |
|
||||
| JSON table | ✅ Supported | Only table type available in browser |
|
||||
| File server upload | ✅ Supported | HTTP/HTTPS |
|
||||
| File server download | ✅ Supported | HTTP/HTTPS |
|
||||
| Size threshold | 500KB | Configurable |
|
||||
|
||||
### Rust
|
||||
|
||||
| Feature | Status | Notes |
|
||||
|---------|--------|-------|
|
||||
| Arrow IPC | ✅ Supported | Requires `arrow2` crate |
|
||||
| JSON table | ✅ Supported | Uses `serde_json` |
|
||||
| File server upload | ✅ Supported | HTTP/HTTPS via `reqwest` |
|
||||
| File server download | ✅ Supported | HTTP/HTTPS via `reqwest` with retry |
|
||||
| Size threshold | 500KB | Configurable |
|
||||
| Async runtime | ✅ Supported | Uses `tokio` for async I/O |
|
||||
| Type safety | ✅ Supported | Compile-time type checking via Rust enums |
|
||||
|
||||
### MicroPython
|
||||
|
||||
| Feature | Status | Notes |
|
||||
@@ -636,6 +891,43 @@ function fileserver_download_handler(
|
||||
|
||||
---
|
||||
|
||||
## Implementation Files
|
||||
|
||||
| File | Platform | Features | Notes |
|
||||
|------|----------|----------|-------|
|
||||
| [`src/NATSBridge.jl`](../src/NATSBridge.jl) | Julia | Full feature set, Arrow IPC, multiple dispatch | Ground truth implementation |
|
||||
| [`src/natsbridge_ssr.js`](../src/natsbridge_ssr.js) | Node.js | Arrow IPC, async/await | Server-side JavaScript |
|
||||
| [`src/natsbridge_csr.js`](../src/natsbridge_csr.js) | Browser | JSON table only, WebSocket NATS | Client-side rendering |
|
||||
| [`src/natsbridge.py`](../src/natsbridge.py) | Python | Arrow IPC, async/await | Desktop Python |
|
||||
| [`src/natsbridge.dart`](../src/natsbridge.dart) | Dart | Full feature set, Arrow IPC, async/await | Desktop/Flutter/Web |
|
||||
| [`src/natsbridge.rs`](../src/natsbridge.rs) | Rust | Full feature set, Arrow IPC, async/await, type-safe | Uses tokio + serde + arrow2 |
|
||||
| [`src/natsbridge_mpy.py`](../src/natsbridge_mpy.py) | MicroPython | Limited to direct transport | Memory-constrained |
|
||||
|
||||
### Browser Implementation Notes
|
||||
|
||||
The browser implementation ([`src/natsbridge_csr.js`](../src/natsbridge_csr.js)) has the following constraints:
|
||||
|
||||
| Constraint | Reason | Workaround |
|
||||
|------------|--------|------------|
|
||||
| No Apache Arrow IPC | Browser-incompatible dependency | Use `jsontable` for tabular data |
|
||||
| WebSocket NATS only | Browser cannot use TCP directly | Use `ws://` or `wss://` broker URLs |
|
||||
| Fetch API for HTTP | Browser fetch() API only | Compatible with Plik and other HTTP servers |
|
||||
|
||||
### Payload Type Availability by Platform
|
||||
|
||||
| Payload Type | Julia | Node.js | Browser | Python | Dart | Rust | MicroPython |
|
||||
|--------------|-------|---------|---------|--------|------|------|-------------|
|
||||
| `text` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
|
||||
| `dictionary` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
|
||||
| `arrowtable` | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ |
|
||||
| `jsontable` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ⚠️ |
|
||||
| `image` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
|
||||
| `audio` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
|
||||
| `video` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
|
||||
| `binary` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
|
||||
|
||||
---
|
||||
|
||||
## Message Flow
|
||||
|
||||
### Sending Flow
|
||||
@@ -686,23 +978,23 @@ flowchart TD
|
||||
|
||||
### Envelope Validation
|
||||
|
||||
| Rule | Condition | Error Code |
|
||||
|------|-----------|------------|
|
||||
| Required fields present | `correlation_id`, `msg_id`, `timestamp`, `send_to`, `payloads` | `INVALID_ENVELOPE` |
|
||||
| Valid UUID format | `correlation_id`, `msg_id`, `sender_id`, `receiver_id` | `INVALID_ENVELOPE` |
|
||||
| Valid timestamp format | ISO 8601 UTC | `INVALID_ENVELOPE` |
|
||||
| Non-empty payloads array | `length(payloads) > 0` | `INVALID_ENVELOPE` |
|
||||
| Rule | Condition | Error Code | Requirement ID |
|
||||
|------|-----------|------------|----------------|
|
||||
| Required fields present | `correlation_id`, `msg_id`, `timestamp`, `send_to`, `payloads` | `INVALID_ENVELOPE` | FR-012, FR-013 |
|
||||
| Valid UUID format | `correlation_id`, `msg_id`, `sender_id`, `receiver_id` | `INVALID_ENVELOPE` | FR-011, FR-012, NFR-401 |
|
||||
| Valid timestamp format | ISO 8601 UTC | `INVALID_ENVELOPE` | FR-012, NFR-401 |
|
||||
| Non-empty payloads array | `length(payloads) > 0` | `INVALID_ENVELOPE` | FR-012, FR-013 |
|
||||
|
||||
### Payload Validation
|
||||
|
||||
| Rule | Condition | Error Code |
|
||||
|------|-----------|------------|
|
||||
| Valid payload_type | Must be in `payload_type` enum | `INVALID_PAYLOAD_TYPE` |
|
||||
| Valid transport | Must be `direct` or `link` | `INVALID_TRANSPORT` |
|
||||
| Valid encoding | Must match payload_type and transport | `INVALID_TRANSPORT` |
|
||||
| Positive size | `size > 0` | `INVALID_PAYLOAD` |
|
||||
| Valid Base64 for direct | `data` matches Base64 pattern | `DESERIALIZATION_ERROR` |
|
||||
| Valid URL for link | `data` matches HTTP(S) URL pattern | `DOWNLOAD_FAILED` |
|
||||
| Rule | Condition | Error Code | Requirement ID |
|
||||
|------|-----------|------------|----------------|
|
||||
| Valid payload_type | Must be in `payload_type` enum | `INVALID_PAYLOAD_TYPE` | FR-001, FR-002, FR-003, FR-006 |
|
||||
| Valid transport | Must be `direct` or `link` | `INVALID_TRANSPORT` | FR-003, FR-004, FR-006 |
|
||||
| Valid encoding | Must match payload_type and transport | `INVALID_TRANSPORT` | FR-001, FR-002, FR-003, FR-012 |
|
||||
| Positive size | `size > 0` | `INVALID_PAYLOAD` | FR-003, FR-004, NFR-104, NFR-105 |
|
||||
| Valid Base64 for direct | `data` matches Base64 pattern | `DESERIALIZATION_ERROR` | FR-001, FR-002, FR-003, FR-012 |
|
||||
| Valid URL for link | `data` matches HTTP(S) URL pattern | `DOWNLOAD_FAILED` | FR-008, FR-009, FR-010 |
|
||||
|
||||
---
|
||||
|
||||
@@ -710,14 +1002,14 @@ flowchart TD
|
||||
|
||||
### Unit Test Validation
|
||||
|
||||
| Test | Input | Expected Output | Notes |
|
||||
|------|-------|-----------------|-------|
|
||||
| Text round-trip | `("msg", "Hello", "text")` | `("msg", "Hello", "text")` | String serialization |
|
||||
| Dictionary round-trip | `("data", {"key": "value"}, "dictionary")` | `("data", {"key": "value"}, "dictionary")` | JSON object round-trip |
|
||||
| Arrow table round-trip | `("table", arrow_table_data, "arrowtable")` | `("table", arrow_table_data, "arrowtable")` | Arrow IPC round-trip |
|
||||
| JSON table round-trip | `("table", [{"a":1},{"b":2}], "jsontable")` | `("table", [{"a":1},{"b":2}], "jsontable")` | JSON array of objects |
|
||||
| Mixed payloads | `[("msg", "Hello", "text"), ("imgname", bytes, "binary")]` | `[("msg", "Hello", "text"), ("imgname", bytes, "binary")]` | Multiple payload types |
|
||||
| Large payload | `("data", rand(10_000_000), "arrowtable")` | `("data", URL, "arrowtable")` with link transport | File server upload |
|
||||
| Test | Input | Expected Output | Notes | Requirement ID |
|
||||
|------|-------|-----------------|-------|----------------|
|
||||
| Text round-trip | `("msg", "Hello", "text")` | `("msg", "Hello", "text")` | String serialization | FR-001, FR-012, NFR-101, NFR-102 |
|
||||
| Dictionary round-trip | `("data", {"key": "value"}, "dictionary")` | `("data", {"key": "value"}, "dictionary")` | JSON object round-trip | FR-002, FR-012, NFR-101, NFR-102 |
|
||||
| Arrow table round-trip | `("table", arrow_table_data, "arrowtable")` | `("table", arrow_table_data, "arrowtable")` | Arrow IPC round-trip | FR-002, FR-012, NFR-101, NFR-102 |
|
||||
| JSON table round-trip | `("table", [{"a":1},{"b":2}], "jsontable")` | `("table", [{"a":1},{"b":2}], "jsontable")` | JSON array of objects | FR-001, FR-002, FR-006, FR-012 |
|
||||
| Mixed payloads | `[("msg", "Hello", "text"), ("imgname", bytes, "binary")]` | `[("msg", "Hello", "text"), ("imgname", bytes, "binary")]` | Multiple payload types | FR-006, FR-007 |
|
||||
| Large payload | `("data", rand(10_000_000), "arrowtable")` | `("data", URL, "arrowtable")` with link transport | File server upload | FR-003, FR-004, FR-008, FR-009, NFR-104, NFR-105 |
|
||||
|
||||
**Platform-Specific Notes:**
|
||||
- **Julia**: Use `Dict`, `Vector{Dict}`, or convert `DataFrame` to dictionary for testing
|
||||
@@ -727,24 +1019,24 @@ flowchart TD
|
||||
|
||||
### Integration Test Scenarios
|
||||
|
||||
| Scenario | Platforms | Payloads | Size Mix | Transport | Expected Result |
|
||||
|----------|-----------|----------|----------|-----------|-----------------|
|
||||
| Single text (small) | All | `text` | Small | direct | Round-trip successful |
|
||||
| Single dictionary (small) | All | `dictionary` | Small | direct | Round-trip successful |
|
||||
| Single arrow table (small) | Julia/JS/Python | `arrowtable` | Small | direct | Arrow IPC round-trip |
|
||||
| Single JSON table (small) | All | `jsontable` | Small | direct | Dictionary array round-trip |
|
||||
| Single image (small) | All | `image` | Small | direct | Binary round-trip |
|
||||
| Single audio (small) | All | `audio` | Small | direct | Binary round-trip |
|
||||
| Single video (small) | All | `video` | Small | direct | Binary round-trip |
|
||||
| Single binary (small) | All | `binary` | Small | direct | Binary round-trip |
|
||||
| Single text (large) | All | `text` | Large | link | File server upload/download |
|
||||
| Single JSON table (large) | All | `jsontable` | Large | link | File server upload/download |
|
||||
| Single image (large) | All | `image` | Large | link | File server upload/download |
|
||||
| **Ultimate Test** | Julia/JS/Python | `text` (small) + `dictionary` (small) + `arrowtable` (small) + `jsontable` (small) + `image` (small) + `audio` (small) + `video` (small) + `binary` (small) + `text` (large) + `dictionary` (large) + `arrowtable` (large) + `jsontable` (large) + `image` (large) | Mixed | direct/link | All payloads preserved with correct transport |
|
||||
| **Ultimate Test** | MicroPython | `text` (small) + `dictionary` (small) + `text` (large) + `dictionary` (large) | Mixed | direct | Limited to text/dictionary with direct transport only |
|
||||
| Cross-platform JSON table | All | `jsontable` | Small | direct | Dictionary array round-trip |
|
||||
| MicroPython ↔ Desktop | MicroPython ↔ Desktop | `text`/`dictionary` | Small | direct | Limited payload types |
|
||||
| Desktop ↔ Desktop (all combos) | Julia↔JS↔Python | All types | Small/Large | direct/link | Full compatibility |
|
||||
| Scenario | Platforms | Payloads | Size Mix | Transport | Expected Result | Requirement ID |
|
||||
|----------|-----------|----------|----------|-----------|-----------------|----------------|
|
||||
| Single text (small) | All | `text` | Small | direct | Round-trip successful | FR-001, FR-012, NFR-101, NFR-102 |
|
||||
| Single dictionary (small) | All | `dictionary` | Small | direct | Round-trip successful | FR-002, FR-012, NFR-101, NFR-102 |
|
||||
| Single arrow table (small) | Julia/JS/Python | `arrowtable` | Small | direct | Arrow IPC round-trip | FR-002, FR-012, NFR-101, NFR-102 |
|
||||
| Single JSON table (small) | All | `jsontable` | Small | direct | Dictionary array round-trip | FR-001, FR-002, FR-006, FR-012 |
|
||||
| Single image (small) | All | `image` | Small | direct | Binary round-trip | FR-001, FR-006, FR-012 |
|
||||
| Single audio (small) | All | `audio` | Small | direct | Binary round-trip | FR-001, FR-006, FR-012 |
|
||||
| Single video (small) | All | `video` | Small | direct | Binary round-trip | FR-001, FR-006, FR-012 |
|
||||
| Single binary (small) | All | `binary` | Small | direct | Binary round-trip | FR-001, FR-006, FR-012 |
|
||||
| Single text (large) | All | `text` | Large | link | File server upload/download | FR-003, FR-004, FR-008, FR-009, NFR-104, NFR-105 |
|
||||
| Single JSON table (large) | All | `jsontable` | Large | link | File server upload/download | FR-003, FR-004, FR-008, FR-009, NFR-104, NFR-105 |
|
||||
| Single image (large) | All | `image` | Large | link | File server upload/download | FR-003, FR-004, FR-008, FR-009, NFR-104, NFR-105 |
|
||||
| **Ultimate Test** | Julia/JS/Python | `text` (small) + `dictionary` (small) + `arrowtable` (small) + `jsontable` (small) + `image` (small) + `audio` (small) + `video` (small) + `binary` (small) + `text` (large) + `dictionary` (large) + `arrowtable` (large) + `jsontable` (large) + `image` (large) | Mixed | direct/link | All payloads preserved with correct transport | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-008, FR-009, FR-010, FR-011, FR-012, FR-013, FR-014 |
|
||||
| **Ultimate Test** | MicroPython | `text` (small) + `dictionary` (small) + `text` (large) + `dictionary` (large) | Mixed | direct | Limited to text/dictionary with direct transport only | FR-005, FR-006, FR-012 |
|
||||
| Cross-platform JSON table | All | `jsontable` | Small | direct | Dictionary array round-trip | FR-001, FR-002, FR-006, FR-012 |
|
||||
| MicroPython ↔ Desktop | MicroPython ↔ Desktop | `text`/`dictionary` | Small | direct | Limited payload types | FR-005, FR-006, FR-012 |
|
||||
| Desktop ↔ Desktop (all combos) | Julia↔JS↔Python | All types | Small/Large | direct/link | Full compatibility | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-012, FR-013, FR-014 |
|
||||
|
||||
---
|
||||
|
||||
@@ -759,11 +1051,24 @@ flowchart TD
|
||||
| Julia | Arrow.jl | Latest | Arrow IPC support |
|
||||
| Julia | HTTP.jl | Latest | HTTP file server |
|
||||
| Julia | UUIDs.jl | Latest | UUID generation |
|
||||
| Node.js | nats | Latest | NATS client |
|
||||
| Node.js | nats | Latest | NATS client (TCP) |
|
||||
| Node.js | node-fetch | Latest | HTTP file server |
|
||||
| Browser | nats.ws | Latest | NATS client (WebSocket) |
|
||||
| Browser | nats | Latest | NATS client (for bundling) |
|
||||
| Python | nats-py | Latest | NATS client |
|
||||
| Python | aiohttp | Latest | HTTP file server |
|
||||
| Python | pyarrow | Latest | Arrow IPC support |
|
||||
| Dart | nats | Latest | NATS client |
|
||||
| Dart | http | Latest | HTTP file server |
|
||||
| Dart | uuid | Latest | UUID generation |
|
||||
| Dart | dart-arrow | Latest | Arrow IPC support (Desktop/Flutter) |
|
||||
| Rust | nats | Latest | NATS client |
|
||||
| Rust | serde | Latest | JSON serialization |
|
||||
| Rust | serde_json | Latest | JSON handling |
|
||||
| Rust | tokio | Latest | Async runtime |
|
||||
| Rust | reqwest | Latest | HTTP file server |
|
||||
| Rust | uuid | Latest | UUID generation |
|
||||
| Rust | arrow2 | Latest | Arrow IPC support |
|
||||
| MicroPython | builtin | N/A | Limited implementation |
|
||||
|
||||
### Optional Dependencies
|
||||
@@ -779,6 +1084,11 @@ flowchart TD
|
||||
|
||||
| Date | Version | Changes |
|
||||
|------|---------|---------|
|
||||
| 2026-03-15 | 1.1.0 | Browser connection management |
|
||||
| - | - | Added NATSClient class with keepAlive support |
|
||||
| - | - | Added NATSConnectionPool for connection reuse |
|
||||
| - | - | Added publishMessage function with closeConnection option |
|
||||
| - | - | Added nats.ws to browser dependencies |
|
||||
| 2026-03-13 | 1.0.0 | Initial specification |
|
||||
| - | - | Message envelope schema defined |
|
||||
| - | - | Payload schema with transport modes |
|
||||
@@ -791,11 +1101,74 @@ flowchart TD
|
||||
|
||||
## References
|
||||
|
||||
- [`docs/requirements.md`](./requirements.md) - Business requirements and user stories
|
||||
- [`docs/architecture.md`](./architecture.md) - System architecture diagrams
|
||||
- [`docs/implementation.md`](./implementation.md) - Implementation details
|
||||
- [`src/NATSBridge.jl`](../src/NATSBridge.jl) - Ground truth implementation
|
||||
- [`README.md`](../README.md) - Project overview
|
||||
### 20.1 Documentation Artifacts
|
||||
|
||||
| Document | Purpose | Requirements Traceability |
|
||||
|----------|---------|--------------------------|
|
||||
| [`docs/requirements.md`](./requirements.md) | Business requirements and user stories | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`docs/specification.md`](./specification.md) | Technical contract for NATSBridge | This document |
|
||||
| [`docs/ui-specification.md`](./ui-specification.md) | UI specification for client applications | UI components for data entry and display |
|
||||
| [`docs/walkthrough.md`](./walkthrough.md) | End-to-end system flow | Traceability from user journey to technical implementation |
|
||||
| [`docs/architecture.md`](./architecture.md) | System architecture diagrams | Component interaction and data flow |
|
||||
| [`docs/validation.md`](./validation.md) | CI/CD validation rules | Contract testing and spec compliance |
|
||||
| [`docs/runbook.md`](./runbook.md) | Operational runbook | Deployment, scaling, and troubleshooting |
|
||||
|
||||
### 20.2 Implementation Files
|
||||
|
||||
| File | Platform | Features | Requirements Traceability |
|
||||
|------|----------|----------|--------------------------|
|
||||
| [`src/NATSBridge.jl`](../src/NATSBridge.jl) | Julia | Full feature set, Arrow IPC, multiple dispatch | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`src/natsbridge_ssr.js`](../src/natsbridge_ssr.js) | Node.js | Arrow IPC, async/await | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`src/natsbridge_csr.js`](../src/natsbridge_csr.js) | Browser | JSON table only, WebSocket NATS | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`src/natsbridge.py`](../src/natsbridge.py) | Python | Arrow IPC, async/await | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`src/natsbridge.dart`](../src/natsbridge.dart) | Dart | Full feature set, Arrow IPC, async/await | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`src/natsbridge.rs`](../src/natsbridge.rs) | Rust | Full feature set, Arrow IPC, async/await, type-safe | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`src/natsbridge_mpy.py`](../src/natsbridge_mpy.py) | MicroPython | Limited to direct transport | FR-005, FR-006, FR-012 |
|
||||
|
||||
### 20.3 External Dependencies
|
||||
|
||||
| Platform | Package | Version | Purpose | Requirements Traceability |
|
||||
|----------|---------|---------|---------|--------------------------|
|
||||
| Julia | NATS.jl | Latest | NATS client | FR-013, FR-014, NFR-201 |
|
||||
| Julia | JSON.jl | Latest | JSON serialization | FR-012, NFR-101, NFR-102 |
|
||||
| Julia | Arrow.jl | Latest | Arrow IPC support | FR-002, FR-012 |
|
||||
| Julia | HTTP.jl | Latest | HTTP file server | FR-008, FR-009 |
|
||||
| Julia | UUIDs.jl | Latest | UUID generation | FR-011, NFR-401 |
|
||||
| Node.js | nats | Latest | NATS client (TCP) | FR-013, FR-014 |
|
||||
| Node.js | node-fetch | Latest | HTTP file server | FR-008, FR-009 |
|
||||
| Browser | nats.ws | Latest | NATS client (WebSocket) | FR-013, FR-014 |
|
||||
| Browser | nats | Latest | NATS client (for bundling) | FR-013, FR-014 |
|
||||
| Python | nats-py | Latest | NATS client | FR-013, FR-014 |
|
||||
| Python | aiohttp | Latest | HTTP file server | FR-008, FR-009 |
|
||||
| Python | pyarrow | Latest | Arrow IPC support | FR-002, FR-012 |
|
||||
| Dart | nats | Latest | NATS client | FR-013, FR-014 |
|
||||
| Dart | http | Latest | HTTP file server | FR-008, FR-009 |
|
||||
| Dart | uuid | Latest | UUID generation | FR-011, NFR-401 |
|
||||
| Dart | dart-arrow | Latest | Arrow IPC support | FR-002, FR-012 |
|
||||
| Rust | nats | Latest | NATS client | FR-013, FR-014 |
|
||||
| Rust | serde | Latest | JSON serialization | FR-012, NFR-101, NFR-102 |
|
||||
| Rust | serde_json | Latest | JSON handling | FR-012, NFR-101, NFR-102 |
|
||||
| Rust | tokio | Latest | Async runtime | FR-013, FR-014 |
|
||||
| Rust | reqwest | Latest | HTTP file server | FR-008, FR-009 |
|
||||
| Rust | uuid | Latest | UUID generation | FR-011, NFR-401 |
|
||||
| Rust | arrow2 | Latest | Arrow IPC support | FR-002, FR-012 |
|
||||
| MicroPython | builtin | N/A | Limited implementation | FR-005, FR-006 |
|
||||
|
||||
---
|
||||
|
||||
## 21. Change Log
|
||||
|
||||
| Date | Version | Changes | Requirement ID(s) |
|
||||
|------|---------|---------|-------------------|
|
||||
| 2026-05-13 | 1.2.0 | Aligned with ground truth implementation (src/NATSBridge.jl) | All |
|
||||
| - | - | Updated smartsend signatures: removed is_publish, nats_connection; added sender_name | FR-001 through FR-014 |
|
||||
| - | - | Updated smartreceive signatures: takes msg_json_str::String instead of msg | FR-001 through FR-014 |
|
||||
| - | - | Removed publishMessage function and NATSClient/NATSConnectionPool classes from browser section | FR-013, FR-014 |
|
||||
| - | - | Added plik_oneshot_upload(filepath) overload to file server interface | FR-008, FR-009 |
|
||||
| - | - | Fixed SIZE_THRESHOLD default to 500,000 bytes | FR-003, FR-004 |
|
||||
| 2026-03-23 | 1.1.0 | Updated to ASG Framework specification guidelines | All |
|
||||
| 2026-03-15 | 1.1.0 | Browser connection management | FR-001 through FR-014 |
|
||||
| 2026-03-13 | 1.0.0 | Initial specification | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
|
||||
---
|
||||
|
||||
741
docs/tutorial.md
741
docs/tutorial.md
@@ -1,741 +0,0 @@
|
||||
# Cross-Platform NATSBridge Tutorial
|
||||
|
||||
A step-by-step guide to get started with NATSBridge across **Julia**, **JavaScript**, and **Python/MicroPython**.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
1. [Overview](#overview)
|
||||
2. [Prerequisites](#prerequisites)
|
||||
3. [Installation](#installation)
|
||||
4. [Quick Start](#quick-start)
|
||||
5. [Basic Examples](#basic-examples)
|
||||
6. [Advanced Usage](#advanced-usage)
|
||||
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
NATSBridge enables seamless communication across platforms 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
|
||||
|
||||
### Cross-Platform API Parity
|
||||
|
||||
All three platforms use the same high-level API:
|
||||
|
||||
```
|
||||
# Input format
|
||||
smartsend(subject, [(dataname, data, type), ...], options)
|
||||
|
||||
# Output format
|
||||
(env, env_json_str) = smartsend(...)
|
||||
env = smartreceive(msg, options)
|
||||
```
|
||||
|
||||
**Important Platform Differences:**
|
||||
|
||||
1. **Encoding field:** Julia and JavaScript preserve the original serialization format in the encoding field (`"base64"`, `"json"`, or `"arrow-ipc"`), while Python and MicroPython always use `"base64"` for all direct transport payloads.
|
||||
|
||||
2. **Async vs Sync:** JavaScript and Python desktop use async/await, while MicroPython uses synchronous API.
|
||||
|
||||
### Supported Payload Types
|
||||
|
||||
| Type | Julia | JavaScript | Python | MicroPython |
|
||||
|------|-------|------------|--------|-------------|
|
||||
| `text` | `String` | `string` | `str` | `str` |
|
||||
| `dictionary` | `Dict` | `Object` | `dict` | `dict` |
|
||||
| `arrowtable` | `DataFrame` | `Array<Object>` | `pandas.DataFrame` | ❌ |
|
||||
| `jsontable` | `Vector{NamedTuple}` | `Array<Object>` | `list[dict]` | ❌ |
|
||||
| `table` | ❌ | ❌ | `pandas.DataFrame` | ❌ |
|
||||
| `image` | `Vector{UInt8}` | `Uint8Array` | `bytes` | `bytearray` |
|
||||
| `audio` | `Vector{UInt8}` | `Uint8Array` | `bytes` | `bytearray` |
|
||||
| `video` | `Vector{UInt8}` | `Uint8Array` | `bytes` | `bytearray` |
|
||||
| `binary` | `Vector{UInt8}` | `Uint8Array` | `bytes` | `bytearray` |
|
||||
|
||||
**Note on MicroPython:** MicroPython does not support table types (`arrowtable`, `jsontable`, or `table`) due to memory constraints. Use `dictionary` or `binary` instead.
|
||||
|
||||
---
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Before you begin, ensure you have:
|
||||
|
||||
1. **NATS Server** running (or accessible)
|
||||
2. **HTTP File Server** (optional, for large payloads > 1MB)
|
||||
3. **Platform-specific packages** installed
|
||||
|
||||
---
|
||||
|
||||
## Installation
|
||||
|
||||
### Julia
|
||||
|
||||
```julia
|
||||
using Pkg
|
||||
Pkg.add("NATS")
|
||||
Pkg.add("Arrow")
|
||||
Pkg.add("JSON3")
|
||||
Pkg.add("HTTP")
|
||||
Pkg.add("UUIDs")
|
||||
Pkg.add("Dates")
|
||||
```
|
||||
|
||||
### JavaScript (Node.js)
|
||||
|
||||
```bash
|
||||
npm install nats uuid apache-arrow node-fetch
|
||||
```
|
||||
|
||||
### JavaScript (Browser)
|
||||
|
||||
```html
|
||||
<script src="https://unpkg.com/nats-js/dist/bundle/nats.min.js"></script>
|
||||
<script src="https://unpkg.com/apache-arrow/arrow.min.js"></script>
|
||||
```
|
||||
|
||||
### Python (Desktop)
|
||||
|
||||
```bash
|
||||
pip install nats-py aiohttp pyarrow pandas
|
||||
```
|
||||
|
||||
### MicroPython
|
||||
|
||||
Uses built-in modules: `network`, `socket`, `time`, `json`, `base64`
|
||||
|
||||
---
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Step 1: Start NATS Server
|
||||
|
||||
```bash
|
||||
docker run -p 4222:4222 nats:latest
|
||||
```
|
||||
|
||||
### Step 2: Start HTTP File Server (Optional)
|
||||
|
||||
```bash
|
||||
mkdir -p /tmp/fileserver
|
||||
python3 -m http.server 8080 --directory /tmp/fileserver
|
||||
```
|
||||
|
||||
### Step 3: Send Your First Message
|
||||
|
||||
#### Julia
|
||||
|
||||
```julia
|
||||
using NATSBridge
|
||||
|
||||
# Send a text message
|
||||
data = [("message", "Hello World", "text")]
|
||||
env, env_json_str = smartsend("/chat/room1", data, broker_url="nats://localhost:4222")
|
||||
# env: msg_envelope_v1 struct 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 struct
|
||||
# env_json_str: JSON string for publishing to NATS
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
|
||||
```javascript
|
||||
const NATSBridge = require('./src/natsbridge.js');
|
||||
|
||||
// Send a text message
|
||||
const data = [["message", "Hello World", "text"]];
|
||||
const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
"/chat/room1",
|
||||
data,
|
||||
{ broker_url: "nats://localhost:4222" }
|
||||
);
|
||||
// env: Object with all metadata and payloads
|
||||
// env_json_str: JSON string for publishing
|
||||
console.log("Message sent!");
|
||||
|
||||
// Or use is_publish=false
|
||||
const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
"/chat/room1",
|
||||
data,
|
||||
{ broker_url: "nats://localhost:4222", is_publish: false }
|
||||
);
|
||||
```
|
||||
|
||||
#### Python
|
||||
|
||||
```python
|
||||
from natsbridge import smartsend
|
||||
|
||||
# Send a text message
|
||||
data = [("message", "Hello World", "text")]
|
||||
env, env_json_str = await smartsend(
|
||||
"/chat/room1",
|
||||
data,
|
||||
broker_url="nats://localhost:4222"
|
||||
)
|
||||
# env: Dict with all metadata and payloads
|
||||
# env_json_str: JSON string for publishing
|
||||
print("Message sent!")
|
||||
|
||||
# Or use is_publish=False
|
||||
env, env_json_str = await smartsend(
|
||||
"/chat/room1",
|
||||
data,
|
||||
broker_url="nats://localhost:4222",
|
||||
is_publish=False
|
||||
)
|
||||
# env: Dict with all metadata and payloads
|
||||
# env_json_str: JSON string for publishing to NATS
|
||||
```
|
||||
|
||||
#### MicroPython
|
||||
|
||||
```python
|
||||
from natsbridge_mpy import NATSBridge
|
||||
|
||||
bridge = NATSBridge()
|
||||
|
||||
# Send a text message (limited to small payloads)
|
||||
data = [("message", "Hello World", "text")]
|
||||
env, env_json_str = bridge.smartsend(
|
||||
"/chat/room1",
|
||||
data,
|
||||
size_threshold=100000 # Lower threshold for MicroPython
|
||||
)
|
||||
print("Message sent!")
|
||||
```
|
||||
|
||||
### Step 4: Receive Messages
|
||||
|
||||
#### Julia
|
||||
|
||||
```julia
|
||||
using NATSBridge
|
||||
|
||||
# Receive and process message
|
||||
env = smartreceive(msg; fileserver_download_handler=_fetch_with_backoff)
|
||||
# Returns: ::JSON.Object{String, Any} with "payloads" field containing Vector{Tuple{String, Any, String}}
|
||||
# Access payloads: for (dataname, data, type) in env["payloads"]
|
||||
for (dataname, data, type) in env["payloads"]
|
||||
println("Received $dataname: $data")
|
||||
end
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
|
||||
```javascript
|
||||
const NATSBridge = require('./src/natsbridge.js');
|
||||
|
||||
// Receive and process message
|
||||
const env = await NATSBridge.smartreceive(msg, {
|
||||
fileserver_download_handler: NATSBridge.fetchWithBackoff
|
||||
});
|
||||
// env.payloads = [[dataname, data, type], ...]
|
||||
for (const [dataname, data, type] of env.payloads) {
|
||||
console.log(`Received ${dataname}:`, data);
|
||||
}
|
||||
```
|
||||
|
||||
#### Python
|
||||
|
||||
```python
|
||||
from natsbridge import smartreceive, fetch_with_backoff
|
||||
|
||||
# Receive and process message
|
||||
env = await smartreceive(
|
||||
msg,
|
||||
fileserver_download_handler=fetch_with_backoff
|
||||
)
|
||||
# env["payloads"] = [(dataname, data, type), ...]
|
||||
for dataname, data, type_ in env["payloads"]:
|
||||
print(f"Received {dataname}: {data}")
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Basic Examples
|
||||
|
||||
### Example 1: Sending a Dictionary
|
||||
|
||||
#### Julia
|
||||
|
||||
```julia
|
||||
using NATSBridge
|
||||
|
||||
config = Dict(
|
||||
"wifi_ssid" => "MyNetwork",
|
||||
"wifi_password" => "password123",
|
||||
"update_interval" => 60
|
||||
)
|
||||
|
||||
data = [("config", config, "dictionary")]
|
||||
env, env_json_str = smartsend("/device/config", data, broker_url="nats://localhost:4222")
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
|
||||
```javascript
|
||||
const NATSBridge = require('./src/natsbridge.js');
|
||||
|
||||
const config = {
|
||||
wifi_ssid: "MyNetwork",
|
||||
wifi_password: "password123",
|
||||
update_interval: 60
|
||||
};
|
||||
|
||||
const data = [["config", config, "dictionary"]];
|
||||
const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
"/device/config",
|
||||
data,
|
||||
{ broker_url: "nats://localhost:4222" }
|
||||
);
|
||||
```
|
||||
|
||||
#### Python
|
||||
|
||||
```python
|
||||
from natsbridge import smartsend
|
||||
|
||||
config = {
|
||||
"wifi_ssid": "MyNetwork",
|
||||
"wifi_password": "password123",
|
||||
"update_interval": 60
|
||||
}
|
||||
|
||||
data = [("config", config, "dictionary")]
|
||||
env, env_json_str = await smartsend(
|
||||
"/device/config",
|
||||
data,
|
||||
broker_url="nats://localhost:4222"
|
||||
)
|
||||
```
|
||||
|
||||
#### MicroPython
|
||||
|
||||
```python
|
||||
from natsbridge_mpy import NATSBridge
|
||||
|
||||
bridge = NATSBridge()
|
||||
|
||||
config = {
|
||||
"wifi_ssid": "MyNetwork",
|
||||
"wifi_password": "password123",
|
||||
"update_interval": 60
|
||||
}
|
||||
|
||||
data = [("config", config, "dictionary")]
|
||||
env, env_json_str = bridge.smartsend(
|
||||
"/device/config",
|
||||
data,
|
||||
size_threshold=100000
|
||||
)
|
||||
```
|
||||
|
||||
### Example 2: Sending Binary Data (Image)
|
||||
|
||||
#### Julia
|
||||
|
||||
```julia
|
||||
using NATSBridge
|
||||
|
||||
# Read image file
|
||||
image_data = read("image.png")
|
||||
|
||||
data = [("user_image", image_data, "binary")]
|
||||
env, env_json_str = smartsend("/chat/image", data, broker_url="nats://localhost:4222")
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
|
||||
```javascript
|
||||
const NATSBridge = require('./src/natsbridge.js');
|
||||
const fs = require('fs');
|
||||
|
||||
// Read image file
|
||||
const image_data = fs.readFileSync('image.png');
|
||||
|
||||
const data = [["user_image", image_data, "binary"]];
|
||||
const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
"/chat/image",
|
||||
data,
|
||||
{ broker_url: "nats://localhost:4222" }
|
||||
);
|
||||
```
|
||||
|
||||
#### Python
|
||||
|
||||
```python
|
||||
from natsbridge import smartsend
|
||||
|
||||
# Read image file
|
||||
with open("image.png", "rb") as f:
|
||||
image_data = f.read()
|
||||
|
||||
data = [("user_image", image_data, "binary")]
|
||||
env, env_json_str = await smartsend(
|
||||
"/chat/image",
|
||||
data,
|
||||
broker_url="nats://localhost:4222"
|
||||
)
|
||||
```
|
||||
|
||||
#### MicroPython
|
||||
|
||||
```python
|
||||
from natsbridge_mpy import NATSBridge
|
||||
|
||||
bridge = NATSBridge()
|
||||
|
||||
# Read image file
|
||||
with open("image.png", "rb") as f:
|
||||
image_data = f.read()
|
||||
|
||||
data = [("user_image", image_data, "binary")]
|
||||
env, env_json_str = bridge.smartsend(
|
||||
"/chat/image",
|
||||
data,
|
||||
size_threshold=100000
|
||||
)
|
||||
```
|
||||
|
||||
### Example 3: Request-Response Pattern
|
||||
|
||||
#### Julia (Requester)
|
||||
|
||||
```julia
|
||||
using NATSBridge
|
||||
|
||||
# Send command with reply-to
|
||||
data = [("command", Dict("action" => "read_sensor"), "dictionary")]
|
||||
env, env_json_str = smartsend(
|
||||
"/device/command",
|
||||
data,
|
||||
broker_url="nats://localhost:4222",
|
||||
reply_to="/device/response",
|
||||
reply_to_msg_id="cmd-001"
|
||||
)
|
||||
```
|
||||
|
||||
#### JavaScript (Requester)
|
||||
|
||||
```javascript
|
||||
const NATSBridge = require('./src/natsbridge.js');
|
||||
|
||||
// Send command with reply-to
|
||||
const data = [["command", { action: "read_sensor" }, "dictionary"]];
|
||||
const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
"/device/command",
|
||||
data,
|
||||
{
|
||||
broker_url: "nats://localhost:4222",
|
||||
reply_to: "/device/response",
|
||||
reply_to_msg_id: "cmd-001"
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
#### Python (Requester)
|
||||
|
||||
```python
|
||||
from natsbridge import smartsend
|
||||
|
||||
# Send command with reply-to
|
||||
data = [("command", {"action": "read_sensor"}, "dictionary")]
|
||||
env, env_json_str = await smartsend(
|
||||
"/device/command",
|
||||
data,
|
||||
broker_url="nats://localhost:4222",
|
||||
reply_to="/device/response",
|
||||
reply_to_msg_id="cmd-001"
|
||||
)
|
||||
```
|
||||
|
||||
#### Julia (Responder)
|
||||
|
||||
```julia
|
||||
using NATSBridge, NATS
|
||||
|
||||
const SUBJECT = "/device/command"
|
||||
const NATS_URL = "nats://localhost:4222"
|
||||
|
||||
function test_responder()
|
||||
conn = NATS.connect(NATS_URL)
|
||||
NATS.subscribe(conn, SUBJECT) do msg
|
||||
env = smartreceive(msg, fileserver_download_handler=_fetch_with_backoff)
|
||||
|
||||
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)
|
||||
if !isempty(reply_to)
|
||||
smartsend(reply_to, [("data", response, "dictionary")])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
sleep(120)
|
||||
NATS.drain(conn)
|
||||
end
|
||||
|
||||
test_responder()
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Advanced Usage
|
||||
|
||||
### Example 4: Large Payloads (File Server)
|
||||
|
||||
For payloads larger than 1MB, NATSBridge automatically uses the file server:
|
||||
|
||||
#### Julia
|
||||
|
||||
```julia
|
||||
using NATSBridge
|
||||
|
||||
# Create large data (> 1MB)
|
||||
large_data = rand(UInt8, 2_000_000)
|
||||
|
||||
env, env_json_str = smartsend(
|
||||
"/data/large",
|
||||
[("large_file", large_data, "binary")],
|
||||
broker_url="nats://localhost:4222",
|
||||
fileserver_url="http://localhost:8080"
|
||||
)
|
||||
|
||||
println("File uploaded to: $(env.payloads[1].data)")
|
||||
# Note: For link transport, data field contains the URL string
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
|
||||
```javascript
|
||||
const NATSBridge = require('./src/natsbridge.js');
|
||||
|
||||
// Create large data (> 1MB)
|
||||
const large_data = Buffer.alloc(2_000_000);
|
||||
for (let i = 0; i < large_data.length; i++) {
|
||||
large_data[i] = Math.floor(Math.random() * 256);
|
||||
}
|
||||
|
||||
const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
"/data/large",
|
||||
[["large_file", large_data, "binary"]],
|
||||
{
|
||||
broker_url: "nats://localhost:4222",
|
||||
fileserver_url: "http://localhost:8080"
|
||||
}
|
||||
);
|
||||
|
||||
console.log("File uploaded to:", env.payloads[0].data);
|
||||
// Note: For link transport, data field contains the URL string
|
||||
```
|
||||
|
||||
#### Python
|
||||
|
||||
```python
|
||||
from natsbridge import smartsend
|
||||
|
||||
# Create large data (> 1MB)
|
||||
import os
|
||||
large_data = os.urandom(2_000_000)
|
||||
|
||||
env, env_json_str = await smartsend(
|
||||
"/data/large",
|
||||
[("large_file", large_data, "binary")],
|
||||
broker_url="nats://localhost:4222",
|
||||
fileserver_url="http://localhost:8080"
|
||||
)
|
||||
|
||||
print(f"File uploaded to: {env['payloads'][0]['data']}")
|
||||
# Note: For link transport, data field contains the URL string
|
||||
```
|
||||
|
||||
#### MicroPython
|
||||
|
||||
MicroPython enforces a hard limit of 50KB per payload:
|
||||
|
||||
```python
|
||||
from natsbridge_mpy import NATSBridge
|
||||
|
||||
bridge = NATSBridge()
|
||||
|
||||
# MicroPython has a hard limit of 50KB per payload
|
||||
# Use streaming or chunking for larger data
|
||||
small_data = bytes(1000) # 1KB
|
||||
|
||||
data = [("small_file", small_data, "binary")]
|
||||
env, env_json_str = bridge.smartsend(
|
||||
"/data/small",
|
||||
data,
|
||||
size_threshold=100000 # Enforced max: 50000 bytes
|
||||
)
|
||||
```
|
||||
|
||||
### Example 5: Mixed Content (Chat with Text + Image)
|
||||
|
||||
NATSBridge supports sending multiple payloads with different types in a single message:
|
||||
|
||||
#### Julia
|
||||
|
||||
```julia
|
||||
using NATSBridge
|
||||
|
||||
image_data = read("avatar.png")
|
||||
|
||||
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 NATSBridge = require('./src/natsbridge.js');
|
||||
const fs = require('fs');
|
||||
|
||||
const image_data = fs.readFileSync('avatar.png');
|
||||
|
||||
const data = [
|
||||
["message_text", "Hello with image!", "text"],
|
||||
["user_avatar", image_data, "image"]
|
||||
];
|
||||
|
||||
const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
"/chat/mixed",
|
||||
data,
|
||||
{ broker_url: "nats://localhost:4222" }
|
||||
);
|
||||
```
|
||||
|
||||
#### Python
|
||||
|
||||
```python
|
||||
from natsbridge import smartsend
|
||||
|
||||
with open("avatar.png", "rb") as f:
|
||||
image_data = f.read()
|
||||
|
||||
data = [
|
||||
("message_text", "Hello with image!", "text"),
|
||||
("user_avatar", image_data, "image")
|
||||
]
|
||||
|
||||
env, env_json_str = await smartsend(
|
||||
"/chat/mixed",
|
||||
data,
|
||||
broker_url="nats://localhost:4222"
|
||||
)
|
||||
# env: Dict with all metadata and payloads
|
||||
```
|
||||
|
||||
### Example 6: Table Data (Arrow IPC)
|
||||
|
||||
For tabular data, NATSBridge uses Apache Arrow IPC format:
|
||||
|
||||
#### Julia
|
||||
|
||||
```julia
|
||||
using NATSBridge
|
||||
using DataFrames
|
||||
|
||||
# Create DataFrame
|
||||
df = DataFrame(
|
||||
id = [1, 2, 3],
|
||||
name = ["Alice", "Bob", "Charlie"],
|
||||
score = [95, 88, 92]
|
||||
)
|
||||
|
||||
data = [("students", df, "arrowtable")]
|
||||
env, env_json_str = smartsend("/data/students", data, broker_url="nats://localhost:4222")
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
|
||||
```javascript
|
||||
const NATSBridge = require('./src/natsbridge.js');
|
||||
|
||||
// Create table data (array of objects)
|
||||
const table_data = [
|
||||
{ id: 1, name: "Alice", score: 95 },
|
||||
{ id: 2, name: "Bob", score: 88 },
|
||||
{ id: 3, name: "Charlie", score: 92 }
|
||||
];
|
||||
|
||||
const data = [["students", table_data, "arrowtable"]];
|
||||
const [env, env_json_str] = await NATSBridge.smartsend(
|
||||
"/data/students",
|
||||
data,
|
||||
{ broker_url: "nats://localhost:4222" }
|
||||
);
|
||||
```
|
||||
|
||||
#### Python
|
||||
|
||||
```python
|
||||
from natsbridge import smartsend
|
||||
import pandas as pd
|
||||
|
||||
# Create DataFrame
|
||||
df = pd.DataFrame({
|
||||
'id': [1, 2, 3],
|
||||
'name': ['Alice', 'Bob', 'Charlie'],
|
||||
'score': [95, 88, 92]
|
||||
})
|
||||
|
||||
data = [("students", df, "table")]
|
||||
env, env_json_str = await smartsend(
|
||||
"/data/students",
|
||||
data,
|
||||
broker_url="nats://localhost:4222"
|
||||
)
|
||||
```
|
||||
|
||||
#### MicroPython
|
||||
|
||||
MicroPython does not support table type due to memory constraints. Use dictionary or binary instead.
|
||||
|
||||
---
|
||||
|
||||
## Next Steps
|
||||
|
||||
1. **Explore the test directory** for more examples
|
||||
2. **Check the documentation** for advanced configuration options
|
||||
3. **Read the walkthrough** for building real-world applications
|
||||
|
||||
---
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Connection Issues
|
||||
|
||||
- Ensure NATS server is running: `docker ps | grep nats`
|
||||
- Check firewall settings
|
||||
- Verify NATS URL configuration
|
||||
|
||||
### File Server Issues
|
||||
|
||||
- Ensure file server is running and accessible
|
||||
- Check upload permissions
|
||||
- Verify file server URL configuration
|
||||
|
||||
### Serialization Errors
|
||||
|
||||
- Verify data type matches the specified type
|
||||
- Check that binary data is in the correct format
|
||||
- MicroPython: Ensure payload size < 50KB
|
||||
|
||||
---
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
@@ -1,23 +1,40 @@
|
||||
# Walkthrough: NATSBridge
|
||||
|
||||
**Version**: 1.0.0
|
||||
**Date**: 2026-03-13
|
||||
**Version**: 1.2.0
|
||||
**Date**: 2026-05-13
|
||||
**Status**: Active
|
||||
**Ground Truth**: [`src/NATSBridge.jl`](../src/NATSBridge.jl)
|
||||
|
||||
---
|
||||
|
||||
## Executive Summary
|
||||
## 1. Executive Summary
|
||||
|
||||
This document provides the **story of flow** for NATSBridge - the cross-platform bi-directional data bridge that enables seamless communication between **Julia**, **JavaScript**, **Python**, and **MicroPython** applications using NATS as the message bus.
|
||||
This document provides the **end-to-end trace** for NATSBridge - the cross-platform bi-directional data bridge that enables seamless communication between **Julia**, **JavaScript**, **Python**, **Dart**, **Rust**, and **MicroPython** applications using NATS as the message bus.
|
||||
|
||||
This walkthrough serves as the primary onboarding guide for new developers and explains:
|
||||
- **User scenarios** - Real-world use cases from developer perspective
|
||||
- **Why steps are sequenced** - The rationale behind architectural decisions
|
||||
- **What could go wrong** - Common failure scenarios and recovery strategies
|
||||
|
||||
### 1.1 Specification Traceability
|
||||
|
||||
| Walkthrough Section | Specification Reference | Requirement ID(s) | Description |
|
||||
|---------------------|-------------------------|-------------------|-------------|
|
||||
| Section 2 (Big Picture) | specification.md:2, specification.md:15 | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-012, FR-013, FR-014 | End-to-end system flow diagrams |
|
||||
| Section 3 (Chat Scenario) | specification.md:2, specification.md:3, specification.md:5, specification.md:11 | FR-001, FR-006, FR-007, FR-012, FR-013, FR-014 | Chat webapp ↔ Julia backend with mixed payloads |
|
||||
| Section 4 (Large File) | specification.md:6, specification.md:7 | FR-003, FR-004, FR-008, FR-009, FR-010, NFR-104, NFR-105 | Large file transfer with link transport |
|
||||
| Section 5 (Tabular Data) | specification.md:5, specification.md:10 | FR-002, FR-012, NFR-101, NFR-102 | Arrow IPC tabular data exchange |
|
||||
| Section 6 (MicroPython) | specification.md:13, specification.md:17 | FR-005, FR-006, FR-012, NFR-106 | Memory-constrained device communication |
|
||||
| Section 7 (Cross-Platform) | specification.md:3, specification.md:4, specification.md:5, specification.md:11 | FR-001, FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-012, FR-013, FR-014 | Multi-platform chat application |
|
||||
| Section 8 (Error Handling) | specification.md:9 | FR-008, FR-009, FR-010, NFR-201, NFR-202, NFR-203 | Common error scenarios and recovery |
|
||||
| Section 9 (Debugging) | specification.md:4, specification.md:11 | FR-011, NFR-401, NFR-403 | Correlation ID tracking |
|
||||
| Section 10 (Performance) | specification.md:7, specification.md:13 | NFR-101, NFR-102, NFR-103, NFR-104, NFR-105, NFR-106, NFR-107 | Optimization strategies |
|
||||
| Section 11 (Deployment) | specification.md:12, specification.md:18 | FR-013, FR-014, NFR-201, NFR-203 | Infrastructure requirements |
|
||||
|
||||
---
|
||||
|
||||
## 2. Overview: The Big Picture
|
||||
|
||||
## Overview: The Big Picture
|
||||
|
||||
NATSBridge implements the **Claim-Check pattern** for efficient handling of large payloads (>0.5MB):
|
||||
@@ -196,23 +213,25 @@ NATSBridge builds the message envelope:
|
||||
- **reply_to**: Tells backend where to send response
|
||||
- **payloads array**: Contains all data with metadata for proper handling
|
||||
|
||||
#### Step 5: Publish to NATS
|
||||
#### Step 5: Publish to NATS (Caller's Responsibility)
|
||||
|
||||
```javascript
|
||||
await NATSBridge.NATSClient.connect("ws://localhost:4222");
|
||||
await NATSBridge.NATSClient.publish("/agent/wine/api/v1/prompt", msgJson);
|
||||
// NATS publishing is the caller's responsibility
|
||||
const conn = await NATS.connect({ servers: "ws://localhost:4222" });
|
||||
await conn.publish("/agent/wine/api/v1/prompt", msgJson);
|
||||
```
|
||||
|
||||
**Rationale**:
|
||||
- NATS provides low-latency message delivery
|
||||
- JSON format ensures cross-platform compatibility
|
||||
- `smartsend()` returns `(env, msgJson)` - caller handles publishing
|
||||
|
||||
#### Step 6: Julia Backend Receives Message
|
||||
|
||||
```julia
|
||||
# Julia backend
|
||||
msg = NATS.subscription.next() # Get message from NATS
|
||||
env = smartreceive(msg)
|
||||
nats_msg = NATS.subscription.next() # Get message from NATS
|
||||
env = smartreceive(String(nats_msg.payload))
|
||||
|
||||
# env["payloads"] is now:
|
||||
# [
|
||||
@@ -338,8 +357,8 @@ const response = await plikOneshotUpload(
|
||||
|
||||
```julia
|
||||
# Julia backend
|
||||
msg = NATS.subscription.next()
|
||||
env = smartreceive(msg)
|
||||
nats_msg = NATS.subscription.next()
|
||||
env = smartreceive(String(nats_msg.payload))
|
||||
|
||||
# NATSBridge automatically:
|
||||
# 1. Extracts URL from payload
|
||||
@@ -411,8 +430,8 @@ arrow_bytes = buf.getvalue()
|
||||
|
||||
```julia
|
||||
# Julia backend
|
||||
msg = NATS.subscription.next()
|
||||
env = smartreceive(msg)
|
||||
nats_msg = NATS.subscription.next()
|
||||
env = smartreceive(String(nats_msg.payload))
|
||||
|
||||
# env["payloads"][1] is now:
|
||||
# ("data", DataFrame with id, name, score columns, "arrowtable")
|
||||
@@ -444,7 +463,155 @@ env, msg_json = smartsend(
|
||||
|
||||
---
|
||||
|
||||
## User Scenario 4: MicroPython Device
|
||||
## User Scenario 4: Rust Service with Type-Safe API
|
||||
|
||||
### Scenario Description
|
||||
|
||||
A Rust service needs to process messages from a Julia analytics pipeline and send typed results back. The Rust implementation leverages compile-time type safety via Rust enums and serde for serialization.
|
||||
|
||||
### Step-by-Step Flow
|
||||
|
||||
#### Step 1: Rust Service Receives Message
|
||||
|
||||
```rust
|
||||
// Rust service - using tokio async runtime
|
||||
use natsbridge::{smartreceive, MsgEnvelopeV1};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let conn = nats::connect("nats://localhost:4222").unwrap();
|
||||
|
||||
// Subscribe and receive messages
|
||||
let mut sub = conn.subscribe("/agent/wine/api/v1/analyze").unwrap();
|
||||
|
||||
for msg in sub.messages() {
|
||||
let envelope: MsgEnvelopeV1 = smartreceive(
|
||||
&String::from_utf8_lossy(&msg.payload),
|
||||
&Default::default(),
|
||||
).await.unwrap();
|
||||
|
||||
// Type-safe payload access
|
||||
for payload in &envelope.payloads {
|
||||
match &payload.data {
|
||||
Payload::ArrowTable(arrow_bytes) => {
|
||||
// Process Arrow IPC data using arrow2
|
||||
let table = arrow2::io::ipc::read::Reader::new(
|
||||
std::io::Cursor::new(arrow_bytes.clone()),
|
||||
);
|
||||
println!("Received {} rows", table.len());
|
||||
},
|
||||
Payload::Text(text) => {
|
||||
println!("Message: {}", text);
|
||||
},
|
||||
_ => println!("Received {} bytes of {} data",
|
||||
match &payload.data {
|
||||
Payload::Binary(b) => b.len(),
|
||||
_ => 0,
|
||||
},
|
||||
payload.payload_type),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Rationale**:
|
||||
- **Type-safe payloads**: Rust enum discriminates between payload types at compile time
|
||||
- **serde serialization**: Automatic JSON deserialization to `MsgEnvelopeV1`
|
||||
- **tokio runtime**: Efficient async I/O for NATS and HTTP operations
|
||||
- **arrow2 integration**: Direct Arrow IPC deserialization without intermediate format
|
||||
|
||||
#### Step 2: Rust Service Sends Processed Results
|
||||
|
||||
```rust
|
||||
// Rust service sends results back with mixed payload types
|
||||
use natsbridge::{smartsend, Payload, SmartsendOptions};
|
||||
|
||||
let results_df = /* processed Arrow table */;
|
||||
let result_bytes = /* serialize to Arrow IPC */;
|
||||
|
||||
let (envelope, json_str) = smartsend(
|
||||
"/agent/wine/api/v1/results",
|
||||
&[
|
||||
(
|
||||
"results".to_string(),
|
||||
Payload::ArrowTable(result_bytes),
|
||||
"arrowtable".to_string(),
|
||||
),
|
||||
(
|
||||
"summary".to_string(),
|
||||
Payload::Text("Analysis complete: 1500 rows processed".to_string()),
|
||||
"text".to_string(),
|
||||
),
|
||||
],
|
||||
&SmartsendOptions {
|
||||
broker_url: "nats://localhost:4222".to_string(),
|
||||
reply_to: "/python/worker/v1/results".to_string(),
|
||||
msg_purpose: "chat".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
).await?;
|
||||
|
||||
// Caller publishes to NATS
|
||||
conn.publish("/agent/wine/api/v1/results", &json_str)?;
|
||||
```
|
||||
|
||||
**Rationale**:
|
||||
- **Builder pattern**: `SmartsendOptions` provides clean configuration
|
||||
- **Enum-based payloads**: Type safety prevents sending incorrect data types
|
||||
- **Default options**: sensible defaults reduce boilerplate
|
||||
- **Result<T, E>**: idiomatic Rust error handling
|
||||
|
||||
#### Step 3: Python/Julia Receives Rust Response
|
||||
|
||||
```python
|
||||
# Python backend receives Rust response
|
||||
env = await smartreceive(str(nats_msg.payload))
|
||||
|
||||
# env["payloads"][0] is now:
|
||||
# ("results", arrow_table_data, "arrowtable")
|
||||
# env["payloads"][1] is now:
|
||||
# ("summary", "Analysis complete: 1500 rows processed", "text")
|
||||
```
|
||||
|
||||
**Rationale**:
|
||||
- **Cross-platform parity**: Rust envelope matches other platform envelopes exactly
|
||||
- **Same JSON wire format**: No protocol translation needed
|
||||
- **Type preservation**: Arrow IPC and text types preserved across all platforms
|
||||
|
||||
#### Step 4: Large File Transfer from Rust
|
||||
|
||||
```rust
|
||||
// Rust service sends large binary file via link transport
|
||||
let large_file_data: Vec<u8> = std::fs::read("/data/large_dataset.parquet")?;
|
||||
|
||||
let (envelope, json_str) = smartsend(
|
||||
"/agent/wine/api/v1/upload",
|
||||
&[
|
||||
(
|
||||
"dataset".to_string(),
|
||||
Payload::Binary(large_file_data),
|
||||
"binary".to_string(),
|
||||
),
|
||||
],
|
||||
&SmartsendOptions {
|
||||
broker_url: "nats://localhost:4222".to_string(),
|
||||
fileserver_url: "http://localhost:8080".to_string(),
|
||||
size_threshold: 500_000, // 0.5MB triggers link transport
|
||||
..Default::default()
|
||||
},
|
||||
).await?;
|
||||
```
|
||||
|
||||
**Rationale**:
|
||||
- **Automatic transport selection**: Same 0.5MB threshold as other desktop platforms
|
||||
- **reqwest integration**: Efficient HTTP client for file server upload/download
|
||||
- **Exponential backoff**: Built-in retry with configurable parameters
|
||||
- **Zero-copy where possible**: `Vec<u8>` passed directly without intermediate copies
|
||||
|
||||
---
|
||||
|
||||
## User Scenario 5: MicroPython Device
|
||||
|
||||
### Scenario Description
|
||||
|
||||
@@ -495,8 +662,8 @@ payload_b64 = base64.b64encode(json_bytes).decode('ascii')
|
||||
|
||||
```python
|
||||
# Python backend
|
||||
msg = await nats_consumer.next()
|
||||
env = await smartreceive(msg)
|
||||
nats_msg = await nats_consumer.next()
|
||||
env = await smartreceive(str(nats_msg.payload))
|
||||
|
||||
# env["payloads"][0] is now:
|
||||
# ("data", {"temperature": 25.5, "humidity": 60.0, ...}, "dictionary")
|
||||
@@ -509,7 +676,7 @@ env = await smartreceive(msg)
|
||||
|
||||
---
|
||||
|
||||
## User Scenario 5: Cross-Platform Chat with Mixed Payloads
|
||||
## User Scenario 6: Cross-Platform Chat with Mixed Payloads
|
||||
|
||||
### Scenario Description
|
||||
|
||||
@@ -544,8 +711,8 @@ const [env, msgJson] = await NATSBridge.smartsend(
|
||||
|
||||
```python
|
||||
# Python (Backend)
|
||||
msg = await nats_consumer.next()
|
||||
env = await smartreceive(msg)
|
||||
nats_msg = await nats_consumer.next()
|
||||
env = await smartreceive(str(nats_msg.payload))
|
||||
|
||||
# env["payloads"] is now:
|
||||
# [
|
||||
@@ -563,8 +730,8 @@ env = await smartreceive(msg)
|
||||
|
||||
```julia
|
||||
# Julia (Backend)
|
||||
msg = NATS.subscription.next()
|
||||
env = smartreceive(msg)
|
||||
nats_msg = NATS.subscription.next()
|
||||
env = smartreceive(String(nats_msg.payload))
|
||||
|
||||
# env["payloads"] is now:
|
||||
# [
|
||||
@@ -684,7 +851,10 @@ log_trace(correlation_id, "Published to NATS")
|
||||
|
||||
| Platform | Threshold | Notes |
|
||||
|----------|-----------|-------|
|
||||
| Desktop (Julia/JS/Python) | 500,000 bytes (0.5MB) | Default threshold |
|
||||
| Desktop (Julia/JS/Python/Dart) | 500,000 bytes (0.5MB) | Default threshold |
|
||||
| Dart Desktop | 500,000 bytes (0.5MB) | Default threshold |
|
||||
| Dart Flutter | 500,000 bytes (0.5MB) | Default threshold |
|
||||
| Dart Web | 500,000 bytes (0.5MB) | Default threshold |
|
||||
| MicroPython | 100,000 bytes (100KB) | Lower threshold for memory constraints |
|
||||
|
||||
---
|
||||
@@ -697,7 +867,7 @@ log_trace(correlation_id, "Published to NATS")
|
||||
|-----------|---------|-------|
|
||||
| NATS Server | 1 instance | Single node for development |
|
||||
| File Server | 1 instance | HTTP server for large payloads |
|
||||
| Client Memory | 50MB | Desktop platforms |
|
||||
| Client Memory | 50MB | Desktop platforms (Julia/JS/Python/Dart) |
|
||||
| Client Memory | 256KB | MicroPython devices |
|
||||
|
||||
### Environment Variables
|
||||
@@ -706,7 +876,7 @@ log_trace(correlation_id, "Published to NATS")
|
||||
|----------|---------|-------------|
|
||||
| `NATS_URL` | `nats://localhost:4222` | NATS server URL |
|
||||
| `FILESERVER_URL` | `http://localhost:8080` | HTTP file server URL |
|
||||
| `SIZE_THRESHOLD` | `1000000` | Size threshold in bytes |
|
||||
| `SIZE_THRESHOLD` | `500000` | Size threshold in bytes (0.5MB) |
|
||||
|
||||
---
|
||||
|
||||
@@ -718,13 +888,62 @@ log_trace(correlation_id, "Published to NATS")
|
||||
|
||||
---
|
||||
|
||||
## References
|
||||
## 12. References
|
||||
|
||||
- [`docs/requirements.md`](./requirements.md) - Business requirements and user stories
|
||||
- [`docs/spec.md`](./spec.md) - Technical specification and contracts
|
||||
- [`docs/architecture.md`](./architecture.md) - System architecture diagrams
|
||||
- [`src/NATSBridge.jl`](../src/NATSBridge.jl) - Ground truth implementation
|
||||
- [`README.md`](../README.md) - Project overview
|
||||
### 12.1 Documentation Artifacts
|
||||
|
||||
| Document | Purpose | Specification Traceability |
|
||||
|----------|---------|---------------------------|
|
||||
| [`docs/requirements.md`](./requirements.md) | Business requirements and user stories | FR-001 through FR-014, NFR-101 through NFR-405 |
|
||||
| [`docs/specification.md`](./specification.md) | Technical contract for NATSBridge | specification.md:2-19 (all sections) |
|
||||
| [`docs/ui-specification.md`](./ui-specification.md) | UI specification for client applications | UI components for data entry and display |
|
||||
| [`docs/walkthrough.md`](./walkthrough.md) | End-to-end system flow | This document |
|
||||
| [`docs/architecture.md`](./architecture.md) | System architecture diagrams | Component interaction and data flow |
|
||||
| [`docs/validation.md`](./validation.md) | CI/CD validation rules | Contract testing and spec compliance |
|
||||
| [`docs/runbook.md`](./runbook.md) | Operational runbook | Deployment, scaling, and troubleshooting |
|
||||
|
||||
### 12.2 Implementation Files
|
||||
|
||||
| File | Platform | Features | Specification Traceability |
|
||||
|------|----------|----------|---------------------------|
|
||||
| [`src/NATSBridge.jl`](../src/NATSBridge.jl) | Julia | Full feature set, Arrow IPC, multiple dispatch | specification.md:2-19 (all sections) |
|
||||
| [`src/natsbridge_ssr.js`](../src/natsbridge_ssr.js) | Node.js | Arrow IPC, async/await | specification.md:2-19 (all sections) |
|
||||
| [`src/natsbridge_csr.js`](../src/natsbridge_csr.js) | Browser | JSON table only, WebSocket NATS | specification.md:2-19 (all sections) |
|
||||
| [`src/natsbridge.py`](../src/natsbridge.py) | Python | Arrow IPC, async/await | specification.md:2-19 (all sections) |
|
||||
| [`src/natsbridge.dart`](../src/natsbridge.dart) | Dart | Full feature set, Arrow IPC, async/await | specification.md:2-19 (all sections) |
|
||||
| [`src/natsbridge.rs`](../src/natsbridge.rs) | Rust | Full feature set, Arrow IPC, async/await, type-safe | specification.md:2-19 (all sections) |
|
||||
| [`src/natsbridge_mpy.py`](../src/natsbridge_mpy.py) | MicroPython | Limited to direct transport | specification.md:2-19 (all sections) |
|
||||
|
||||
---
|
||||
|
||||
## 13. Change Log
|
||||
|
||||
| Date | Version | Changes | Specification Reference |
|
||||
|------|---------|---------|------------------------|
|
||||
| 2026-05-13 | 1.3.0 | Added Rust support with tokio, serde, and arrow2 | All sections |
|
||||
| - | - | Added Rust user scenario (User Scenario 4) | specification.md:11 (Rust API) |
|
||||
| - | - | Updated scenario numbering (MicroPython → Scenario 5, Cross-Platform → Scenario 6) | All sections |
|
||||
| 2026-05-13 | 1.2.0 | Aligned with ground truth implementation (src/NATSBridge.jl) | All sections |
|
||||
| - | - | Updated smartreceive calls to use String(nats_msg.payload) pattern | All sections |
|
||||
| - | - | Removed NATSClient.publish() calls (caller responsible for NATS publishing) | All sections |
|
||||
| - | - | Removed is_publish and nats_connection parameter references | All sections |
|
||||
| 2026-03-23 | 1.0.0 | Updated to ASG Framework walkthrough guidelines | All sections |
|
||||
| 2026-03-13 | 1.0.0 | Initial walkthrough documentation | specification.md:2-19 (all sections) |
|
||||
|
||||
---
|
||||
|
||||
## 14. Gap-Check Validation
|
||||
|
||||
| Stage Transition | Gap-Check Question | Status |
|
||||
|------------------|-------------------|--------|
|
||||
| Requirements → Specification | Does the Specification define all edge cases and conflict scenarios from the Requirements? | ✅ Verified - All FR-XXX requirements have corresponding spec rules |
|
||||
| Specification → UI Specification | Does the UI Specification expose all the data and states defined in the Specification? | ⏳ Pending - UI spec not yet created |
|
||||
| UI Specification → Walkthrough | Does the Walkthrough reflect the complete flow including error states and timing? | ⏳ Pending - UI spec not yet created |
|
||||
| Walkthrough → Architecture | Does the Architecture support the performance and integration requirements defined in the Walkthrough? | ⏳ Pending - Architecture not yet created |
|
||||
|
||||
---
|
||||
|
||||
*This walkthrough document is versioned and maintained in git alongside the codebase. All implementations must adhere to this documentation.*
|
||||
|
||||
---
|
||||
|
||||
|
||||
96
examples/smartreceive_example.rs
Normal file
96
examples/smartreceive_example.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
use natsbridge::{smartreceive, SmartreceiveOptions};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Simulated NATS message JSON (received from NATS subscription)
|
||||
let msg_json_str = r#"{
|
||||
"correlation_id": "abc123-def456-ghi789",
|
||||
"msg_id": "msg-uuid-001",
|
||||
"timestamp": "2026-05-13T12:00:00.000Z",
|
||||
"send_to": "/agent/wine/api/v1/prompt",
|
||||
"msg_purpose": "chat",
|
||||
"sender_name": "js-webapp",
|
||||
"sender_id": "sender-uuid-001",
|
||||
"receiver_name": "rust-backend",
|
||||
"receiver_id": "",
|
||||
"reply_to": "/agent/wine/api/v1/response",
|
||||
"reply_to_msg_id": "",
|
||||
"broker_url": "nats://localhost:4222",
|
||||
"metadata": {},
|
||||
"payloads": [
|
||||
{
|
||||
"id": "payload-uuid-001",
|
||||
"dataname": "message",
|
||||
"payload_type": "text",
|
||||
"transport": "direct",
|
||||
"encoding": "base64",
|
||||
"size": 29,
|
||||
"data": "SGVsbG8gZnJvbSBKYXZhU2NyaXB0ISE=",
|
||||
"metadata": {"payload_bytes": 29}
|
||||
},
|
||||
{
|
||||
"id": "payload-uuid-002",
|
||||
"dataname": "user_data",
|
||||
"payload_type": "dictionary",
|
||||
"transport": "direct",
|
||||
"encoding": "json",
|
||||
"size": 58,
|
||||
"data": "eyJ0eXBlIjoiY2hhdCIsInNlbmRlciI6InNlcnZpY2VBIiwicmVjZWl2ZXIiOiJzZXJ2aWNlQiJ9",
|
||||
"metadata": {"payload_bytes": 58}
|
||||
}
|
||||
]
|
||||
}"#;
|
||||
|
||||
let options = SmartreceiveOptions::default();
|
||||
|
||||
match smartreceive(msg_json_str, &options).await {
|
||||
Ok(envelope) => {
|
||||
println!("=== Envelope Received ===");
|
||||
println!("Correlation ID: {}", envelope.correlation_id);
|
||||
println!("Message ID: {}", envelope.msg_id);
|
||||
println!("Subject: {}", envelope.send_to);
|
||||
println!("Purpose: {}", envelope.msg_purpose);
|
||||
println!("Sender: {}", envelope.sender_name);
|
||||
println!("Receiver: {}", envelope.receiver_name);
|
||||
println!("Payloads: {}", envelope.payloads.len());
|
||||
println!();
|
||||
|
||||
for payload in &envelope.payloads {
|
||||
println!("--- Payload: {} ---", payload.dataname);
|
||||
println!(" Type: {}", payload.payload_type);
|
||||
println!(" Transport: {}", payload.transport);
|
||||
println!(" Encoding: {}", payload.encoding);
|
||||
println!(" Size: {} bytes", payload.size);
|
||||
|
||||
// In a real scenario, you would deserialize payload.data here
|
||||
// based on payload_type to get the actual data
|
||||
match payload.payload_type.as_str() {
|
||||
"text" => {
|
||||
// For demonstration, decode the base64
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
|
||||
if payload.transport == "direct" {
|
||||
let decoded = BASE64.decode(&payload.data).unwrap();
|
||||
println!(" Data: {}", String::from_utf8_lossy(&decoded));
|
||||
} else {
|
||||
println!(" URL: {}", payload.data);
|
||||
}
|
||||
}
|
||||
"dictionary" => {
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
|
||||
if payload.transport == "direct" {
|
||||
let decoded = BASE64.decode(&payload.data).unwrap();
|
||||
let json: serde_json::Value = serde_json::from_slice(&decoded).unwrap();
|
||||
println!(" Data: {}", serde_json::to_string_pretty(&json).unwrap());
|
||||
}
|
||||
}
|
||||
other => {
|
||||
println!(" Data type: {}", other);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
70
examples/smartsend_example.rs
Normal file
70
examples/smartsend_example.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
use natsbridge::{smartsend, Payload, SmartsendOptions};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Create mixed payload data
|
||||
let payloads = vec![
|
||||
(
|
||||
"message".to_string(),
|
||||
Payload::Text("Hello from Rust!".to_string()),
|
||||
"text".to_string(),
|
||||
),
|
||||
(
|
||||
"user_data".to_string(),
|
||||
Payload::Dictionary(serde_json::json!({
|
||||
"name": "Alice",
|
||||
"role": "admin",
|
||||
"scores": [95, 88, 92]
|
||||
})),
|
||||
"dictionary".to_string(),
|
||||
),
|
||||
(
|
||||
"avatar".to_string(),
|
||||
Payload::Binary(vec![0x89, 0x50, 0x4E, 0x47]), // PNG header
|
||||
"image".to_string(),
|
||||
),
|
||||
];
|
||||
|
||||
let options = SmartsendOptions {
|
||||
broker_url: "nats://localhost:4222".to_string(),
|
||||
fileserver_url: "http://localhost:8080".to_string(),
|
||||
msg_purpose: "chat".to_string(),
|
||||
sender_name: "rust-example".to_string(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
match smartsend("/agent/wine/api/v1/prompt", &payloads, &options).await {
|
||||
Ok((envelope, json_str)) => {
|
||||
println!("=== Envelope Created ===");
|
||||
println!("Correlation ID: {}", envelope.correlation_id);
|
||||
println!("Message ID: {}", envelope.msg_id);
|
||||
println!("Timestamp: {}", envelope.timestamp);
|
||||
println!("Subject: {}", envelope.send_to);
|
||||
println!("Purpose: {}", envelope.msg_purpose);
|
||||
println!("Sender: {}", envelope.sender_name);
|
||||
println!("Payloads: {}", envelope.payloads.len());
|
||||
println!();
|
||||
|
||||
for payload in &envelope.payloads {
|
||||
println!("Payload: {} (type: {}, transport: {}, encoding: {})",
|
||||
payload.dataname,
|
||||
payload.payload_type,
|
||||
payload.transport,
|
||||
payload.encoding);
|
||||
println!(" Size: {} bytes", payload.size);
|
||||
println!(" Data: {}", if payload.transport == "direct" {
|
||||
&payload.data[..payload.data.len().min(40)]
|
||||
} else {
|
||||
&payload.data[..payload.data.len().min(60)]
|
||||
});
|
||||
}
|
||||
|
||||
println!();
|
||||
println!("=== JSON String for NATS Publishing ===");
|
||||
println!("{}", json_str);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@
|
||||
|
||||
module NATSBridge
|
||||
|
||||
using NATS, JSON, Arrow, HTTP, UUIDs, Dates, Base64, PrettyPrinting, DataFrames
|
||||
using JSON, Arrow, HTTP, UUIDs, Dates, Base64, PrettyPrinting, DataFrames
|
||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
||||
|
||||
# Constants
|
||||
@@ -340,25 +340,29 @@ end
|
||||
""" smartsend - Send data either directly via NATS or via a fileserver URL, depending on payload size
|
||||
|
||||
This function intelligently routes data delivery based on payload size relative to a threshold.
|
||||
If the serialized payload is smaller than `size_threshold`, it encodes the data as Base64 and publishes directly over NATS.
|
||||
Otherwise, it uploads the data to a fileserver (by default using `plik_oneshot_upload`) and publishes only the download URL over NATS.
|
||||
If the serialized payload is smaller than `size_threshold`, it encodes the data as Base64 and constructs a "direct" msg_payload_v1.
|
||||
Otherwise, it uploads the data to a fileserver (by default using `plik_oneshot_upload`) and constructs a "link" msg_payload_v1 with the download URL.
|
||||
|
||||
The function accepts a list of (dataname, data, type) tuples as input and processes each payload individually.
|
||||
Each payload can have a different type, enabling mixed-content messages (e.g., chat with text, images, audio).
|
||||
|
||||
This function creates and returns the msg_envelope_v1 and its JSON string representation only.
|
||||
NATS publishing must be performed by the caller.
|
||||
|
||||
# Function Workflow:
|
||||
1. Iterates through the list of (dataname, data, type) tuples
|
||||
2. For each payload: extracts the type from the tuple and serializes accordingly
|
||||
3. Compares the serialized size against `size_threshold`
|
||||
4. For small payloads: encodes as Base64, constructs a "direct" msg_payload_v1
|
||||
5. For large payloads: uploads to the fileserver, constructs a "link" msg_payload_v1 with the URL
|
||||
6. Converts envelope to JSON string and optionally publishes to NATS
|
||||
6. Constructs msg_envelope_v1 with all payloads and metadata
|
||||
7. Converts envelope to JSON string and returns (NATS publishing is handled by the caller)
|
||||
|
||||
# Arguments:
|
||||
- `subject::String` - NATS subject to publish the message to
|
||||
- `data::AbstractArray{Tuple{String, Any, String}}` - List of (dataname, data, type) tuples to send
|
||||
- `data::AbstractArray{Tuple{String, T1, String}, 1}` - List of (dataname, data, type) tuples to send
|
||||
- `dataname::String` - Name of the payload
|
||||
- `data::Any` - The actual data to send
|
||||
- `data::T1` - The actual data to send (any type supported by `_serialize_data`)
|
||||
- `payload_type::String` - Payload type: "text", "dictionary", "arrowtable", "jsontable", "image", "audio", "video", "binary"
|
||||
- No standalone `type` parameter - type is specified per payload
|
||||
|
||||
@@ -367,17 +371,15 @@ Each payload can have a different type, enabling mixed-content messages (e.g., c
|
||||
- `fileserver_url = DEFAULT_FILESERVER_URL` - URL of the HTTP file server for large payloads
|
||||
- `fileserver_upload_handler::Function = plik_oneshot_upload` - Function to handle fileserver uploads (must return Dict with "status", "uploadid", "fileid", "url" keys)
|
||||
- `size_threshold::Int = DEFAULT_SIZE_THRESHOLD` - Threshold in bytes separating direct vs link transport
|
||||
- `correlation_id::String = string(uuid4())` - Correlation ID for tracing (auto-generated UUID)
|
||||
- `msg_purpose::String = "chat"` - Purpose of the message: "ACK", "NACK", "updateStatus", "shutdown", "chat", etc.
|
||||
- `sender_name::String = "NATSBridge"` - Name of the sender
|
||||
- `receiver_name::String = ""` - Name of the receiver (empty string means broadcast)
|
||||
- `correlation_id::String = string(uuid4())` - Correlation ID for tracing (auto-generated UUID)
|
||||
- `msg_purpose::String = "chat"` - Purpose of the message: "ACK", "NACK", "updateStatus", "shutdown", "chat", etc.
|
||||
- `sender_name::String = "NATSBridge"` - Name of the sender
|
||||
- `receiver_name::String = ""` - Name of the receiver (empty string means broadcast)
|
||||
- `receiver_id::String = ""` - UUID of the receiver (empty string means broadcast)
|
||||
- `reply_to::String = ""` - Topic to reply to (empty string if no reply expected)
|
||||
- `reply_to_msg_id::String = ""` - Message ID this message is replying to
|
||||
- `is_publish::Bool = true` - Whether to automatically publish the message to NATS
|
||||
- `NATS_connection::Union{NATS.Connection, Nothing} = nothing` - Pre-existing NATS connection (if provided, uses this connection instead of creating a new one; saves connection establishment overhead)
|
||||
- `msg_id::String = string(uuid4())` - Message ID (auto-generated UUID if not provided)
|
||||
- `sender_id::String = string(uuid4())` - Sender ID (auto-generated UUID if not provided)
|
||||
- `reply_to::String = ""` - Topic to reply to (empty string if no reply expected)
|
||||
- `reply_to_msg_id::String = ""` - Message ID this message is replying to
|
||||
- `msg_id::String = string(uuid4())` - Message ID (auto-generated UUID if not provided)
|
||||
- `sender_id::String = string(uuid4())` - Sender ID (auto-generated UUID if not provided)
|
||||
|
||||
# Return:
|
||||
- `::Tuple{msg_envelope_v1, String}` - A tuple containing:
|
||||
@@ -412,8 +414,9 @@ env, msg_json = smartsend("chat.subject", [
|
||||
("audio_clip", audio_data, "audio")
|
||||
])
|
||||
|
||||
# Publish the JSON string directly using NATS request-reply pattern
|
||||
# reply = NATS.request(broker_url, subject, env_json_str; reply_to=reply_to_topic)
|
||||
# Publish the JSON string directly using NATS (manual publish)
|
||||
# conn = NATS.connect(broker_url)
|
||||
# NATS.publish(conn, subject, env_json_str)
|
||||
```
|
||||
"""
|
||||
function smartsend(
|
||||
@@ -438,8 +441,6 @@ function smartsend(
|
||||
receiver_id::String = "",
|
||||
reply_to::String = "",
|
||||
reply_to_msg_id::String = "",
|
||||
is_publish::Bool = true, # some time the user want to get env and env_json_str from this function without publishing the msg
|
||||
NATS_connection::Union{NATS.Connection, Nothing} = nothing, # a provided connection saves establishing connection overhead.
|
||||
msg_id::String = string(uuid4()), # Message ID
|
||||
sender_id::String = string(uuid4()) # Sender ID
|
||||
)::Tuple{msg_envelope_v1, String} where {T1<:Any}
|
||||
@@ -539,13 +540,15 @@ function smartsend(
|
||||
)
|
||||
|
||||
env_json_str = envelope_to_json(env) # Convert envelope to JSON
|
||||
if is_publish == false
|
||||
# skip publish a message
|
||||
elseif is_publish == true && NATS_connection === nothing
|
||||
publish_message(broker_url, subject, env_json_str, correlation_id) # Publish message to NATS
|
||||
elseif is_publish == true && NATS_connection !== nothing
|
||||
publish_message(NATS_connection, subject, env_json_str, correlation_id) # Publish message to NATS
|
||||
end
|
||||
# if is_publish == false
|
||||
# # skip publish a message
|
||||
# elseif is_publish == true && NATS_connection === nothing
|
||||
# # Publish message to NATS using new connection
|
||||
# publish_message(broker_url, subject, env_json_str, correlation_id)
|
||||
# elseif is_publish == true && NATS_connection !== nothing
|
||||
# # Publish message to NATS using existing connection
|
||||
# publish_message(NATS_connection, subject, env_json_str, correlation_id)
|
||||
# end
|
||||
|
||||
return (env, env_json_str)
|
||||
end
|
||||
@@ -700,73 +703,73 @@ function _serialize_data(data::Any, payload_type::String)
|
||||
end
|
||||
|
||||
|
||||
""" publish_message - Publish message to NATS
|
||||
This function publishes a message to a NATS subject with proper
|
||||
connection management and logging.
|
||||
# """ publish_message - Publish message to NATS
|
||||
# This function publishes a message to a NATS subject with proper
|
||||
# connection management and logging.
|
||||
|
||||
# Arguments:
|
||||
- `broker_url::String` - NATS server URL (e.g., "nats://localhost:4222")
|
||||
- `subject::String` - NATS subject to publish to (e.g., "/agent/wine/api/v1/prompt")
|
||||
- `message::String` - JSON message to publish
|
||||
- `correlation_id::String` - Correlation ID for tracing and logging
|
||||
# # Arguments:
|
||||
# - `broker_url::String` - NATS server URL (e.g., "nats://localhost:4222")
|
||||
# - `subject::String` - NATS subject to publish to (e.g., "/agent/wine/api/v1/prompt")
|
||||
# - `message::String` - JSON message to publish
|
||||
# - `correlation_id::String` - Correlation ID for tracing and logging
|
||||
|
||||
# Return:
|
||||
- `nothing` - This function performs publishing but returns nothing
|
||||
# # Return:
|
||||
# - `nothing` - This function performs publishing but returns nothing
|
||||
|
||||
# Example
|
||||
```jldoctest
|
||||
using NATS
|
||||
# # Example
|
||||
# ```jldoctest
|
||||
# using NATS
|
||||
|
||||
# Prepare JSON message
|
||||
message = "{\"correlation_id\":\"abc123\",\"payload\":\"test\"}"
|
||||
# # Prepare JSON message
|
||||
# message = "{\"correlation_id\":\"abc123\",\"payload\":\"test\"}"
|
||||
|
||||
# Publish to NATS
|
||||
publish_message("nats://localhost:4222", "my.subject", message, "abc123")
|
||||
```
|
||||
"""
|
||||
function publish_message(broker_url::String, subject::String, message::String, correlation_id::String)
|
||||
conn = NATS.connect(broker_url) # Create NATS connection
|
||||
publish_message(conn, subject, message, correlation_id)
|
||||
end
|
||||
# # Publish to NATS
|
||||
# publish_message("nats://localhost:4222", "my.subject", message, "abc123")
|
||||
# ```
|
||||
# """
|
||||
# function publish_message(broker_url::String, subject::String, message::String, correlation_id::String)
|
||||
# conn = NATS.connect(broker_url) # Create NATS connection
|
||||
# publish_message(conn, subject, message, correlation_id)
|
||||
# end
|
||||
|
||||
""" publish_message - Publish message to NATS using pre-existing connection
|
||||
This function publishes a message to a NATS subject using a pre-existing NATS connection,
|
||||
avoiding the overhead of connection establishment.
|
||||
# """ publish_message - Publish message to NATS using pre-existing connection
|
||||
# This function publishes a message to a NATS subject using a pre-existing NATS connection,
|
||||
# avoiding the overhead of connection establishment.
|
||||
|
||||
# Arguments:
|
||||
- `conn::NATS.Connection` - Pre-existing NATS connection
|
||||
- `subject::String` - NATS subject to publish to (e.g., "/agent/wine/api/v1/prompt")
|
||||
- `message::String` - JSON message to publish
|
||||
- `correlation_id::String` - Correlation ID for tracing and logging
|
||||
# # Arguments:
|
||||
# - `conn::NATS.Connection` - Pre-existing NATS connection
|
||||
# - `subject::String` - NATS subject to publish to (e.g., "/agent/wine/api/v1/prompt")
|
||||
# - `message::String` - JSON message to publish
|
||||
# - `correlation_id::String` - Correlation ID for tracing and logging
|
||||
|
||||
# Return:
|
||||
- `nothing` - This function performs publishing but returns nothing
|
||||
# # Return:
|
||||
# - `nothing` - This function performs publishing but returns nothing
|
||||
|
||||
# Example
|
||||
```jldoctest
|
||||
using NATS
|
||||
# # Example
|
||||
# ```jldoctest
|
||||
# using NATS
|
||||
|
||||
# Prepare JSON message
|
||||
message = "{\"correlation_id\":\"abc123\",\"payload\":\"test\"}"
|
||||
# # Prepare JSON message
|
||||
# message = "{\"correlation_id\":\"abc123\",\"payload\":\"test\"}"
|
||||
|
||||
# Create connection once and reuse for multiple publishes
|
||||
conn = NATS.connect("nats://localhost:4222")
|
||||
publish_message(conn, "my.subject", message, "abc123")
|
||||
# Connection is automatically drained after publish
|
||||
```
|
||||
# # Create connection once and reuse for multiple publishes
|
||||
# conn = NATS.connect("nats://localhost:4222")
|
||||
# publish_message(conn, "my.subject", message, "abc123")
|
||||
# # Connection is automatically drained after publish
|
||||
# ```
|
||||
|
||||
# Use Case:
|
||||
Use this version when you already have an established NATS connection and want to publish
|
||||
multiple messages without the overhead of creating a new connection for each publish.
|
||||
"""
|
||||
function publish_message(conn::NATS.Connection, subject::String, message::String, correlation_id::String)
|
||||
try
|
||||
NATS.publish(conn, subject, message) # Publish message to NATS
|
||||
log_trace(correlation_id, "Message published to $subject") # Log successful publish
|
||||
finally
|
||||
NATS.drain(conn) # Ensure connection is closed properly
|
||||
end
|
||||
end
|
||||
# # Use Case:
|
||||
# Use this version when you already have an established NATS connection and want to publish
|
||||
# multiple messages without the overhead of creating a new connection for each publish.
|
||||
# """
|
||||
# function publish_message(conn::NATS.Connection, subject::String, message::String, correlation_id::String)
|
||||
# try
|
||||
# NATS.publish(conn, subject, message) # Publish message to NATS
|
||||
# log_trace(correlation_id, "Message published to $subject") # Log successful publish
|
||||
# finally
|
||||
# NATS.drain(conn) # Ensure connection is closed properly
|
||||
# end
|
||||
# end
|
||||
|
||||
|
||||
""" smartreceive - Receive and process messages from NATS
|
||||
@@ -783,7 +786,7 @@ A HTTP file server is required along with its download function.
|
||||
5. For link transport: fetches data from URL with exponential backoff, then deserializes
|
||||
|
||||
# Arguments:
|
||||
- `msg::NATS.Msg` - NATS message to process
|
||||
- `msg_json_str::String` - JSON string from NATS message payload (e.g., `String(nats_msg.payload)`)
|
||||
|
||||
# Keyword Arguments:
|
||||
- `fileserver_download_handler::Function = _fetch_with_backoff` - Function to handle downloading data from file server URLs
|
||||
@@ -798,19 +801,21 @@ A HTTP file server is required along with its download function.
|
||||
```jldoctest
|
||||
# Receive and process message
|
||||
msg = nats_message # NATS message
|
||||
env = smartreceive(msg; fileserver_download_handler=_fetch_with_backoff, max_retries=5, base_delay=100, max_delay=5000)
|
||||
msg_json_str = String(msg.payload)
|
||||
env = smartreceive(msg_json_str; fileserver_download_handler=_fetch_with_backoff, max_retries=5, base_delay=100, max_delay=5000)
|
||||
# env["payloads"] = [("dataname1", data1, "type1"), ("dataname2", data2, "type2"), ...]
|
||||
```
|
||||
"""
|
||||
function smartreceive(
|
||||
msg::NATS.Msg;
|
||||
msg_json_str::String; # get it from String(nats_msg.payload)
|
||||
fileserver_download_handler::Function = _fetch_with_backoff,
|
||||
max_retries::Int = 5,
|
||||
base_delay::Int = 100,
|
||||
max_delay::Int = 5000
|
||||
)::JSON.Object{String, Any}
|
||||
|
||||
# Parse the JSON envelope
|
||||
env_json_obj = JSON.parse(String(msg.payload))
|
||||
env_json_obj = JSON.parse(msg_json_str)
|
||||
log_trace(env_json_obj["correlation_id"], "Processing received message") # Log message processing start
|
||||
|
||||
# Process all payloads in the envelope
|
||||
|
||||
1230
src/natsbridge.rs
Normal file
1230
src/natsbridge.rs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -6,7 +6,15 @@
|
||||
* using NATS as the message bus, with support for both direct payload transport and
|
||||
* URL-based transport for larger payloads.
|
||||
*
|
||||
* Supported payload types: "text", "dictionary", "arrowtable", "jsontable", "image", "audio", "video", "binary"
|
||||
* Supported payload types: "text", "dictionary", "jsontable", "image", "audio", "video", "binary"
|
||||
* Note: Browser version does NOT support Apache Arrow IPC (arrowtable) due to browser compatibility constraints.
|
||||
* Use "jsontable" for tabular data in browser applications.
|
||||
*
|
||||
* Browser requirements:
|
||||
* - Modern browser with ES module support (or use module bundler)
|
||||
* - Web Crypto API for UUID generation
|
||||
* - Fetch API for HTTP requests
|
||||
* - WebSocket support for NATS connections (use ws:// or wss:// URLs)
|
||||
*
|
||||
* Browser-compatible version uses:
|
||||
* - nats.ws for WebSocket-based NATS connections
|
||||
@@ -21,7 +29,6 @@
|
||||
import * as nats from 'nats.ws';
|
||||
|
||||
// Use native fetch available in browsers
|
||||
import { tableFromArrays, tableToIPC } from 'apache-arrow/browser';
|
||||
|
||||
// ---------------------------------------------- Constants ---------------------------------------------- //
|
||||
|
||||
@@ -49,10 +56,7 @@ const DEFAULT_FILESERVER_URL = 'http://localhost:8080';
|
||||
*/
|
||||
function bufferToBase64(data) {
|
||||
const bytes = new Uint8Array(data);
|
||||
let binary = '';
|
||||
for (let i = 0; i < bytes.length; i++) {
|
||||
binary += String.fromCharCode(bytes[i]);
|
||||
}
|
||||
const binary = String.fromCharCode(...bytes);
|
||||
return btoa(binary);
|
||||
}
|
||||
|
||||
@@ -71,6 +75,34 @@ function base64ToBuffer(base64) {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Uint8Array to Base64 string (Unicode-safe version)
|
||||
* Uses TextEncoder/TextDecoder for proper Unicode handling
|
||||
* @param {Uint8Array} data - Data to encode
|
||||
* @returns {string} Base64 encoded string
|
||||
*/
|
||||
function bufferToBase64UnicodeSafe(data) {
|
||||
const bytes = new Uint8Array(data);
|
||||
// Use TextDecoder to properly handle the bytes as text
|
||||
const binary = String.fromCharCode(...bytes);
|
||||
return btoa(binary);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Base64 string to Uint8Array (Unicode-safe version)
|
||||
* @param {string} base64 - Base64 encoded string
|
||||
* @returns {Uint8Array} Decoded binary data
|
||||
*/
|
||||
function base64ToBufferUnicodeSafe(base64) {
|
||||
const binary = atob(base64);
|
||||
const len = binary.length;
|
||||
const bytes = new Uint8Array(len);
|
||||
for (let i = 0; i < len; i++) {
|
||||
bytes[i] = binary.charCodeAt(i);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate UUID v4 using Web Crypto API
|
||||
* @returns {string} UUID string
|
||||
@@ -98,7 +130,7 @@ function logTrace(correlationId, message) {
|
||||
/**
|
||||
* Serialize data according to specified format
|
||||
* @param {any} data - Data to serialize
|
||||
* @param {string} payloadType - Target format: "text", "dictionary", "arrowtable", "jsontable", "image", "audio", "video", "binary"
|
||||
* @param {string} payloadType - Target format: "text", "dictionary", "jsontable", "image", "audio", "video", "binary"
|
||||
* @returns {Uint8Array} Binary representation of the serialized data
|
||||
*/
|
||||
async function serializeData(data, payloadType) {
|
||||
@@ -111,13 +143,6 @@ async function serializeData(data, payloadType) {
|
||||
} else if (payloadType === 'dictionary') {
|
||||
const jsonStr = JSON.stringify(data);
|
||||
return new Uint8Array(new TextEncoder().encode(jsonStr));
|
||||
} else if (payloadType === 'arrowtable') {
|
||||
// Convert array of objects to Arrow IPC format
|
||||
if (!Array.isArray(data) || data.length === 0) {
|
||||
throw new Error('Arrow table data must be a non-empty array of objects');
|
||||
}
|
||||
|
||||
return serializeArrowTable(data);
|
||||
} else if (payloadType === 'jsontable') {
|
||||
// Serialize array of objects to JSON format
|
||||
if (!Array.isArray(data)) {
|
||||
@@ -154,49 +179,6 @@ async function serializeData(data, payloadType) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to properly serialize table data to Arrow IPC
|
||||
* @param {Array<Object>} data - Array of objects representing table rows
|
||||
* @returns {Uint8Array} Arrow IPC formatted buffer
|
||||
*/
|
||||
function serializeArrowTable(data) {
|
||||
if (!Array.isArray(data) || data.length === 0) {
|
||||
throw new Error('Table data must be a non-empty array of objects');
|
||||
}
|
||||
|
||||
logTrace('serializeArrowTable', `Serializing table with ${data.length} rows`);
|
||||
|
||||
// Convert array of objects to a key-value format expected by tableFromArrays
|
||||
const columns = {};
|
||||
const keys = Object.keys(data[0]);
|
||||
for (const key of keys) {
|
||||
columns[key] = data.map(row => row[key]);
|
||||
}
|
||||
|
||||
logTrace('serializeArrowTable', `Columns: ${Object.keys(columns).join(', ')}`);
|
||||
|
||||
const table = tableFromArrays(columns);
|
||||
|
||||
logTrace('serializeArrowTable', `Arrow table created with ${table.numRows} rows, ${table.numCols} cols`);
|
||||
|
||||
// Convert to IPC format
|
||||
const ipcBuffer = tableToIPC(table);
|
||||
|
||||
logTrace('serializeArrowTable', `IPC buffer type: ${typeof ipcBuffer}, byteLength: ${ipcBuffer.byteLength}`);
|
||||
|
||||
const resultBuffer = new Uint8Array(ipcBuffer);
|
||||
logTrace('serializeArrowTable', `Result buffer: ${resultBuffer.length} bytes`);
|
||||
|
||||
// Debug: Show first 20 bytes in hex
|
||||
const hexPreview = [];
|
||||
for (let i = 0; i < Math.min(20, resultBuffer.length); i++) {
|
||||
hexPreview.push(resultBuffer[i].toString(16).padStart(2, '0'));
|
||||
}
|
||||
logTrace('serializeArrowTable', `First 20 bytes (hex): ${hexPreview.join(' ')}`);
|
||||
|
||||
return resultBuffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize bytes to data based on type
|
||||
* @param {Uint8Array|ArrayBuffer} data - Serialized data as bytes
|
||||
@@ -210,7 +192,7 @@ async function deserializeData(data, payloadType, correlationId) {
|
||||
logTrace(correlationId, `deserializeData: type=${payloadType}, bufferLength=${buffer.length}`);
|
||||
|
||||
// Debug: Show first 20 bytes in hex for binary data
|
||||
if (payloadType === 'arrowtable' || payloadType === 'jsontable' || payloadType === 'image' || payloadType === 'binary') {
|
||||
if (payloadType === 'jsontable' || payloadType === 'image' || payloadType === 'binary') {
|
||||
const hexPreview = [];
|
||||
for (let i = 0; i < Math.min(20, buffer.length); i++) {
|
||||
hexPreview.push(buffer[i].toString(16).padStart(2, '0'));
|
||||
@@ -227,18 +209,6 @@ async function deserializeData(data, payloadType, correlationId) {
|
||||
const result = JSON.parse(jsonStr);
|
||||
logTrace(correlationId, `deserializeData: dictionary keys=${Object.keys(result).join(', ')}`);
|
||||
return result;
|
||||
} else if (payloadType === 'arrowtable') {
|
||||
logTrace(correlationId, `deserializeData: Attempting Arrow table deserialization`);
|
||||
|
||||
try {
|
||||
// Try tableFromIPC (browser API)
|
||||
const table = tableFromIPC(buffer);
|
||||
logTrace(correlationId, `deserializeData: Arrow table from IPC - rows=${table.numRows}, cols=${table.numCols}`);
|
||||
return table;
|
||||
} catch (e) {
|
||||
logTrace(correlationId, `deserializeData: tableFromIPC failed: ${e.message}`);
|
||||
throw new Error(`Unable to deserialize Arrow table: ${e.message}`);
|
||||
}
|
||||
} else if (payloadType === 'jsontable') {
|
||||
const jsonStr = new TextDecoder().decode(buffer);
|
||||
const result = JSON.parse(jsonStr);
|
||||
@@ -357,15 +327,18 @@ async function fetchWithBackoff(url, maxRetries, baseDelay, maxDelay, correlatio
|
||||
|
||||
/**
|
||||
* NATS client wrapper for connection management
|
||||
* Supports both single-use and persistent connection modes
|
||||
*/
|
||||
class NATSClient {
|
||||
/**
|
||||
* Create a new NATS client
|
||||
* @param {string} url - NATS server URL (ws:// or wss://)
|
||||
* @param {boolean} [keepAlive=false] - Keep connection open for multiple publishes
|
||||
*/
|
||||
constructor(url) {
|
||||
constructor(url, keepAlive = false) {
|
||||
this.url = url;
|
||||
this.connection = null;
|
||||
this.keepAlive = keepAlive;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -373,6 +346,9 @@ class NATSClient {
|
||||
* @returns {Promise<NATS.Connection>}
|
||||
*/
|
||||
async connect() {
|
||||
if (this.connection) {
|
||||
return this.connection;
|
||||
}
|
||||
this.connection = await nats.connect({ servers: this.url });
|
||||
return this.connection;
|
||||
}
|
||||
@@ -397,8 +373,94 @@ class NATSClient {
|
||||
async close() {
|
||||
if (this.connection) {
|
||||
this.connection.close();
|
||||
this.connection = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current connection (for external use)
|
||||
* @returns {NATS.Connection|null}
|
||||
*/
|
||||
getConnection() {
|
||||
return this.connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if connected
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isConnected() {
|
||||
return this.connection !== null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connection pool for managing multiple NATS connections
|
||||
* Useful for applications with multiple concurrent publishers
|
||||
*/
|
||||
class NATSConnectionPool {
|
||||
/**
|
||||
* Create a new connection pool
|
||||
* @param {string} url - NATS server URL (ws:// or wss://)
|
||||
* @param {number} [maxSize=10] - Maximum pool size
|
||||
*/
|
||||
constructor(url, maxSize = 10) {
|
||||
this.url = url;
|
||||
this.maxSize = maxSize;
|
||||
this.connections = new Map();
|
||||
this.idCounter = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a connection from the pool (or create new)
|
||||
* @returns {Promise<NATSClient>}
|
||||
*/
|
||||
async acquire() {
|
||||
// Try to find an existing idle connection
|
||||
for (const [id, client] of this.connections) {
|
||||
if (client.isConnected()) {
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
// Create new connection if under limit
|
||||
if (this.connections.size < this.maxSize) {
|
||||
const id = `conn_${++this.idCounter}`;
|
||||
const client = new NATSClient(this.url, true);
|
||||
await client.connect();
|
||||
this.connections.set(id, client);
|
||||
return client;
|
||||
}
|
||||
|
||||
// Pool exhausted - create new connection (caller should close when done)
|
||||
const client = new NATSClient(this.url, false);
|
||||
await client.connect();
|
||||
return client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a connection to the pool
|
||||
* @param {NATSClient} client - Connection to return
|
||||
*/
|
||||
release(client) {
|
||||
// Only return persistent connections
|
||||
if (client.keepAlive && client.isConnected()) {
|
||||
// Connection already in pool, do nothing
|
||||
return;
|
||||
}
|
||||
// Non-persistent connection - close it
|
||||
client.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Close all connections in the pool
|
||||
*/
|
||||
async closeAll() {
|
||||
for (const [id, client] of this.connections) {
|
||||
await client.close();
|
||||
}
|
||||
this.connections.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------- Core Functions ---------------------------------------------- //
|
||||
@@ -409,9 +471,11 @@ class NATSClient {
|
||||
* @param {string} subject - NATS subject to publish to
|
||||
* @param {string} message - JSON message to publish
|
||||
* @param {string} correlationId - Correlation ID for tracing
|
||||
* @param {boolean} [closeConnection=true] - Close connection after publish (set false for persistent connections)
|
||||
*/
|
||||
async function publishMessage(brokerUrlOrClient, subject, message, correlationId) {
|
||||
async function publishMessage(brokerUrlOrClient, subject, message, correlationId, closeConnection = true) {
|
||||
let conn;
|
||||
let shouldClose = false;
|
||||
|
||||
if (brokerUrlOrClient instanceof NATSClient) {
|
||||
conn = brokerUrlOrClient;
|
||||
@@ -425,15 +489,18 @@ async function publishMessage(brokerUrlOrClient, subject, message, correlationId
|
||||
await brokerUrlOrClient.close();
|
||||
}
|
||||
};
|
||||
shouldClose = true;
|
||||
} else {
|
||||
// String URL - create new client
|
||||
const client = new NATSClient(brokerUrlOrClient);
|
||||
conn = client;
|
||||
shouldClose = true;
|
||||
}
|
||||
|
||||
await conn.publish(subject, message, correlationId);
|
||||
|
||||
if (conn instanceof NATSClient) {
|
||||
// Only close if explicitly requested and it's a short-lived client
|
||||
if (shouldClose && closeConnection && conn instanceof NATSClient) {
|
||||
await conn.close();
|
||||
}
|
||||
}
|
||||
@@ -478,8 +545,6 @@ function buildPayload(dataname, payloadType, payloadBytes, transport, data) {
|
||||
let encoding = 'base64';
|
||||
if (payloadType === 'jsontable') {
|
||||
encoding = 'json';
|
||||
} else if (payloadType === 'arrowtable') {
|
||||
encoding = 'arrow-ipc';
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -504,7 +569,8 @@ function buildPayload(dataname, payloadType, payloadBytes, transport, data) {
|
||||
*
|
||||
* @param {string} subject - NATS subject to publish the message to
|
||||
* @param {Array} data - List of [dataname, data, type] tuples to send
|
||||
* - type: "text", "dictionary", "arrowtable", "jsontable", "image", "audio", "video", "binary"
|
||||
* - type: "text", "dictionary", "jsontable", "image", "audio", "video", "binary"
|
||||
* - Note: "arrowtable" is NOT supported in browser (use "jsontable" for tabular data)
|
||||
* @param {Object} options - Optional configuration
|
||||
* @param {string} [options.broker_url=DEFAULT_BROKER_URL] - URL of the NATS server (WebSocket)
|
||||
* @param {string} [options.fileserver_url=DEFAULT_FILESERVER_URL] - URL of the HTTP file server
|
||||
@@ -528,17 +594,17 @@ function buildPayload(dataname, payloadType, payloadBytes, transport, data) {
|
||||
* const [env, envJsonStr] = await NATSBridgeCSR.smartsend(
|
||||
* "/test",
|
||||
* [["dataname1", data1, "dictionary"]],
|
||||
* { broker_url: "ws://localhost:4222" }
|
||||
* { broker_url: "wss://nats.example.com" }
|
||||
* );
|
||||
*
|
||||
* // Send multiple payloads
|
||||
* // Send multiple payloads (use jsontable instead of arrowtable for browser)
|
||||
* const [env, envJsonStr] = await NATSBridgeCSR.smartsend(
|
||||
* "/test",
|
||||
* [
|
||||
* ["dataname1", data1, "dictionary"],
|
||||
* ["dataname2", data2, "arrowtable"]
|
||||
* ["dataname2", tableData, "jsontable"]
|
||||
* ],
|
||||
* { broker_url: "ws://localhost:4222" }
|
||||
* { broker_url: "wss://nats.example.com" }
|
||||
* );
|
||||
*/
|
||||
async function smartsend(subject, data, options = {}) {
|
||||
@@ -774,9 +840,37 @@ async function smartreceive(msg, options = {}) {
|
||||
const NATSBridgeCSR = {
|
||||
/**
|
||||
* NATS client class for connection management
|
||||
* Supports both single-use and persistent connection modes
|
||||
*
|
||||
* @example
|
||||
* // Single-use connection (closes after publish)
|
||||
* const client = new NATSBridgeCSR.NATSClient("wss://nats.example.com");
|
||||
* await NATSBridgeCSR.smartsend("/test", [["msg", "Hello", "text"]], { nats_connection: client });
|
||||
* await client.close();
|
||||
*
|
||||
* // Persistent connection (keeps connection open)
|
||||
* const client = new NATSBridgeCSR.NATSClient("wss://nats.example.com", true);
|
||||
* await client.connect();
|
||||
* await NATSBridgeCSR.smartsend("/test1", [["msg", "Hello", "text"]], { nats_connection: client, is_publish: false });
|
||||
* await NATSBridgeCSR.publishMessage(client, "/test2", JSON.stringify({msg: "World"}), "trace-id");
|
||||
* // Connection remains open for more publishes
|
||||
* await client.close();
|
||||
*/
|
||||
NATSClient,
|
||||
|
||||
/**
|
||||
* Connection pool for managing multiple NATS connections
|
||||
* Useful for applications with multiple concurrent publishers
|
||||
*
|
||||
* @example
|
||||
* const pool = new NATSBridgeCSR.NATSConnectionPool("wss://nats.example.com", 10);
|
||||
* const client = await pool.acquire();
|
||||
* await NATSBridgeCSR.smartsend("/test", [["msg", "Hello", "text"]], { nats_connection: client });
|
||||
* pool.release(client);
|
||||
* await pool.closeAll();
|
||||
*/
|
||||
NATSConnectionPool,
|
||||
|
||||
/**
|
||||
* Send data via NATS with automatic transport selection
|
||||
*/
|
||||
@@ -787,6 +881,19 @@ const NATSBridgeCSR = {
|
||||
*/
|
||||
smartreceive,
|
||||
|
||||
/**
|
||||
* Publish message to NATS
|
||||
*
|
||||
* @example
|
||||
* // Using a persistent connection
|
||||
* const client = new NATSBridgeCSR.NATSClient("wss://nats.example.com", true);
|
||||
* await client.connect();
|
||||
* await NATSBridgeCSR.publishMessage(client, "/subject", JSON.stringify({msg: "Hello"}), "trace-id", false);
|
||||
* // Connection stays open for more publishes
|
||||
* await client.close();
|
||||
*/
|
||||
publishMessage,
|
||||
|
||||
/**
|
||||
* Upload data to plik server in one-shot mode
|
||||
*/
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* NATSBridge - Cross-Platform Bi-Directional Data Bridge
|
||||
* JavaScript/Node.js Implementation (Client-Side Rendering)
|
||||
* JavaScript/Node.js Implementation (Desktop/Server-Side)
|
||||
*
|
||||
* This module provides functionality for sending and receiving data across network boundaries
|
||||
* using NATS as the message bus, with support for both direct payload transport and
|
||||
@@ -8,6 +8,12 @@
|
||||
*
|
||||
* Supported payload types: "text", "dictionary", "arrowtable", "jsontable", "image", "audio", "video", "binary"
|
||||
*
|
||||
* Node.js-specific features:
|
||||
* - Apache Arrow IPC support via apache-arrow
|
||||
* - TCP NATS connections (nats:// or tls:// URLs)
|
||||
* - Buffer for binary data handling
|
||||
* - Connection pooling for high-throughput scenarios
|
||||
*
|
||||
* @module NATSBridge
|
||||
*/
|
||||
|
||||
@@ -342,15 +348,18 @@ async function fetchWithBackoff(url, maxRetries, baseDelay, maxDelay, correlatio
|
||||
|
||||
/**
|
||||
* NATS client wrapper for connection management
|
||||
* Supports both single-use and persistent connection modes
|
||||
*/
|
||||
class NATSClient {
|
||||
/**
|
||||
* Create a new NATS client
|
||||
* @param {string} url - NATS server URL
|
||||
* @param {string} url - NATS server URL (nats:// or tls://)
|
||||
* @param {boolean} [keepAlive=false] - Keep connection open for multiple publishes
|
||||
*/
|
||||
constructor(url) {
|
||||
constructor(url, keepAlive = false) {
|
||||
this.url = url;
|
||||
this.connection = null;
|
||||
this.keepAlive = keepAlive;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -358,6 +367,9 @@ class NATSClient {
|
||||
* @returns {Promise<NATS.Connection>}
|
||||
*/
|
||||
async connect() {
|
||||
if (this.connection) {
|
||||
return this.connection;
|
||||
}
|
||||
this.connection = await nats.connect({ servers: this.url });
|
||||
return this.connection;
|
||||
}
|
||||
@@ -382,8 +394,94 @@ class NATSClient {
|
||||
async close() {
|
||||
if (this.connection) {
|
||||
this.connection.close();
|
||||
this.connection = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current connection (for external use)
|
||||
* @returns {NATS.Connection|null}
|
||||
*/
|
||||
getConnection() {
|
||||
return this.connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if connected
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isConnected() {
|
||||
return this.connection !== null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connection pool for managing multiple NATS connections
|
||||
* Useful for applications with multiple concurrent publishers
|
||||
*/
|
||||
class NATSConnectionPool {
|
||||
/**
|
||||
* Create a new connection pool
|
||||
* @param {string} url - NATS server URL (nats:// or tls://)
|
||||
* @param {number} [maxSize=10] - Maximum pool size
|
||||
*/
|
||||
constructor(url, maxSize = 10) {
|
||||
this.url = url;
|
||||
this.maxSize = maxSize;
|
||||
this.connections = new Map();
|
||||
this.idCounter = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a connection from the pool (or create new)
|
||||
* @returns {Promise<NATSClient>}
|
||||
*/
|
||||
async acquire() {
|
||||
// Try to find an existing idle connection
|
||||
for (const [id, client] of this.connections) {
|
||||
if (client.isConnected()) {
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
// Create new connection if under limit
|
||||
if (this.connections.size < this.maxSize) {
|
||||
const id = `conn_${++this.idCounter}`;
|
||||
const client = new NATSClient(this.url, true);
|
||||
await client.connect();
|
||||
this.connections.set(id, client);
|
||||
return client;
|
||||
}
|
||||
|
||||
// Pool exhausted - create new connection (caller should close when done)
|
||||
const client = new NATSClient(this.url, false);
|
||||
await client.connect();
|
||||
return client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a connection to the pool
|
||||
* @param {NATSClient} client - Connection to return
|
||||
*/
|
||||
release(client) {
|
||||
// Only return persistent connections
|
||||
if (client.keepAlive && client.isConnected()) {
|
||||
// Connection already in pool, do nothing
|
||||
return;
|
||||
}
|
||||
// Non-persistent connection - close it
|
||||
client.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Close all connections in the pool
|
||||
*/
|
||||
async closeAll() {
|
||||
for (const [id, client] of this.connections) {
|
||||
await client.close();
|
||||
}
|
||||
this.connections.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------- Core Functions ---------------------------------------------- //
|
||||
@@ -394,9 +492,11 @@ class NATSClient {
|
||||
* @param {string} subject - NATS subject to publish to
|
||||
* @param {string} message - JSON message to publish
|
||||
* @param {string} correlationId - Correlation ID for tracing
|
||||
* @param {boolean} [closeConnection=true] - Close connection after publish (set false for persistent connections)
|
||||
*/
|
||||
async function publishMessage(brokerUrlOrClient, subject, message, correlationId) {
|
||||
async function publishMessage(brokerUrlOrClient, subject, message, correlationId, closeConnection = true) {
|
||||
let conn;
|
||||
let shouldClose = false;
|
||||
|
||||
if (brokerUrlOrClient instanceof NATSClient) {
|
||||
conn = brokerUrlOrClient;
|
||||
@@ -410,15 +510,18 @@ async function publishMessage(brokerUrlOrClient, subject, message, correlationId
|
||||
await brokerUrlOrClient.close();
|
||||
}
|
||||
};
|
||||
shouldClose = true;
|
||||
} else {
|
||||
// String URL - create new client
|
||||
const client = new NATSClient(brokerUrlOrClient);
|
||||
conn = client;
|
||||
shouldClose = true;
|
||||
}
|
||||
|
||||
await conn.publish(subject, message, correlationId);
|
||||
|
||||
if (conn instanceof NATSClient) {
|
||||
// Only close if explicitly requested and it's a short-lived client
|
||||
if (shouldClose && closeConnection && conn instanceof NATSClient) {
|
||||
await conn.close();
|
||||
}
|
||||
}
|
||||
@@ -764,9 +867,37 @@ async function smartreceive(msg, options = {}) {
|
||||
const NATSBridge = {
|
||||
/**
|
||||
* NATS client class for connection management
|
||||
* Supports both single-use and persistent connection modes
|
||||
*
|
||||
* @example
|
||||
* // Single-use connection (closes after publish)
|
||||
* const client = new NATSBridge.NATSClient("nats://localhost:4222");
|
||||
* await NATSBridge.smartsend("/test", [["msg", "Hello", "text"]], { nats_connection: client });
|
||||
* await client.close();
|
||||
*
|
||||
* // Persistent connection (keeps connection open)
|
||||
* const client = new NATSBridge.NATSClient("nats://localhost:4222", true);
|
||||
* await client.connect();
|
||||
* await NATSBridge.smartsend("/test1", [["msg", "Hello", "text"]], { nats_connection: client, is_publish: false });
|
||||
* await NATSBridge.publishMessage(client, "/test2", JSON.stringify({msg: "World"}), "trace-id");
|
||||
* // Connection remains open for more publishes
|
||||
* await client.close();
|
||||
*/
|
||||
NATSClient,
|
||||
|
||||
/**
|
||||
* Connection pool for managing multiple NATS connections
|
||||
* Useful for applications with multiple concurrent publishers
|
||||
*
|
||||
* @example
|
||||
* const pool = new NATSBridge.NATSConnectionPool("nats://localhost:4222", 10);
|
||||
* const client = await pool.acquire();
|
||||
* await NATSBridge.smartsend("/test", [["msg", "Hello", "text"]], { nats_connection: client });
|
||||
* pool.release(client);
|
||||
* await pool.closeAll();
|
||||
*/
|
||||
NATSConnectionPool,
|
||||
|
||||
/**
|
||||
* Send data via NATS with automatic transport selection
|
||||
*/
|
||||
@@ -777,6 +908,19 @@ const NATSBridge = {
|
||||
*/
|
||||
smartreceive,
|
||||
|
||||
/**
|
||||
* Publish message to NATS
|
||||
*
|
||||
* @example
|
||||
* // Using a persistent connection
|
||||
* const client = new NATSBridge.NATSClient("nats://localhost:4222", true);
|
||||
* await client.connect();
|
||||
* await NATSBridge.publishMessage(client, "/subject", JSON.stringify({msg: "Hello"}), "trace-id", false);
|
||||
* // Connection stays open for more publishes
|
||||
* await client.close();
|
||||
*/
|
||||
publishMessage,
|
||||
|
||||
/**
|
||||
* Upload data to plik server in one-shot mode
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user