Compare commits
54 Commits
v0.4.0
...
split_smar
| Author | SHA1 | Date | |
|---|---|---|---|
| fcc50847e4 | |||
| f8d93991f5 | |||
| bee9f783d9 | |||
| 3e1c8d563e | |||
| 1299febcdc | |||
| be94c62760 | |||
| 6a862ef243 | |||
| ae2de5fc62 | |||
| df0bbc7327 | |||
| d94761c866 | |||
| f8235e1a59 | |||
| 647cadf497 | |||
| 8c793a81b6 | |||
| 6a42ba7e43 | |||
| 14b3790251 | |||
| 61d81bed62 | |||
| 1a10bc1a5f | |||
| 7f68d08134 | |||
| ab20cd896f | |||
| 5a9e93d6e7 | |||
| b51641dc7e | |||
| 45f1257896 | |||
| 3e2b8b1e3a | |||
| 90d81617ef | |||
| 64c62e616b | |||
| 2c340e37c7 | |||
| 7853e94d2e | |||
| 99bf57b154 | |||
| 0fa6eaf95b | |||
| 76f42be740 | |||
| d99dc41be9 | |||
| 263508b8f7 | |||
| 0c2cca30ed | |||
| 46fdf668c6 | |||
| f8a92a45a0 | |||
| cec70e6036 | |||
| f9e08ba628 | |||
| c12a078149 | |||
| dedd803dc3 | |||
| e8e927a491 | |||
| d950bbac23 | |||
| fc8da2ebf5 | |||
| f6e50c405f | |||
| c06f508e8f | |||
| 97bf1e47f4 | |||
| ef47fddd56 | |||
| 896dd84d2a | |||
| def75d8f86 | |||
| 69f2173f75 | |||
| 075d355c58 | |||
| 0de9725ba8 | |||
| 6dcccc903f | |||
| 507b4951b4 | |||
| a064be0e5c |
@@ -13,3 +13,41 @@ Role: Principal Systems Architect & Lead Software Engineer.Objective: Implement
|
|||||||
|
|
||||||
|
|
||||||
Create a walkthrough for Julia service-A service sending a mix-content chat message to Julia service-B. the chat message must includes
|
Create a walkthrough for Julia service-A service sending a mix-content chat message to Julia service-B. the chat message must includes
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
I updated the following:
|
||||||
|
- NATSBridge.jl. Essentially I add NATS_connection keyword and new publish_message function to support the keyword.
|
||||||
|
|
||||||
|
Use them and ONLY them as ground truth.
|
||||||
|
|
||||||
|
Then update the following files accordingly:
|
||||||
|
- architecture.md
|
||||||
|
- implementation.md
|
||||||
|
|
||||||
|
All API should be semantically consistent and naming should be consistent across the board.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Task: Update NATSBridge.js to reflect recent changes in NATSBridge.jl and docs
|
||||||
|
|
||||||
|
Context: NATSBridge.jl and docs has been updated.
|
||||||
|
|
||||||
|
Requirements:
|
||||||
|
|
||||||
|
Source of Truth: Treat the updated NATSBridge.jl and docs as the definitive source.
|
||||||
|
API Consistency: Ensure the Main Package API (e.g., smartsend(), publish_message()) uses consistent naming across all three supported languages.
|
||||||
|
Ecosystem Variance: Low-level native functions (e.g., NATS.connect(), JSON.read()) should follow the conventions of the specific language ecosystem and do not require cross-language consistency.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
julia_version = "1.12.5"
|
julia_version = "1.12.5"
|
||||||
manifest_format = "2.0"
|
manifest_format = "2.0"
|
||||||
project_hash = "8a7a8b88d777403234a6816e699fb0ab1e991aac"
|
project_hash = "b632f853bcf5355f5c53ad3efa7a19f70444dc6c"
|
||||||
|
|
||||||
[[deps.AliasTables]]
|
[[deps.AliasTables]]
|
||||||
deps = ["PtrArrays", "Random"]
|
deps = ["PtrArrays", "Random"]
|
||||||
@@ -436,6 +436,12 @@ git-tree-sha1 = "d9d9a189fb9155a460e6b5e8966bf6a66737abf8"
|
|||||||
uuid = "55e73f9c-eeeb-467f-b4cc-a633fde63d2a"
|
uuid = "55e73f9c-eeeb-467f-b4cc-a633fde63d2a"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[[deps.NATSBridge]]
|
||||||
|
deps = ["Arrow", "DataFrames", "Dates", "GeneralUtils", "HTTP", "JSON", "NATS", "PrettyPrinting", "Revise", "UUIDs"]
|
||||||
|
path = "."
|
||||||
|
uuid = "f2724d33-f338-4a57-b9f8-1be882570d10"
|
||||||
|
version = "0.4.1"
|
||||||
|
|
||||||
[[deps.NanoDates]]
|
[[deps.NanoDates]]
|
||||||
deps = ["Dates", "Parsers"]
|
deps = ["Dates", "Parsers"]
|
||||||
git-tree-sha1 = "850a0557ae5934f6e67ac0dc5ca13d0328422d1f"
|
git-tree-sha1 = "850a0557ae5934f6e67ac0dc5ca13d0328422d1f"
|
||||||
|
|||||||
10
Project.toml
10
Project.toml
@@ -1,5 +1,11 @@
|
|||||||
|
name = "NATSBridge"
|
||||||
|
uuid = "f2724d33-f338-4a57-b9f8-1be882570d10"
|
||||||
|
version = "0.4.3"
|
||||||
|
authors = ["narawat <narawat@gmail.com>"]
|
||||||
|
|
||||||
[deps]
|
[deps]
|
||||||
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
|
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
|
||||||
|
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
|
||||||
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
|
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
|
||||||
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
|
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
|
||||||
GeneralUtils = "c6c72f09-b708-4ac8-ac7c-2084d70108fe"
|
GeneralUtils = "c6c72f09-b708-4ac8-ac7c-2084d70108fe"
|
||||||
@@ -9,3 +15,7 @@ NATS = "55e73f9c-eeeb-467f-b4cc-a633fde63d2a"
|
|||||||
PrettyPrinting = "54e16d92-306c-5ea0-a30b-337be88ac337"
|
PrettyPrinting = "54e16d92-306c-5ea0-a30b-337be88ac337"
|
||||||
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
|
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
|
||||||
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
|
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
|
||||||
|
|
||||||
|
[compat]
|
||||||
|
Base64 = "1.11.0"
|
||||||
|
JSON = "1.4.0"
|
||||||
|
|||||||
495
README.md
Normal file
495
README.md
Normal file
@@ -0,0 +1,495 @@
|
|||||||
|
# NATSBridge
|
||||||
|
|
||||||
|
A high-performance, bi-directional data bridge for **Julia** applications using NATS (Core & JetStream), implementing the Claim-Check pattern for large payloads.
|
||||||
|
|
||||||
|
[](https://opensource.org/licenses/MIT)
|
||||||
|
[](https://nats.io)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Table of Contents
|
||||||
|
|
||||||
|
- [Overview](#overview)
|
||||||
|
- [Features](#features)
|
||||||
|
- [Architecture](#architecture)
|
||||||
|
- [Installation](#installation)
|
||||||
|
- [Quick Start](#quick-start)
|
||||||
|
- [API Reference](#api-reference)
|
||||||
|
- [Payload Types](#payload-types)
|
||||||
|
- [Transport Strategies](#transport-strategies)
|
||||||
|
- [Examples](#examples)
|
||||||
|
- [Testing](#testing)
|
||||||
|
- [License](#license)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
NATSBridge enables seamless communication for Julia applications through NATS, with intelligent transport selection based on payload size:
|
||||||
|
|
||||||
|
| Transport | Payload Size | Method |
|
||||||
|
|-----------|--------------|--------|
|
||||||
|
| **Direct** | < 1MB | Sent directly via NATS (Base64 encoded) |
|
||||||
|
| **Link** | >= 1MB | Uploaded to HTTP file server, URL sent via NATS |
|
||||||
|
|
||||||
|
### Use Cases
|
||||||
|
|
||||||
|
- **Chat Applications**: Text, images, audio, video in a single message
|
||||||
|
- **File Transfer**: Efficient transfer of large files using claim-check pattern
|
||||||
|
- **Streaming Data**: Sensor data, telemetry, and analytics pipelines
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- ✅ **Bi-directional messaging** for Julia applications
|
||||||
|
- ✅ **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)
|
||||||
|
- ✅ **Exponential backoff** for reliable file server downloads
|
||||||
|
- ✅ **Correlation ID tracking** for message tracing
|
||||||
|
- ✅ **Reply-to support** for request-response patterns
|
||||||
|
- ✅ **JetStream support** for message replay and durability
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
### System Components
|
||||||
|
|
||||||
|
```
|
||||||
|
┌─────────────────────────────────────────────────────────────────────┐
|
||||||
|
│ NATSBridge Architecture │
|
||||||
|
├─────────────────────────────────────────────────────────────────────┤
|
||||||
|
│ ┌──────────────┐ │ │
|
||||||
|
│ │ Julia │ ▼ │
|
||||||
|
│ │ (NATS.jl) │ ┌─────────────────────────┐ │
|
||||||
|
│ └──────────────┘ │ NATS │ │
|
||||||
|
│ │ (Message Broker) │ │
|
||||||
|
│ └─────────────────────────┘ │
|
||||||
|
│ │ │
|
||||||
|
│ ▼ │
|
||||||
|
│ ┌──────────────────────┐ │
|
||||||
|
│ │ File Server │ │
|
||||||
|
│ │ (HTTP Upload/Get) │ │
|
||||||
|
│ └──────────────────────┘ │
|
||||||
|
└─────────────────────────────────────────────────────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
### Message Flow
|
||||||
|
|
||||||
|
1. **Sender** creates a message envelope with payloads
|
||||||
|
2. **NATSBridge** serializes and encodes payloads based on type
|
||||||
|
3. **Transport Decision**: Small payloads go directly to NATS, large payloads are uploaded to file server
|
||||||
|
4. **NATS** routes messages to subscribers
|
||||||
|
5. **Receiver** fetches payloads (from NATS or file server)
|
||||||
|
6. **NATSBridge** deserializes and decodes payloads
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
### Prerequisites
|
||||||
|
|
||||||
|
- **NATS Server** (v2.10+ recommended)
|
||||||
|
- **HTTP File Server** (optional, for payloads > 1MB)
|
||||||
|
|
||||||
|
### Julia
|
||||||
|
|
||||||
|
```julia
|
||||||
|
using Pkg
|
||||||
|
Pkg.add("NATS")
|
||||||
|
Pkg.add("https://git.yiem.cc/ton/NATSBridge")
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
|
||||||
|
### Step 1: Start NATS Server
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run -p 4222:4222 nats:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 2: Start HTTP File Server (Optional)
|
||||||
|
|
||||||
|
```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
|
||||||
|
|
||||||
|
#### Julia
|
||||||
|
|
||||||
|
```julia
|
||||||
|
using NATSBridge
|
||||||
|
|
||||||
|
# Send a text message
|
||||||
|
data = [("message", "Hello World", "text")]
|
||||||
|
env, env_json_str = NATSBridge.smartsend("/chat/room1", data; broker_url="nats://localhost:4222")
|
||||||
|
println("Message sent!")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 4: Receive Messages
|
||||||
|
|
||||||
|
#### Julia
|
||||||
|
|
||||||
|
```julia
|
||||||
|
using NATS, NATSBridge
|
||||||
|
|
||||||
|
# Configuration
|
||||||
|
const SUBJECT = "/chat/room1"
|
||||||
|
const NATS_URL = "nats://localhost:4222"
|
||||||
|
|
||||||
|
# Helper: Log with correlation ID
|
||||||
|
function log_trace(message)
|
||||||
|
timestamp = Dates.now()
|
||||||
|
println("[$timestamp] $message")
|
||||||
|
end
|
||||||
|
|
||||||
|
# Receiver: Listen for messages - msg comes from the callback
|
||||||
|
function test_receive()
|
||||||
|
conn = NATS.connect(NATS_URL)
|
||||||
|
NATS.subscribe(conn, SUBJECT) do msg
|
||||||
|
log_trace("Received message on $(msg.subject)")
|
||||||
|
|
||||||
|
# Receive and process message
|
||||||
|
env, env_json_str = NATSBridge.smartreceive(msg, fileserverDownloadHandler)
|
||||||
|
for (dataname, data, type) in env["payloads"]
|
||||||
|
println("Received $dataname: $data")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Keep listening for 120 seconds
|
||||||
|
sleep(120)
|
||||||
|
NATS.drain(conn)
|
||||||
|
end
|
||||||
|
|
||||||
|
test_receive()
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## API Reference
|
||||||
|
|
||||||
|
### smartsend
|
||||||
|
|
||||||
|
Sends data either directly via NATS or via a fileserver URL, depending on payload size.
|
||||||
|
|
||||||
|
#### Julia
|
||||||
|
|
||||||
|
```julia
|
||||||
|
using NATSBridge
|
||||||
|
|
||||||
|
env, env_json_str = NATSBridge.smartsend(
|
||||||
|
subject, # NATS subject
|
||||||
|
data::AbstractArray{Tuple{String, Any, String}}; # List of (dataname, data, type)
|
||||||
|
broker_url::String = "nats://localhost:4222",
|
||||||
|
fileserver_url = "http://localhost:8080",
|
||||||
|
fileserver_upload_handler::Function = plik_oneshot_upload,
|
||||||
|
size_threshold::Int = 1_000_000,
|
||||||
|
correlation_id::Union{String, Nothing} = nothing,
|
||||||
|
msg_purpose::String = "chat",
|
||||||
|
sender_name::String = "NATSBridge",
|
||||||
|
receiver_name::String = "",
|
||||||
|
receiver_id::String = "",
|
||||||
|
reply_to::String = "",
|
||||||
|
reply_to_msg_id::String = "",
|
||||||
|
is_publish::Bool = true, # Whether to automatically publish to NATS
|
||||||
|
NATS_connection::Union{NATS.Connection, Nothing} = nothing # Pre-existing NATS connection (optional, saves connection overhead)
|
||||||
|
)
|
||||||
|
# Returns: (msgEnvelope_v1, JSON string)
|
||||||
|
# - env: msgEnvelope_v1 object with all envelope metadata and payloads
|
||||||
|
# - env_json_str: JSON string representation of the envelope for publishing
|
||||||
|
```
|
||||||
|
|
||||||
|
### smartreceive
|
||||||
|
|
||||||
|
Receives and processes messages from NATS, handling both direct and link transport.
|
||||||
|
|
||||||
|
#### Julia
|
||||||
|
|
||||||
|
```julia
|
||||||
|
using NATSBridge
|
||||||
|
|
||||||
|
# Note: msg is a NATS.Msg object passed from the subscription callback
|
||||||
|
env = NATSBridge.smartreceive(
|
||||||
|
msg::NATS.Msg;
|
||||||
|
fileserver_download_handler::Function = _fetch_with_backoff,
|
||||||
|
max_retries::Int = 5,
|
||||||
|
base_delay::Int = 100,
|
||||||
|
max_delay::Int = 5000
|
||||||
|
)
|
||||||
|
# Returns: Dict with envelope metadata and payloads array
|
||||||
|
```
|
||||||
|
|
||||||
|
### publish_message
|
||||||
|
|
||||||
|
Publish a message to a NATS subject. This function is available in Julia with two overloads:
|
||||||
|
|
||||||
|
#### Julia
|
||||||
|
|
||||||
|
**Using broker URL (creates new connection):**
|
||||||
|
```julia
|
||||||
|
using NATSBridge, NATS
|
||||||
|
|
||||||
|
# Publish with URL - creates a new connection
|
||||||
|
NATSBridge.publish_message(
|
||||||
|
"nats://localhost:4222", # broker_url
|
||||||
|
"/chat/room1", # subject
|
||||||
|
"{\"correlation_id\":\"abc123\"}", # message
|
||||||
|
"abc123" # correlation_id
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Using pre-existing connection (saves connection overhead):**
|
||||||
|
```julia
|
||||||
|
using NATSBridge, NATS
|
||||||
|
|
||||||
|
# Create connection once and reuse
|
||||||
|
conn = NATS.connect("nats://localhost:4222")
|
||||||
|
NATSBridge.publish_message(conn, "/chat/room1", "{\"correlation_id\":\"abc123\"}", "abc123")
|
||||||
|
# Connection is automatically drained after publish
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Payload Types
|
||||||
|
|
||||||
|
| Type | Description | Serialization |
|
||||||
|
|------|-------------|---------------|
|
||||||
|
| `text` | Plain text strings | UTF-8 bytes |
|
||||||
|
| `dictionary` | JSON-serializable dictionaries | JSON |
|
||||||
|
| `table` | Tabular data (DataFrames, arrays) | Apache Arrow IPC |
|
||||||
|
| `image` | Image data (PNG, JPG) | Raw bytes |
|
||||||
|
| `audio` | Audio data (WAV, MP3) | Raw bytes |
|
||||||
|
| `video` | Video data (MP4, AVI) | Raw bytes |
|
||||||
|
| `binary` | Generic binary data | Raw bytes |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Transport Strategies
|
||||||
|
|
||||||
|
### Direct Transport (Payloads < 1MB)
|
||||||
|
|
||||||
|
Small payloads are sent directly via NATS with Base64 encoding.
|
||||||
|
|
||||||
|
#### Julia
|
||||||
|
```julia
|
||||||
|
data = [("message", "Hello", "text")]
|
||||||
|
smartsend("/topic", data)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Link Transport (Payloads >= 1MB)
|
||||||
|
|
||||||
|
Large payloads are uploaded to an HTTP file server.
|
||||||
|
|
||||||
|
#### Julia
|
||||||
|
```julia
|
||||||
|
data = [("file", large_data, "binary")]
|
||||||
|
smartsend("/topic", data; fileserver_url="http://localhost:8080")
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
### Example 1: Chat with Mixed Content
|
||||||
|
|
||||||
|
Send text, small image, and large file in one message.
|
||||||
|
|
||||||
|
#### Julia
|
||||||
|
```julia
|
||||||
|
using NATSBridge
|
||||||
|
|
||||||
|
data = [
|
||||||
|
("message_text", "Hello!", "text"),
|
||||||
|
("user_avatar", image_data, "image"),
|
||||||
|
("large_document", large_file_data, "binary")
|
||||||
|
]
|
||||||
|
|
||||||
|
env, env_json_str = NATSBridge.smartsend("/chat/room1", data; fileserver_url="http://localhost:8080")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example 2: Dictionary Exchange
|
||||||
|
|
||||||
|
Send configuration data between platforms.
|
||||||
|
|
||||||
|
#### Julia
|
||||||
|
```julia
|
||||||
|
using NATSBridge
|
||||||
|
|
||||||
|
config = Dict(
|
||||||
|
"wifi_ssid" => "MyNetwork",
|
||||||
|
"wifi_password" => "password123",
|
||||||
|
"update_interval" => 60
|
||||||
|
)
|
||||||
|
|
||||||
|
data = [("config", config, "dictionary")]
|
||||||
|
env, env_json_str = NATSBridge.smartsend("/device/config", data)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example 3: Table Data (Arrow IPC)
|
||||||
|
|
||||||
|
Send tabular data using Apache Arrow IPC format.
|
||||||
|
|
||||||
|
#### Julia
|
||||||
|
```julia
|
||||||
|
using NATSBridge
|
||||||
|
using DataFrames
|
||||||
|
|
||||||
|
df = DataFrame(
|
||||||
|
id = [1, 2, 3],
|
||||||
|
name = ["Alice", "Bob", "Charlie"],
|
||||||
|
score = [95, 88, 92]
|
||||||
|
)
|
||||||
|
|
||||||
|
data = [("students", df, "table")]
|
||||||
|
env, env_json_str = NATSBridge.smartsend("/data/analysis", data)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example 4: Request-Response Pattern with Envelope JSON
|
||||||
|
|
||||||
|
Bi-directional communication with reply-to support. The `smartsend` function now returns both the envelope object and a JSON string that can be published directly.
|
||||||
|
|
||||||
|
#### Julia (Requester)
|
||||||
|
```julia
|
||||||
|
using NATSBridge
|
||||||
|
|
||||||
|
env, env_json_str = NATSBridge.smartsend(
|
||||||
|
"/device/command",
|
||||||
|
[("command", Dict("action" => "read_sensor"), "dictionary")];
|
||||||
|
broker_url="nats://localhost:4222",
|
||||||
|
reply_to="/device/response"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Julia (Responder)
|
||||||
|
```julia
|
||||||
|
using NATS, NATSBridge
|
||||||
|
|
||||||
|
# Configuration
|
||||||
|
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 = NATSBridge.smartreceive(msg, fileserver_download_handler=_fetch_with_backoff)
|
||||||
|
|
||||||
|
# Extract reply_to from the envelope metadata
|
||||||
|
reply_to = env["reply_to"]
|
||||||
|
|
||||||
|
for (dataname, data, type) in env["payloads"]
|
||||||
|
if dataname == "command" && data["action"] == "read_sensor"
|
||||||
|
response = Dict("sensor_id" => "sensor-001", "value" => 42.5)
|
||||||
|
# Send response to the reply_to subject from the request
|
||||||
|
if !isempty(reply_to)
|
||||||
|
smartsend(reply_to, [("data", response, "dictionary")])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
sleep(120)
|
||||||
|
NATS.drain(conn)
|
||||||
|
end
|
||||||
|
|
||||||
|
test_responder()
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example 5: IoT Device Sensor Data
|
||||||
|
|
||||||
|
IoT device sending sensor data.
|
||||||
|
|
||||||
|
#### Julia (Receiver)
|
||||||
|
```julia
|
||||||
|
using NATS, NATSBridge
|
||||||
|
|
||||||
|
# Configuration
|
||||||
|
const SUBJECT = "/device/sensors"
|
||||||
|
const NATS_URL = "nats://localhost:4222"
|
||||||
|
|
||||||
|
function test_receiver()
|
||||||
|
conn = NATS.connect(NATS_URL)
|
||||||
|
NATS.subscribe(conn, SUBJECT) do msg
|
||||||
|
env, env_json_str = NATSBridge.smartreceive(msg, fileserverDownloadHandler)
|
||||||
|
for (dataname, data, type) in env["payloads"]
|
||||||
|
if dataname == "temperature"
|
||||||
|
println("Temperature: $data")
|
||||||
|
elseif dataname == "humidity"
|
||||||
|
println("Humidity: $data")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
sleep(120)
|
||||||
|
NATS.drain(conn)
|
||||||
|
end
|
||||||
|
|
||||||
|
test_receiver()
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
Run the test scripts to verify functionality:
|
||||||
|
|
||||||
|
### Julia
|
||||||
|
|
||||||
|
```julia
|
||||||
|
# Text message exchange
|
||||||
|
julia test/test_julia_text_sender.jl
|
||||||
|
julia test/test_julia_text_receiver.jl
|
||||||
|
|
||||||
|
# Dictionary exchange
|
||||||
|
julia test/test_julia_dict_sender.jl
|
||||||
|
julia test/test_julia_dict_receiver.jl
|
||||||
|
|
||||||
|
# File transfer
|
||||||
|
julia test/test_julia_file_sender.jl
|
||||||
|
julia test/test_julia_file_receiver.jl
|
||||||
|
|
||||||
|
# Mixed payload types
|
||||||
|
julia test/test_julia_mix_payloads_sender.jl
|
||||||
|
julia test/test_julia_mix_payloads_receiver.jl
|
||||||
|
|
||||||
|
# Table exchange
|
||||||
|
julia test/test_julia_table_sender.jl
|
||||||
|
julia test/test_julia_table_receiver.jl
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2026 NATSBridge Contributors
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
# Architecture Documentation: Bi-Directional Data Bridge (Julia ↔ JavaScript)
|
# Architecture Documentation: Bi-Directional Data Bridge
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
This document describes the architecture for a high-performance, bi-directional data bridge between a Julia service and a JavaScript (Node.js) service using NATS (Core & JetStream), implementing the Claim-Check pattern for large payloads.
|
This document describes the architecture for a high-performance, bi-directional data bridge for **Julia** applications using NATS (Core & JetStream), implementing the Claim-Check pattern for large payloads.
|
||||||
|
|
||||||
|
The system enables seamless communication for Julia applications:
|
||||||
|
- **Julia** messaging with NATS
|
||||||
|
|
||||||
### File Server Handler Architecture
|
### File Server Handler Architecture
|
||||||
|
|
||||||
@@ -12,16 +15,16 @@ The system uses **handler functions** to abstract file server operations, allowi
|
|||||||
|
|
||||||
```julia
|
```julia
|
||||||
# Upload handler - uploads data to file server and returns URL
|
# Upload handler - uploads data to file server and returns URL
|
||||||
# The handler is passed to smartsend as fileserverUploadHandler parameter
|
# The handler is passed to smartsend as fileserver_upload_handler parameter
|
||||||
# It receives: (fileserver_url::String, dataname::String, data::Vector{UInt8})
|
# It receives: (fileserver_url::String, dataname::String, data::Vector{UInt8})
|
||||||
# Returns: Dict{String, Any} with keys: "status", "uploadid", "fileid", "url"
|
# Returns: Dict{String, Any} with keys: "status", "uploadid", "fileid", "url"
|
||||||
fileserverUploadHandler(fileserver_url::String, dataname::String, data::Vector{UInt8})::Dict{String, Any}
|
fileserver_upload_handler(fileserver_url::String, dataname::String, data::Vector{UInt8})::Dict{String, Any}
|
||||||
|
|
||||||
# Download handler - fetches data from file server URL with exponential backoff
|
# Download handler - fetches data from file server URL with exponential backoff
|
||||||
# The handler is passed to smartreceive as fileserverDownloadHandler parameter
|
# The handler is passed to smartreceive as fileserver_download_handler parameter
|
||||||
# It receives: (url::String, max_retries::Int, base_delay::Int, max_delay::Int, correlation_id::String)
|
# It receives: (url::String, max_retries::Int, base_delay::Int, max_delay::Int, correlation_id::String)
|
||||||
# Returns: Vector{UInt8} (the downloaded data)
|
# Returns: Vector{UInt8} (the downloaded data)
|
||||||
fileserverDownloadHandler(url::String, max_retries::Int, base_delay::Int, max_delay::Int, correlation_id::String)::Vector{UInt8}
|
fileserver_download_handler(url::String, max_retries::Int, base_delay::Int, max_delay::Int, correlation_id::String)::Vector{UInt8}
|
||||||
```
|
```
|
||||||
|
|
||||||
This design allows the system to support multiple file server backends without changing the core messaging logic.
|
This design allows the system to support multiple file server backends without changing the core messaging logic.
|
||||||
@@ -35,8 +38,24 @@ The system uses a **standardized list-of-tuples format** for all payload operati
|
|||||||
# Input format for smartsend (always a list of tuples with type info)
|
# Input format for smartsend (always a list of tuples with type info)
|
||||||
[(dataname1, data1, type1), (dataname2, data2, type2), ...]
|
[(dataname1, data1, type1), (dataname2, data2, type2), ...]
|
||||||
|
|
||||||
# Output format for smartreceive (always returns a list of tuples)
|
# Output format for smartreceive (returns a dictionary-like object with payloads field containing list of tuples)
|
||||||
[(dataname1, data1, type1), (dataname2, data2, type2), ...]
|
# Returns: Dict-like object with envelope metadata and payloads field containing Vector{Tuple{String, Any, String}}
|
||||||
|
# {
|
||||||
|
# "correlation_id": "...",
|
||||||
|
# "msg_id": "...",
|
||||||
|
# "timestamp": "...",
|
||||||
|
# "send_to": "...",
|
||||||
|
# "msg_purpose": "...",
|
||||||
|
# "sender_name": "...",
|
||||||
|
# "sender_id": "...",
|
||||||
|
# "receiver_name": "...",
|
||||||
|
# "receiver_id": "...",
|
||||||
|
# "reply_to": "...",
|
||||||
|
# "reply_to_msg_id": "...",
|
||||||
|
# "broker_url": "...",
|
||||||
|
# "metadata": {...},
|
||||||
|
# "payloads": [(dataname1, data1, type1), (dataname2, data2, type2), ...]
|
||||||
|
# }
|
||||||
```
|
```
|
||||||
|
|
||||||
**Supported Types:**
|
**Supported Types:**
|
||||||
@@ -57,17 +76,16 @@ This design allows per-payload type specification, enabling **mixed-content mess
|
|||||||
smartsend(
|
smartsend(
|
||||||
"/test",
|
"/test",
|
||||||
[("dataname1", data1, "dictionary")], # List with one tuple (data, type)
|
[("dataname1", data1, "dictionary")], # List with one tuple (data, type)
|
||||||
nats_url="nats://localhost:4222",
|
broker_url="nats://localhost:4222",
|
||||||
fileserverUploadHandler=plik_oneshot_upload,
|
fileserver_upload_handler=plik_oneshot_upload
|
||||||
metadata=user_provided_envelope_level_metadata
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Multiple payloads in one message with different types
|
# Multiple payloads in one message with different types
|
||||||
smartsend(
|
smartsend(
|
||||||
"/test",
|
"/test",
|
||||||
[("dataname1", data1, "dictionary"), ("dataname2", data2, "table")],
|
[("dataname1", data1, "dictionary"), ("dataname2", data2, "table")],
|
||||||
nats_url="nats://localhost:4222",
|
broker_url="nats://localhost:4222",
|
||||||
fileserverUploadHandler=plik_oneshot_upload
|
fileserver_upload_handler=plik_oneshot_upload
|
||||||
)
|
)
|
||||||
|
|
||||||
# Mixed content (e.g., chat with text, image, audio)
|
# Mixed content (e.g., chat with text, image, audio)
|
||||||
@@ -78,12 +96,14 @@ smartsend(
|
|||||||
("user_image", image_data, "image"),
|
("user_image", image_data, "image"),
|
||||||
("audio_clip", audio_data, "audio")
|
("audio_clip", audio_data, "audio")
|
||||||
],
|
],
|
||||||
nats_url="nats://localhost:4222"
|
broker_url="nats://localhost:4222"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Receive always returns a list
|
# Receive returns a dictionary envelope with all metadata and deserialized payloads
|
||||||
payloads = smartreceive(msg, fileserverDownloadHandler, max_retries, base_delay, max_delay)
|
env = smartreceive(msg; fileserver_download_handler=_fetch_with_backoff, max_retries=5, base_delay=100, max_delay=5000)
|
||||||
# payloads = [("dataname1", data1, type1), ("dataname2", data2, type2), ...]
|
# env["payloads"] = [("dataname1", data1, type1), ("dataname2", data2, type2), ...]
|
||||||
|
# env["correlation_id"], env["msg_id"], etc.
|
||||||
|
# env is a dictionary containing envelope metadata and payloads field
|
||||||
```
|
```
|
||||||
|
|
||||||
## Architecture Diagram
|
## Architecture Diagram
|
||||||
@@ -91,8 +111,7 @@ payloads = smartreceive(msg, fileserverDownloadHandler, max_retries, base_delay,
|
|||||||
```mermaid
|
```mermaid
|
||||||
flowchart TD
|
flowchart TD
|
||||||
subgraph Client
|
subgraph Client
|
||||||
JS[JavaScript Client]
|
App[Julia Application]
|
||||||
JSApp[Application Logic]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
subgraph Server
|
subgraph Server
|
||||||
@@ -101,14 +120,12 @@ flowchart TD
|
|||||||
FileServer[HTTP File Server]
|
FileServer[HTTP File Server]
|
||||||
end
|
end
|
||||||
|
|
||||||
JS -->|Control/Small Data| JSApp
|
App -->|NATS| NATS
|
||||||
JSApp -->|NATS| NATS
|
|
||||||
NATS -->|NATS| Julia
|
NATS -->|NATS| Julia
|
||||||
Julia -->|NATS| NATS
|
Julia -->|NATS| NATS
|
||||||
Julia -->|HTTP POST| FileServer
|
Julia -->|HTTP POST| FileServer
|
||||||
JS -->|HTTP GET| FileServer
|
|
||||||
|
|
||||||
style JS fill:#e1f5fe
|
style App fill:#e8f5e9
|
||||||
style Julia fill:#e8f5e9
|
style Julia fill:#e8f5e9
|
||||||
style NATS fill:#fff3e0
|
style NATS fill:#fff3e0
|
||||||
style FileServer fill:#f3e5f5
|
style FileServer fill:#f3e5f5
|
||||||
@@ -116,48 +133,48 @@ flowchart TD
|
|||||||
|
|
||||||
## System Components
|
## System Components
|
||||||
|
|
||||||
### 1. msgEnvelope_v1 - Message Envelope
|
### 1. msg_envelope_v1 - Message Envelope
|
||||||
|
|
||||||
The `msgEnvelope_v1` structure provides a comprehensive message format for bidirectional communication between Julia and JavaScript services.
|
The `msg_envelope_v1` structure provides a comprehensive message format for bidirectional communication in Julia applications.
|
||||||
|
|
||||||
**Julia Structure:**
|
**Julia Structure:**
|
||||||
```julia
|
```julia
|
||||||
struct msgEnvelope_v1
|
struct msg_envelope_v1
|
||||||
correlationId::String # Unique identifier to track messages across systems
|
correlation_id::String # Unique identifier to track messages across systems
|
||||||
msgId::String # This message id
|
msg_id::String # This message id
|
||||||
timestamp::String # Message published timestamp
|
timestamp::String # Message published timestamp
|
||||||
|
|
||||||
sendTo::String # Topic/subject the sender sends to
|
send_to::String # Topic/subject the sender sends to
|
||||||
msgPurpose::String # Purpose of this message (ACK | NACK | updateStatus | shutdown | ...)
|
msg_purpose::String # Purpose of this message (ACK | NACK | updateStatus | shutdown | ...)
|
||||||
senderName::String # Sender name (e.g., "agent-wine-web-frontend")
|
sender_name::String # Sender name (e.g., "agent-wine-web-frontend")
|
||||||
senderId::String # Sender id (uuid4)
|
sender_id::String # Sender id (uuid4)
|
||||||
receiverName::String # Message receiver name (e.g., "agent-backend")
|
receiver_name::String # Message receiver name (e.g., "agent-backend")
|
||||||
receiverId::String # Message receiver id (uuid4 or nothing for broadcast)
|
receiver_id::String # Message receiver id (uuid4 or nothing for broadcast)
|
||||||
replyTo::String # Topic to reply to
|
reply_to::String # Topic to reply to
|
||||||
replyToMsgId::String # Message id this message is replying to
|
reply_to_msg_id::String # Message id this message is replying to
|
||||||
brokerURL::String # NATS server address
|
broker_url::String # NATS server address
|
||||||
|
|
||||||
metadata::Dict{String, Any}
|
metadata::Dict{String, Any}
|
||||||
payloads::AbstractArray{msgPayload_v1} # Multiple payloads stored here
|
payloads::Vector{msg_payload_v1} # Multiple payloads stored here
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
**JSON Schema:**
|
**JSON Schema:**
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"correlationId": "uuid-v4-string",
|
"correlation_id": "uuid-v4-string",
|
||||||
"msgId": "uuid-v4-string",
|
"msg_id": "uuid-v4-string",
|
||||||
"timestamp": "2024-01-15T10:30:00Z",
|
"timestamp": "2024-01-15T10:30:00Z",
|
||||||
|
|
||||||
"sendTo": "topic/subject",
|
"send_to": "topic/subject",
|
||||||
"msgPurpose": "ACK | NACK | updateStatus | shutdown | chat",
|
"msg_purpose": "ACK | NACK | updateStatus | shutdown | chat",
|
||||||
"senderName": "agent-wine-web-frontend",
|
"sender_name": "agent-wine-web-frontend",
|
||||||
"senderId": "uuid4",
|
"sender_id": "uuid4",
|
||||||
"receiverName": "agent-backend",
|
"receiver_name": "agent-backend",
|
||||||
"receiverId": "uuid4",
|
"receiver_id": "uuid4",
|
||||||
"replyTo": "topic",
|
"reply_to": "topic",
|
||||||
"replyToMsgId": "uuid4",
|
"reply_to_msg_id": "uuid4",
|
||||||
"brokerURL": "nats://localhost:4222",
|
"broker_url": "nats://localhost:4222",
|
||||||
|
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
|
||||||
@@ -167,7 +184,7 @@ end
|
|||||||
{
|
{
|
||||||
"id": "uuid4",
|
"id": "uuid4",
|
||||||
"dataname": "login_image",
|
"dataname": "login_image",
|
||||||
"type": "image",
|
"payload_type": "image",
|
||||||
"transport": "direct",
|
"transport": "direct",
|
||||||
"encoding": "base64",
|
"encoding": "base64",
|
||||||
"size": 15433,
|
"size": 15433,
|
||||||
@@ -179,7 +196,7 @@ end
|
|||||||
{
|
{
|
||||||
"id": "uuid4",
|
"id": "uuid4",
|
||||||
"dataname": "large_data",
|
"dataname": "large_data",
|
||||||
"type": "table",
|
"payload_type": "table",
|
||||||
"transport": "link",
|
"transport": "link",
|
||||||
"encoding": "none",
|
"encoding": "none",
|
||||||
"size": 524288,
|
"size": 524288,
|
||||||
@@ -192,16 +209,16 @@ end
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### 2. msgPayload_v1 - Payload Structure
|
### 2. msg_payload_v1 - Payload Structure
|
||||||
|
|
||||||
The `msgPayload_v1` structure provides flexible payload handling for various data types.
|
The `msg_payload_v1` structure provides flexible payload handling for various data types.
|
||||||
|
|
||||||
**Julia Structure:**
|
**Julia Structure:**
|
||||||
```julia
|
```julia
|
||||||
struct msgPayload_v1
|
struct msg_payload_v1
|
||||||
id::String # Id of this payload (e.g., "uuid4")
|
id::String # Id of this payload (e.g., "uuid4")
|
||||||
dataname::String # Name of this payload (e.g., "login_image")
|
dataname::String # Name of this payload (e.g., "login_image")
|
||||||
type::String # "text | dictionary | table | image | audio | video | binary"
|
payload_type::String # "text | dictionary | table | image | audio | video | binary"
|
||||||
transport::String # "direct | link"
|
transport::String # "direct | link"
|
||||||
encoding::String # "none | json | base64 | arrow-ipc"
|
encoding::String # "none | json | base64 | arrow-ipc"
|
||||||
size::Integer # Data size in bytes
|
size::Integer # Data size in bytes
|
||||||
@@ -222,15 +239,15 @@ end
|
|||||||
┌─────────────────────────────────────────────────────────────┐
|
┌─────────────────────────────────────────────────────────────┐
|
||||||
│ smartsend Function │
|
│ smartsend Function │
|
||||||
│ Accepts: [(dataname1, data1, type1), ...] │
|
│ Accepts: [(dataname1, data1, type1), ...] │
|
||||||
│ (No standalone type parameter - type per payload) │
|
│ (Type is per payload, not standalone) │
|
||||||
└─────────────────────────────────────────────────────────────┘
|
└─────────────────────────────────────────────────────────────┘
|
||||||
│
|
│
|
||||||
▼
|
▼
|
||||||
┌─────────────────────────────────────────────────────────────┐
|
┌─────────────────────────────────────────────────────────────┐
|
||||||
│ For each payload: │
|
│ For each payload: │
|
||||||
│ 1. Extract type from tuple │
|
│ 1. Extract type from tuple │
|
||||||
│ 2. Serialize based on type │
|
│ 2. Serialize based on type │
|
||||||
│ 3. Check payload size │
|
│ 3. Check payload size │
|
||||||
└─────────────────────────────────────────────────────────────┘
|
└─────────────────────────────────────────────────────────────┘
|
||||||
│
|
│
|
||||||
┌────────────────┴─-────────────────┐
|
┌────────────────┴─-────────────────┐
|
||||||
@@ -254,14 +271,14 @@ end
|
|||||||
```mermaid
|
```mermaid
|
||||||
graph TD
|
graph TD
|
||||||
subgraph JuliaModule
|
subgraph JuliaModule
|
||||||
smartsendJulia[smartsend Julia]
|
JuliaSmartSend[smartsend]
|
||||||
SizeCheck[Size Check]
|
SizeCheck[Size Check]
|
||||||
DirectPath[Direct Path]
|
DirectPath[Direct Path]
|
||||||
LinkPath[Link Path]
|
LinkPath[Link Path]
|
||||||
HTTPClient[HTTP Client]
|
HTTPClient[HTTP Client]
|
||||||
end
|
end
|
||||||
|
|
||||||
smartsendJulia --> SizeCheck
|
JuliaSmartSend --> SizeCheck
|
||||||
SizeCheck -->|< 1MB| DirectPath
|
SizeCheck -->|< 1MB| DirectPath
|
||||||
SizeCheck -->|>= 1MB| LinkPath
|
SizeCheck -->|>= 1MB| LinkPath
|
||||||
LinkPath --> HTTPClient
|
LinkPath --> HTTPClient
|
||||||
@@ -269,24 +286,6 @@ graph TD
|
|||||||
style JuliaModule fill:#c5e1a5
|
style JuliaModule fill:#c5e1a5
|
||||||
```
|
```
|
||||||
|
|
||||||
### 5. JavaScript Module Architecture
|
|
||||||
|
|
||||||
```mermaid
|
|
||||||
graph TD
|
|
||||||
subgraph JSModule
|
|
||||||
smartsendJS[smartsend JS]
|
|
||||||
smartreceiveJS[smartreceive JS]
|
|
||||||
JetStreamConsumer[JetStream Pull Consumer]
|
|
||||||
ApacheArrow[Apache Arrow]
|
|
||||||
end
|
|
||||||
|
|
||||||
smartsendJS --> NATS
|
|
||||||
smartreceiveJS --> JetStreamConsumer
|
|
||||||
JetStreamConsumer --> ApacheArrow
|
|
||||||
|
|
||||||
style JSModule fill:#f3e5f5
|
|
||||||
```
|
|
||||||
|
|
||||||
## Implementation Details
|
## Implementation Details
|
||||||
|
|
||||||
### Julia Implementation
|
### Julia Implementation
|
||||||
@@ -303,13 +302,47 @@ graph TD
|
|||||||
```julia
|
```julia
|
||||||
function smartsend(
|
function smartsend(
|
||||||
subject::String,
|
subject::String,
|
||||||
data::AbstractArray{Tuple{String, Any, String}}; # No standalone type parameter
|
data::AbstractArray{Tuple{String, Any, String}, 1}; # List of (dataname, data, type) tuples
|
||||||
nats_url::String = "nats://localhost:4222",
|
broker_url::String = DEFAULT_BROKER_URL, # NATS server URL
|
||||||
fileserverUploadHandler::Function = plik_oneshot_upload,
|
fileserver_url = DEFAULT_FILESERVER_URL,
|
||||||
size_threshold::Int = 1_000_000 # 1MB
|
fileserver_upload_handler::Function = plik_oneshot_upload,
|
||||||
|
size_threshold::Int = DEFAULT_SIZE_THRESHOLD,
|
||||||
|
correlation_id::Union{String, Nothing} = nothing,
|
||||||
|
msg_purpose::String = "chat",
|
||||||
|
sender_name::String = "NATSBridge",
|
||||||
|
receiver_name::String = "",
|
||||||
|
receiver_id::String = "",
|
||||||
|
reply_to::String = "",
|
||||||
|
reply_to_msg_id::String = "",
|
||||||
|
is_publish::Bool = true, # Whether to automatically publish to NATS
|
||||||
|
NATS_connection::Union{NATS.Connection, Nothing} = nothing # Pre-existing NATS connection (optional, saves connection overhead)
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**Keyword Parameter - NATS_connection:**
|
||||||
|
- `NATS_connection::Union{NATS.Connection, Nothing} = nothing` - Pre-existing NATS connection. When provided, `smartsend` uses this connection instead of creating a new one, avoiding the overhead of connection establishment. This is useful for high-frequency publishing scenarios where connection reuse provides performance benefits.
|
||||||
|
|
||||||
|
**Connection Handling Logic:**
|
||||||
|
```julia
|
||||||
|
if is_publish == false
|
||||||
|
# skip publish a message
|
||||||
|
elseif is_publish == true && NATS_connection === nothing
|
||||||
|
publish_message(broker_url, subject, env_json_str, cid) # Creates new connection
|
||||||
|
elseif is_publish == true && NATS_connection !== nothing
|
||||||
|
publish_message(NATS_connection, subject, env_json_str, cid) # Uses provided connection
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
**Return Value:**
|
||||||
|
- Returns a tuple `(env, env_json_str)` where:
|
||||||
|
- `env::msg_envelope_v1` - The envelope object containing all metadata and payloads
|
||||||
|
- `env_json_str::String` - JSON string representation of the envelope for publishing
|
||||||
|
|
||||||
|
**Options:**
|
||||||
|
- `is_publish::Bool = true` - When `true` (default), the message is automatically published to NATS. When `false`, the function returns the envelope and JSON string without publishing, allowing manual publishing via NATS request-reply pattern.
|
||||||
|
|
||||||
|
The envelope object can be accessed directly for programmatic use, while the JSON string can be published directly to NATS using the request-reply pattern.
|
||||||
|
|
||||||
**Input Format:**
|
**Input Format:**
|
||||||
- `data::AbstractArray{Tuple{String, Any, String}}` - **Must be a list of (dataname, data, type) tuples**: `[("dataname1", data1, "type1"), ("dataname2", data2, "type2"), ...]`
|
- `data::AbstractArray{Tuple{String, Any, String}}` - **Must be a list of (dataname, data, type) tuples**: `[("dataname1", data1, "type1"), ("dataname2", data2, "type2"), ...]`
|
||||||
- Even for single payloads: `[(dataname1, data1, "type1")]`
|
- Even for single payloads: `[(dataname1, data1, "type1")]`
|
||||||
@@ -326,8 +359,8 @@ function smartsend(
|
|||||||
|
|
||||||
```julia
|
```julia
|
||||||
function smartreceive(
|
function smartreceive(
|
||||||
msg::NATS.Message,
|
msg::NATS.Msg;
|
||||||
fileserverDownloadHandler::Function;
|
fileserver_download_handler::Function = _fetch_with_backoff,
|
||||||
max_retries::Int = 5,
|
max_retries::Int = 5,
|
||||||
base_delay::Int = 100,
|
base_delay::Int = 100,
|
||||||
max_delay::Int = 5000
|
max_delay::Int = 5000
|
||||||
@@ -336,86 +369,82 @@ function smartreceive(
|
|||||||
# Iterate through all payloads
|
# Iterate through all payloads
|
||||||
# For each payload: check transport type
|
# For each payload: check transport type
|
||||||
# If direct: decode Base64 payload
|
# If direct: decode Base64 payload
|
||||||
# If link: fetch from URL with exponential backoff using fileserverDownloadHandler
|
# If link: fetch from URL with exponential backoff using fileserver_download_handler
|
||||||
# Deserialize payload based on type
|
# Deserialize payload based on type
|
||||||
# Return list of (dataname, data, type) tuples
|
# Return envelope dictionary with all metadata and deserialized payloads
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
**Output Format:**
|
**Output Format:**
|
||||||
- Always returns a list of tuples: `[(dataname1, data1, type1), (dataname2, data2, type2), ...]`
|
- Returns a JSON object (dictionary) containing all envelope fields:
|
||||||
- Even for single payloads: `[(dataname1, data1, type1)]`
|
- `correlation_id`, `msg_id`, `timestamp`, `send_to`, `msg_purpose`, `sender_name`, `sender_id`, `receiver_name`, `receiver_id`, `reply_to`, `reply_to_msg_id`, `broker_url`
|
||||||
|
- `metadata` - Message-level metadata dictionary
|
||||||
|
- `payloads` - List of tuples, each containing `(dataname, data, type)` with deserialized payload data
|
||||||
|
|
||||||
**Process Flow:**
|
**Process Flow:**
|
||||||
1. Parse the JSON envelope to extract the `payloads` array
|
1. Parse the JSON envelope to extract all fields
|
||||||
2. Iterate through each payload in `payloads`
|
2. Iterate through each payload in `payloads`
|
||||||
3. For each payload:
|
3. For each payload:
|
||||||
- Determine transport type (`direct` or `link`)
|
- Determine transport type (`direct` or `link`)
|
||||||
- If `direct`: decode Base64 data from the message
|
- If `direct`: decode Base64 data from the message
|
||||||
- If `link`: fetch data from URL using exponential backoff (via `fileserverDownloadHandler`)
|
- If `link`: fetch data from URL using exponential backoff (via `fileserver_download_handler`)
|
||||||
- Deserialize based on payload type (`dictionary`, `table`, `binary`, etc.)
|
- Deserialize based on payload type (`dictionary`, `table`, `binary`, etc.)
|
||||||
4. Return list of `(dataname, data, type)` tuples
|
4. Return envelope dictionary with `payloads` field containing list of `(dataname, data, type)` tuples
|
||||||
|
|
||||||
**Note:** The `fileserverDownloadHandler` receives `(url::String, max_retries::Int, base_delay::Int, max_delay::Int, correlation_id::String)` and returns `Vector{UInt8}`.
|
**Note:** The `fileserver_download_handler` receives `(url::String, max_retries::Int, base_delay::Int, max_delay::Int, correlation_id::String)` and returns `Vector{UInt8}`.
|
||||||
|
|
||||||
### JavaScript Implementation
|
#### publish_message Function
|
||||||
|
|
||||||
#### Dependencies
|
The `publish_message` function provides two overloads for publishing messages to NATS:
|
||||||
- `nats.js` - Core NATS functionality
|
|
||||||
- `apache-arrow` - Arrow IPC serialization
|
|
||||||
- `uuid` - Correlation ID generation
|
|
||||||
|
|
||||||
#### smartsend Function
|
**Overload 1 - URL-based publishing (creates new connection):**
|
||||||
|
```julia
|
||||||
```javascript
|
function publish_message(broker_url::String, subject::String, message::String, correlation_id::String)
|
||||||
async function smartsend(subject, data, options = {})
|
conn = NATS.connect(broker_url) # Create NATS connection
|
||||||
// data format: [(dataname, data, type), ...]
|
publish_message(conn, subject, message, correlation_id)
|
||||||
// options object should include:
|
end
|
||||||
// - natsUrl: NATS server URL
|
|
||||||
// - fileserverUrl: base URL of the file server
|
|
||||||
// - sizeThreshold: threshold in bytes for transport selection
|
|
||||||
// - correlationId: optional correlation ID for tracing
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Input Format:**
|
**Overload 2 - Connection-based publishing (uses pre-existing connection):**
|
||||||
- `data` - **Must be a list of (dataname, data, type) tuples**: `[(dataname1, data1, "type1"), (dataname2, data2, "type2"), ...]`
|
```julia
|
||||||
- Even for single payloads: `[(dataname1, data1, "type1")]`
|
function publish_message(conn::NATS.Connection, subject::String, message::String, correlation_id::String)
|
||||||
- Each payload can have a different type, enabling mixed-content messages
|
try
|
||||||
|
NATS.publish(conn, subject, message) # Publish message to NATS
|
||||||
**Flow:**
|
log_trace(correlation_id, "Message published to $subject") # Log successful publish
|
||||||
1. Iterate through the list of (dataname, data, type) tuples
|
finally
|
||||||
2. For each payload: extract the type from the tuple and serialize accordingly
|
NATS.drain(conn) # Ensure connection is closed properly
|
||||||
3. Check payload size
|
end
|
||||||
4. If < threshold: publish directly to NATS
|
end
|
||||||
5. If >= threshold: upload to HTTP server, publish NATS with URL
|
|
||||||
|
|
||||||
#### smartreceive Handler
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
async function smartreceive(msg, options = {})
|
|
||||||
// options object should include:
|
|
||||||
// - fileserverDownloadHandler: function to fetch data from file server URL
|
|
||||||
// - max_retries: maximum retry attempts for fetching URL
|
|
||||||
// - base_delay: initial delay for exponential backoff in ms
|
|
||||||
// - max_delay: maximum delay for exponential backoff in ms
|
|
||||||
// - correlationId: optional correlation ID for tracing
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Process Flow:**
|
**Use Case:** Use the connection-based overload 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. This is a Julia-specific optimization that leverages function overloading.
|
||||||
1. Parse the JSON envelope to extract the `payloads` array
|
|
||||||
2. Iterate through each payload in `payloads`
|
**Integration with smartsend:**
|
||||||
3. For each payload:
|
```julia
|
||||||
- Determine transport type (`direct` or `link`)
|
# When NATS_connection is provided to smartsend, it uses the connection-based publish_message
|
||||||
- If `direct`: decode Base64 data from the message
|
env, env_json_str = smartsend(
|
||||||
- If `link`: fetch data from URL using exponential backoff
|
"my.subject",
|
||||||
- Deserialize based on payload type (`dictionary`, `table`, `binary`, etc.)
|
[("data", payload_data, "type")],
|
||||||
4. Return list of `(dataname, data, type)` tuples
|
NATS_connection=my_connection, # Pre-existing connection
|
||||||
|
is_publish=true
|
||||||
|
)
|
||||||
|
# Uses: publish_message(NATS_connection, subject, env_json_str, cid)
|
||||||
|
|
||||||
|
# When NATS_connection is not provided, it uses the URL-based publish_message
|
||||||
|
env, env_json_str = smartsend(
|
||||||
|
"my.subject",
|
||||||
|
[("data", payload_data, "type")],
|
||||||
|
broker_url="nats://localhost:4222",
|
||||||
|
is_publish=true
|
||||||
|
)
|
||||||
|
# Uses: publish_message(broker_url, subject, env_json_str, cid)
|
||||||
|
```
|
||||||
|
|
||||||
## Scenario Implementations
|
## Scenario Implementations
|
||||||
|
|
||||||
### Scenario 1: Command & Control (Small Dictionary)
|
### Scenario 1: Command & Control (Small Dictionary)
|
||||||
|
|
||||||
**Julia (Receiver):**
|
**Julia (Sender/Receiver):**
|
||||||
```julia
|
```julia
|
||||||
# Subscribe to control subject
|
# Subscribe to control subject
|
||||||
# Parse JSON envelope
|
# Parse JSON envelope
|
||||||
@@ -423,15 +452,9 @@ async function smartreceive(msg, options = {})
|
|||||||
# Send acknowledgment
|
# Send acknowledgment
|
||||||
```
|
```
|
||||||
|
|
||||||
**JavaScript (Sender):**
|
|
||||||
```javascript
|
|
||||||
// Create small dictionary config
|
|
||||||
// Send via smartsend with type="dictionary"
|
|
||||||
```
|
|
||||||
|
|
||||||
### Scenario 2: Deep Dive Analysis (Large Arrow Table)
|
### Scenario 2: Deep Dive Analysis (Large Arrow Table)
|
||||||
|
|
||||||
**Julia (Sender):**
|
**Julia (Sender/Receiver):**
|
||||||
```julia
|
```julia
|
||||||
# Create large DataFrame
|
# Create large DataFrame
|
||||||
# Convert to Arrow IPC stream
|
# Convert to Arrow IPC stream
|
||||||
@@ -440,50 +463,28 @@ async function smartreceive(msg, options = {})
|
|||||||
# Publish NATS with URL
|
# Publish NATS with URL
|
||||||
```
|
```
|
||||||
|
|
||||||
**JavaScript (Receiver):**
|
|
||||||
```javascript
|
|
||||||
// Receive NATS message with URL
|
|
||||||
// Fetch data from HTTP server
|
|
||||||
// Parse Arrow IPC with zero-copy
|
|
||||||
// Load into Perspective.js or D3
|
|
||||||
```
|
|
||||||
|
|
||||||
### Scenario 3: Live Audio Processing
|
### Scenario 3: Live Audio Processing
|
||||||
|
|
||||||
**JavaScript (Sender):**
|
**Julia (Sender/Receiver):**
|
||||||
```javascript
|
|
||||||
// Capture audio chunk
|
|
||||||
// Send as binary with metadata headers
|
|
||||||
// Use smartsend with type="audio"
|
|
||||||
```
|
|
||||||
|
|
||||||
**Julia (Receiver):**
|
|
||||||
```julia
|
```julia
|
||||||
// Receive audio data
|
# Receive audio data
|
||||||
// Perform FFT or AI transcription
|
# Perform FFT or AI transcription
|
||||||
// Send results back (JSON + Arrow table)
|
# Send results back (JSON + Arrow table)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Scenario 4: Catch-Up (JetStream)
|
### Scenario 4: Catch-Up (JetStream)
|
||||||
|
|
||||||
**Julia (Producer):**
|
**Julia (Producer/Consumer):**
|
||||||
```julia
|
```julia
|
||||||
# Publish to JetStream
|
# Publish to JetStream
|
||||||
# Include metadata for temporal tracking
|
# Include metadata for temporal tracking
|
||||||
```
|
```
|
||||||
|
|
||||||
**JavaScript (Consumer):**
|
|
||||||
```javascript
|
|
||||||
// Connect to JetStream
|
|
||||||
// Request replay from last 10 minutes
|
|
||||||
// Process historical and real-time messages
|
|
||||||
```
|
|
||||||
|
|
||||||
### Scenario 5: Selection (Low Bandwidth)
|
### Scenario 5: Selection (Low Bandwidth)
|
||||||
|
|
||||||
**Focus:** Small Arrow tables, Julia to JavaScript. The Action: Julia wants to send a small DataFrame to show on a JavaScript dashboard for the user to choose.
|
**Focus:** Small Arrow tables. The Action: Julia wants to send a small DataFrame to show on a receiving application for the user to choose.
|
||||||
|
|
||||||
**Julia (Sender):**
|
**Julia (Sender/Receiver):**
|
||||||
```julia
|
```julia
|
||||||
# Create small DataFrame (e.g., 50KB - 500KB)
|
# Create small DataFrame (e.g., 50KB - 500KB)
|
||||||
# Convert to Arrow IPC stream
|
# Convert to Arrow IPC stream
|
||||||
@@ -492,18 +493,6 @@ async function smartreceive(msg, options = {})
|
|||||||
# Include metadata for dashboard selection context
|
# Include metadata for dashboard selection context
|
||||||
```
|
```
|
||||||
|
|
||||||
**JavaScript (Receiver):**
|
|
||||||
```javascript
|
|
||||||
// Receive NATS message with direct transport
|
|
||||||
// Decode Base64 payload
|
|
||||||
// Parse Arrow IPC with zero-copy
|
|
||||||
// Load into selection UI component (e.g., dropdown, table)
|
|
||||||
// User makes selection
|
|
||||||
// Send selection back to Julia
|
|
||||||
```
|
|
||||||
|
|
||||||
**Use Case:** Julia server generates a list of available options (e.g., file selections, configuration presets) as a small DataFrame and sends to JavaScript dashboard for user selection. The selection is then sent back to Julia for processing.
|
|
||||||
|
|
||||||
### Scenario 6: Chat System
|
### Scenario 6: Chat System
|
||||||
|
|
||||||
**Focus:** Every conversational message is composed of any number and any combination of components, spanning the full spectrum from small to large. This includes text, images, audio, video, tables, and files—specifically accommodating everything from brief snippets to high-resolution images, large audio files, extensive tables, and massive documents. Support for claim-check delivery and full bi-directional messaging.
|
**Focus:** Every conversational message is composed of any number and any combination of components, spanning the full spectrum from small to large. This includes text, images, audio, video, tables, and files—specifically accommodating everything from brief snippets to high-resolution images, large audio files, extensive tables, and massive documents. Support for claim-check delivery and full bi-directional messaging.
|
||||||
@@ -528,26 +517,9 @@ async function smartreceive(msg, options = {})
|
|||||||
# Support bidirectional messaging with replyTo fields
|
# Support bidirectional messaging with replyTo fields
|
||||||
```
|
```
|
||||||
|
|
||||||
**JavaScript (Sender/Receiver):**
|
|
||||||
```javascript
|
|
||||||
// Build chat message with mixed content:
|
|
||||||
// - User input text: direct transport
|
|
||||||
// - Selected image: check size, use appropriate transport
|
|
||||||
// - Audio recording: link transport for large files
|
|
||||||
// - File attachment: link transport
|
|
||||||
//
|
|
||||||
// Parse received message:
|
|
||||||
// - Direct payloads: decode Base64
|
|
||||||
// - Link payloads: fetch from HTTP with exponential backoff
|
|
||||||
// - Deserialize all payloads appropriately
|
|
||||||
//
|
|
||||||
// Render mixed content in chat interface
|
|
||||||
// Support bidirectional reply with claim-check delivery confirmation
|
|
||||||
```
|
|
||||||
|
|
||||||
**Use Case:** Full-featured chat system supporting rich media. User can send text, small images directly, or upload large files that get uploaded to HTTP server and referenced via URLs. Claim-check pattern ensures reliable delivery tracking for all message components.
|
**Use Case:** Full-featured chat system supporting rich media. User can send text, small images directly, or upload large files that get uploaded to HTTP server and referenced via URLs. Claim-check pattern ensures reliable delivery tracking for all message components.
|
||||||
|
|
||||||
**Implementation Note:** The `smartreceive` function iterates through all payloads in the envelope and processes each according to its transport type. See the standard API format in Section 1: `msgEnvelope_v1` supports `AbstractArray{msgPayload_v1}` for multiple payloads.
|
**Implementation Note:** The `smartreceive` function iterates through all payloads in the envelope and processes each according to its transport type. See the standard API format in Section 1: `msg_envelope_v1` supports `Vector{msg_payload_v1}` for multiple payloads.
|
||||||
|
|
||||||
## Performance Considerations
|
## Performance Considerations
|
||||||
|
|
||||||
|
|||||||
@@ -2,83 +2,170 @@
|
|||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
This document describes the implementation of the high-performance, bi-directional data bridge between Julia and JavaScript services using NATS (Core & JetStream), implementing the Claim-Check pattern for large payloads.
|
This document describes the implementation of the high-performance, bi-directional data bridge for **Julia** applications using NATS (Core & JetStream), implementing the Claim-Check pattern for large payloads.
|
||||||
|
|
||||||
### Multi-Payload Support
|
The system enables seamless communication for Julia applications.
|
||||||
|
|
||||||
The implementation uses a **standardized list-of-tuples format** for all payload operations. **Even when sending a single payload, the user must wrap it in a list.**
|
### Implementation Files
|
||||||
|
|
||||||
|
NATSBridge is implemented in Julia:
|
||||||
|
|
||||||
|
| Language | Implementation File | Description |
|
||||||
|
|----------|---------------------|-------------|
|
||||||
|
| **Julia** | [`src/NATSBridge.jl`](../src/NATSBridge.jl) | Full Julia implementation with Arrow IPC support |
|
||||||
|
|
||||||
|
### File Server Handler Architecture
|
||||||
|
|
||||||
|
The system uses **handler functions** to abstract file server operations, allowing support for different file server implementations (e.g., Plik, AWS S3, custom HTTP server).
|
||||||
|
|
||||||
|
**Handler Function Signatures:**
|
||||||
|
|
||||||
|
```julia
|
||||||
|
# Upload handler - uploads data to file server and returns URL
|
||||||
|
# The handler is passed to smartsend as fileserver_upload_handler parameter
|
||||||
|
# It receives: (fileserver_url::String, dataname::String, data::Vector{UInt8})
|
||||||
|
# Returns: Dict{String, Any} with keys: "status", "uploadid", "fileid", "url"
|
||||||
|
fileserver_upload_handler(fileserver_url::String, dataname::String, data::Vector{UInt8})::Dict{String, Any}
|
||||||
|
|
||||||
|
# Download handler - fetches data from file server URL with exponential backoff
|
||||||
|
# The handler is passed to smartreceive as fileserver_download_handler parameter
|
||||||
|
# It receives: (url::String, max_retries::Int, base_delay::Int, max_delay::Int, correlation_id::String)
|
||||||
|
# Returns: Vector{UInt8} (the downloaded data)
|
||||||
|
fileserver_download_handler(url::String, max_retries::Int, base_delay::Int, max_delay::Int, correlation_id::String)::Vector{UInt8}
|
||||||
|
```
|
||||||
|
|
||||||
|
This design allows the system to support multiple file server backends without changing the core messaging logic.
|
||||||
|
|
||||||
|
### Multi-Payload Support (Standard API)
|
||||||
|
|
||||||
|
The system uses a **standardized list-of-tuples format** for all payload operations. **Even when sending a single payload, the user must wrap it in a list.**
|
||||||
|
|
||||||
**API Standard:**
|
**API Standard:**
|
||||||
```julia
|
```julia
|
||||||
# Input format for smartsend (always a list of tuples with type info)
|
# Input format for smartsend (always a list of tuples with type info)
|
||||||
[(dataname1, data1, type1), (dataname2, data2, type2), ...]
|
[(dataname1, data1, type1), (dataname2, data2, type2), ...]
|
||||||
|
|
||||||
# Output format for smartreceive (always returns a list of tuples with type info)
|
# Output format for smartreceive (returns a dictionary with payloads field containing list of tuples)
|
||||||
[(dataname1, data1, type1), (dataname2, data2, type2), ...]
|
# Returns: Dict with envelope metadata and payloads field containing Vector{Tuple{String, Any, String}}
|
||||||
|
# {
|
||||||
|
# "correlation_id": "...",
|
||||||
|
# "msg_id": "...",
|
||||||
|
# "timestamp": "...",
|
||||||
|
# "send_to": "...",
|
||||||
|
# "msg_purpose": "...",
|
||||||
|
# "sender_name": "...",
|
||||||
|
# "sender_id": "...",
|
||||||
|
# "receiver_name": "...",
|
||||||
|
# "receiver_id": "...",
|
||||||
|
# "reply_to": "...",
|
||||||
|
# "reply_to_msg_id": "...",
|
||||||
|
# "broker_url": "...",
|
||||||
|
# "metadata": {...},
|
||||||
|
# "payloads": [(dataname1, data1, type1), (dataname2, data2, type2), ...]
|
||||||
|
# }
|
||||||
```
|
```
|
||||||
|
|
||||||
Where `type` can be: `"text"`, `"dictionary"`, `"table"`, `"image"`, `"audio"`, `"video"`, `"binary"`
|
**Supported Types:**
|
||||||
|
- `"text"` - Plain text
|
||||||
|
- `"dictionary"` - JSON-serializable dictionaries (Dict, NamedTuple)
|
||||||
|
- `"table"` - Tabular data (DataFrame, array of structs)
|
||||||
|
- `"image"` - Image data (Bitmap, PNG/JPG bytes)
|
||||||
|
- `"audio"` - Audio data (WAV, MP3 bytes)
|
||||||
|
- `"video"` - Video data (MP4, AVI bytes)
|
||||||
|
- `"binary"` - Generic binary data (Vector{UInt8})
|
||||||
|
|
||||||
|
This design allows per-payload type specification, enabling **mixed-content messages** where different payloads can use different serialization formats in a single message.
|
||||||
|
|
||||||
**Examples:**
|
**Examples:**
|
||||||
```julia
|
```julia
|
||||||
# Single payload - still wrapped in a list (type is required as third element)
|
# Single payload - still wrapped in a list
|
||||||
smartsend("/test", [(dataname1, data1, "text")], ...)
|
smartsend(
|
||||||
|
"/test",
|
||||||
|
[("dataname1", data1, "dictionary")], # List with one tuple (data, type)
|
||||||
|
broker_url="nats://localhost:4222",
|
||||||
|
fileserver_upload_handler=plik_oneshot_upload
|
||||||
|
)
|
||||||
|
|
||||||
# Multiple payloads in one message (each payload has its own type)
|
# Multiple payloads in one message with different types
|
||||||
smartsend("/test", [(dataname1, data1, "dictionary"), (dataname2, data2, "table")], ...)
|
smartsend(
|
||||||
|
"/test",
|
||||||
|
[("dataname1", data1, "dictionary"), ("dataname2", data2, "table")],
|
||||||
|
broker_url="nats://localhost:4222",
|
||||||
|
fileserver_upload_handler=plik_oneshot_upload
|
||||||
|
)
|
||||||
|
|
||||||
# Receive always returns a list with type info
|
# Mixed content (e.g., chat with text, image, audio)
|
||||||
payloads = smartreceive(msg, ...)
|
smartsend(
|
||||||
# payloads = [(dataname1, data1, "text"), (dataname2, data2, "table"), ...]
|
"/chat",
|
||||||
|
[
|
||||||
|
("message_text", "Hello!", "text"),
|
||||||
|
("user_image", image_data, "image"),
|
||||||
|
("audio_clip", audio_data, "audio")
|
||||||
|
],
|
||||||
|
broker_url="nats://localhost:4222"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Receive returns a dictionary envelope with all metadata and deserialized payloads
|
||||||
|
env = smartreceive(msg; fileserver_download_handler=_fetch_with_backoff, max_retries=5, base_delay=100, max_delay=5000)
|
||||||
|
# env["payloads"] = [("dataname1", data1, type1), ("dataname2", data2, type2), ...]
|
||||||
|
# env["correlation_id"], env["msg_id"], etc.
|
||||||
|
# env is a dictionary containing envelope metadata and payloads field
|
||||||
```
|
```
|
||||||
|
|
||||||
## Architecture
|
## Architecture
|
||||||
|
|
||||||
The implementation follows the Claim-Check pattern:
|
The Julia implementation follows the Claim-Check pattern:
|
||||||
|
|
||||||
```
|
```
|
||||||
┌─────────────────────────────────────────────────────────────────────────┐
|
┌─────────────────────────────────────────────────────────────────────────┐
|
||||||
│ SmartSend Function │
|
│ SmartSend Function │
|
||||||
└─────────────────────────────────────────────────────────────────────────┘
|
└─────────────────────────────────────────────────────────────────────────┘
|
||||||
│
|
│
|
||||||
▼
|
▼
|
||||||
┌─────────────────────────────────────────────────────────────────────────┐
|
┌─────────────────────────────────────────────────────────────────────────┐
|
||||||
│ Is payload size < 1MB? │
|
│ Is payload size < 1MB? │
|
||||||
└─────────────────────────────────────────────────────────────────────────┘
|
└─────────────────────────────────────────────────────────────────────────┘
|
||||||
│
|
│
|
||||||
┌─────────────────┴─────────────────┐
|
┌─────────────────┴─────────────────┐
|
||||||
▼ ▼
|
▼ ▼
|
||||||
┌─────────────────┐ ┌─────────────────┐
|
┌─────────────────┐ ┌─────────────────┐
|
||||||
│ Direct Path │ │ Link Path │
|
│ Direct Path │ │ Link Path │
|
||||||
│ (< 1MB) │ │ (> 1MB) │
|
│ (< 1MB) │ │ (> 1MB) │
|
||||||
│ │ │ │
|
│ │ │ │
|
||||||
│ • Serialize to │ │ • Serialize to │
|
│ • Serialize to │ │ • Serialize to │
|
||||||
│ IOBuffer │ │ IOBuffer │
|
│ Buffer │ │ Buffer │
|
||||||
│ • Base64 encode │ │ • Upload to │
|
│ • Base64 encode │ │ • Upload to │
|
||||||
│ • Publish to │ │ HTTP Server │
|
│ • Publish to │ │ HTTP Server │
|
||||||
│ NATS │ │ • Publish to │
|
│ NATS │ │ • Publish to │
|
||||||
│ │ │ NATS with URL │
|
│ │ │ NATS with URL │
|
||||||
└─────────────────┘ └─────────────────┘
|
└─────────────────┘ └─────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
## Files
|
## smartsend Return Value
|
||||||
|
|
||||||
### Julia Module: [`src/julia_bridge.jl`](../src/julia_bridge.jl)
|
The `smartsend` function now returns a tuple containing both the envelope object and the JSON string representation:
|
||||||
|
|
||||||
|
```julia
|
||||||
|
env, env_json_str = smartsend(...)
|
||||||
|
# env::msg_envelope_v1 - The envelope object with all metadata and payloads
|
||||||
|
# env_json_str::String - JSON string for publishing to NATS
|
||||||
|
```
|
||||||
|
|
||||||
|
**Options:**
|
||||||
|
- `is_publish::Bool = true` - When `true` (default), the message is automatically published to NATS. When `false`, the function returns the envelope and JSON string without publishing, allowing manual publishing via NATS request-reply pattern.
|
||||||
|
|
||||||
|
This enables two use cases:
|
||||||
|
1. **Programmatic envelope access**: Access envelope fields directly via the `env` object
|
||||||
|
2. **Direct JSON publishing**: Publish the JSON string directly using NATS request-reply pattern
|
||||||
|
|
||||||
|
### Julia Module: [`src/NATSBridge.jl`](../src/NATSBridge.jl)
|
||||||
|
|
||||||
The Julia implementation provides:
|
The Julia implementation provides:
|
||||||
|
|
||||||
- **[`MessageEnvelope`](../src/julia_bridge.jl)**: Struct for the unified JSON envelope
|
- **[`msg_envelope_v1`](src/NATSBridge.jl)**: Struct for the unified JSON envelope
|
||||||
- **[`SmartSend()`](../src/julia_bridge.jl)**: Handles transport selection based on payload size
|
- **[`msg_payload_v1`](src/NATSBridge.jl)**: Struct for individual payload representation
|
||||||
- **[`SmartReceive()`](../src/julia_bridge.jl)**: Handles both direct and link transport
|
- **[`smartsend()`](src/NATSBridge.jl)**: Handles transport selection based on payload size
|
||||||
|
- **[`smartreceive()`](src/NATSBridge.jl)**: Handles both direct and link transport
|
||||||
### JavaScript Module: [`src/NATSBridge.js`](../src/NATSBridge.js)
|
|
||||||
|
|
||||||
The JavaScript implementation provides:
|
|
||||||
|
|
||||||
- **`MessageEnvelope` class**: For the unified JSON envelope
|
|
||||||
- **`MessagePayload` class**: For individual payload representation
|
|
||||||
- **[`smartsend()`](../src/NATSBridge.js)**: Handles transport selection based on payload size
|
|
||||||
- **[`smartreceive()`](../src/NATSBridge.js)**: Handles both direct and link transport
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -94,12 +181,6 @@ Pkg.add("UUIDs")
|
|||||||
Pkg.add("Dates")
|
Pkg.add("Dates")
|
||||||
```
|
```
|
||||||
|
|
||||||
### JavaScript Dependencies
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm install nats.js apache-arrow uuid base64-url
|
|
||||||
```
|
|
||||||
|
|
||||||
## Usage Tutorial
|
## Usage Tutorial
|
||||||
|
|
||||||
### Step 1: Start NATS Server
|
### Step 1: Start NATS Server
|
||||||
@@ -122,21 +203,62 @@ python3 -m http.server 8080 --directory /tmp/fileserver
|
|||||||
### Step 3: Run Test Scenarios
|
### Step 3: Run Test Scenarios
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Scenario 1: Command & Control (JavaScript sender)
|
# Scenario 1: Command & Control
|
||||||
node test/scenario1_command_control.js
|
julia test/scenario1_command_control.jl
|
||||||
|
|
||||||
# Scenario 2: Large Arrow Table (JavaScript sender)
|
# Scenario 2: Large Arrow Table
|
||||||
node test/scenario2_large_table.js
|
julia test/scenario2_large_table.jl
|
||||||
|
|
||||||
# Scenario 3: Julia-to-Julia communication
|
# Scenario 3: Julia-to-Julia communication
|
||||||
# Run both Julia and JavaScript versions
|
|
||||||
julia test/scenario3_julia_to_julia.jl
|
julia test/scenario3_julia_to_julia.jl
|
||||||
node test/scenario3_julia_to_julia.js
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
### Scenario 0: Basic Multi-Payload Example
|
### Scenario 1: Command & Control (Small Dictionary)
|
||||||
|
|
||||||
|
**Focus:** Sending small dictionary configurations. This is the simplest use case for command and control scenarios.
|
||||||
|
|
||||||
|
**Julia (Sender/Receiver):**
|
||||||
|
```julia
|
||||||
|
using NATSBridge
|
||||||
|
|
||||||
|
# Send small dictionary config (wrapped in list with type)
|
||||||
|
config = Dict("step_size" => 0.01, "iterations" => 1000, "threshold" => 0.5)
|
||||||
|
env, env_json_str = smartsend(
|
||||||
|
"control",
|
||||||
|
[("config", config, "dictionary")],
|
||||||
|
broker_url="nats://localhost:4222"
|
||||||
|
)
|
||||||
|
# env: msg_envelope_v1 with all metadata and payloads
|
||||||
|
# env_json_str: JSON string for publishing
|
||||||
|
```
|
||||||
|
|
||||||
|
**Julia (Sender/Receiver) with NATS_connection for connection reuse:**
|
||||||
|
```julia
|
||||||
|
using NATSBridge
|
||||||
|
|
||||||
|
# Create connection once for high-frequency publishing
|
||||||
|
conn = NATS.connect("nats://localhost:4222")
|
||||||
|
|
||||||
|
# Send multiple messages using the same connection (saves connection overhead)
|
||||||
|
for i in 1:100
|
||||||
|
config = Dict("iteration" => i, "data" => rand())
|
||||||
|
smartsend(
|
||||||
|
"control",
|
||||||
|
[("config", config, "dictionary")],
|
||||||
|
NATS_connection=conn, # Reuse connection
|
||||||
|
is_publish=true
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Close connection when done
|
||||||
|
NATS.close(conn)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Use Case:** High-frequency publishing scenarios where connection reuse provides performance benefits by avoiding the overhead of establishing a new NATS connection for each message.
|
||||||
|
|
||||||
|
### Basic Multi-Payload Example
|
||||||
|
|
||||||
#### Julia (Sender)
|
#### Julia (Sender)
|
||||||
```julia
|
```julia
|
||||||
@@ -146,95 +268,21 @@ using NATSBridge
|
|||||||
smartsend(
|
smartsend(
|
||||||
"/test",
|
"/test",
|
||||||
[("dataname1", data1, "dictionary"), ("dataname2", data2, "table")],
|
[("dataname1", data1, "dictionary"), ("dataname2", data2, "table")],
|
||||||
nats_url="nats://localhost:4222",
|
broker_url="nats://localhost:4222",
|
||||||
fileserver_url="http://localhost:8080",
|
fileserver_url="http://localhost:8080"
|
||||||
metadata=Dict("custom_key" => "custom_value")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Even single payload must be wrapped in a list with type
|
# Even single payload must be wrapped in a list with type
|
||||||
smartsend("/test", [("single_data", mydata, "dictionary")])
|
smartsend("/test", [("single_data", mydata, "dictionary")], broker_url="nats://localhost:4222")
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Julia (Receiver)
|
#### Julia (Receiver)
|
||||||
```julia
|
```julia
|
||||||
using NATSBridge
|
using NATSBridge
|
||||||
|
|
||||||
# Receive returns a list of payloads with type info
|
# Receive returns a dictionary with envelope metadata and payloads field
|
||||||
payloads = smartreceive(msg, "http://localhost:8080")
|
env = smartreceive(msg)
|
||||||
# payloads = [(dataname1, data1, "dictionary"), (dataname2, data2, "table"), ...]
|
# env["payloads"] = [(dataname1, data1, "dictionary"), (dataname2, data2, "table"), ...]
|
||||||
```
|
|
||||||
|
|
||||||
### Scenario 1: Command & Control (Small JSON)
|
|
||||||
|
|
||||||
#### JavaScript (Sender)
|
|
||||||
```javascript
|
|
||||||
const { smartsend } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
// Single payload wrapped in a list
|
|
||||||
const config = [{
|
|
||||||
dataname: "config",
|
|
||||||
data: { step_size: 0.01, iterations: 1000 },
|
|
||||||
type: "dictionary"
|
|
||||||
}];
|
|
||||||
|
|
||||||
await smartsend("control", config, {
|
|
||||||
correlationId: "unique-id"
|
|
||||||
});
|
|
||||||
|
|
||||||
// Multiple payloads
|
|
||||||
const configs = [
|
|
||||||
{
|
|
||||||
dataname: "config1",
|
|
||||||
data: { step_size: 0.01 },
|
|
||||||
type: "dictionary"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
dataname: "config2",
|
|
||||||
data: { iterations: 1000 },
|
|
||||||
type: "dictionary"
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
await smartsend("control", configs);
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Julia (Receiver)
|
|
||||||
```julia
|
|
||||||
using NATS
|
|
||||||
using JSON3
|
|
||||||
|
|
||||||
# Subscribe to control subject
|
|
||||||
subscribe(nats, "control") do msg
|
|
||||||
env = MessageEnvelope(String(msg.data))
|
|
||||||
config = JSON3.read(env.payload)
|
|
||||||
|
|
||||||
# Execute simulation with parameters
|
|
||||||
step_size = config.step_size
|
|
||||||
iterations = config.iterations
|
|
||||||
|
|
||||||
# Send acknowledgment
|
|
||||||
response = Dict("status" => "Running", "correlation_id" => env.correlation_id)
|
|
||||||
publish(nats, "control_response", JSON3.stringify(response))
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
### JavaScript (Receiver)
|
|
||||||
```javascript
|
|
||||||
const { smartreceive } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
// Subscribe to messages
|
|
||||||
const nc = await connect({ servers: ['nats://localhost:4222'] });
|
|
||||||
const sub = nc.subscribe("control");
|
|
||||||
|
|
||||||
for await (const msg of sub) {
|
|
||||||
const result = await smartreceive(msg);
|
|
||||||
|
|
||||||
// Process the result
|
|
||||||
for (const { dataname, data, type } of result) {
|
|
||||||
console.log(`Received ${dataname} of type ${type}`);
|
|
||||||
console.log(`Data: ${JSON.stringify(data)}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Scenario 2: Deep Dive Analysis (Large Arrow Table)
|
### Scenario 2: Deep Dive Analysis (Large Arrow Table)
|
||||||
@@ -251,120 +299,164 @@ df = DataFrame(
|
|||||||
category = rand(["A", "B", "C"], 10_000_000)
|
category = rand(["A", "B", "C"], 10_000_000)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Send via SmartSend - wrapped in a list (type is part of each tuple)
|
# Send via smartsend - wrapped in list with type
|
||||||
await SmartSend("analysis_results", [("table_data", df, "table")]);
|
# Large payload will use link transport (HTTP fileserver)
|
||||||
|
env, env_json_str = smartsend(
|
||||||
|
"analysis_results",
|
||||||
|
[("table_data", df, "table")],
|
||||||
|
broker_url="nats://localhost:4222",
|
||||||
|
fileserver_url="http://localhost:8080"
|
||||||
|
)
|
||||||
|
# env: msg_envelope_v1 with all metadata and payloads
|
||||||
|
# env_json_str: JSON string for publishing
|
||||||
```
|
```
|
||||||
|
|
||||||
#### JavaScript (Receiver)
|
#### smartsend Function Signature (Julia)
|
||||||
```javascript
|
|
||||||
const { smartreceive } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
const result = await smartreceive(msg);
|
|
||||||
|
|
||||||
// Use table data for visualization with Perspective.js or D3
|
|
||||||
// Note: Tables are sent as arrays of objects in JavaScript
|
|
||||||
const table = result;
|
|
||||||
```
|
|
||||||
|
|
||||||
### Scenario 3: Live Binary Processing
|
|
||||||
|
|
||||||
#### JavaScript (Sender)
|
|
||||||
```javascript
|
|
||||||
const { smartsend } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
// Binary data wrapped in a list
|
|
||||||
const binaryData = [{
|
|
||||||
dataname: "audio_chunk",
|
|
||||||
data: binaryBuffer, // ArrayBuffer or Uint8Array
|
|
||||||
type: "binary"
|
|
||||||
}];
|
|
||||||
|
|
||||||
await smartsend("binary_input", binaryData, {
|
|
||||||
metadata: {
|
|
||||||
sample_rate: 44100,
|
|
||||||
channels: 1
|
|
||||||
}
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Julia (Receiver)
|
|
||||||
```julia
|
```julia
|
||||||
using WAV
|
function smartsend(
|
||||||
using DSP
|
subject::String,
|
||||||
|
data::AbstractArray{Tuple{String, Any, String}, 1}; # List of (dataname, data, type) tuples
|
||||||
|
broker_url::String = DEFAULT_BROKER_URL, # NATS server URL
|
||||||
|
fileserver_url = DEFAULT_FILESERVER_URL,
|
||||||
|
fileserver_upload_handler::Function = plik_oneshot_upload,
|
||||||
|
size_threshold::Int = DEFAULT_SIZE_THRESHOLD,
|
||||||
|
correlation_id::Union{String, Nothing} = nothing,
|
||||||
|
msg_purpose::String = "chat",
|
||||||
|
sender_name::String = "NATSBridge",
|
||||||
|
receiver_name::String = "",
|
||||||
|
receiver_id::String = "",
|
||||||
|
reply_to::String = "",
|
||||||
|
reply_to_msg_id::String = "",
|
||||||
|
is_publish::Bool = true,
|
||||||
|
NATS_connection::Union{NATS.Connection, Nothing} = nothing # Pre-existing NATS connection (optional)
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
# Receive binary data
|
**New Keyword Parameter:**
|
||||||
function process_binary(data)
|
- `NATS_connection::Union{NATS.Connection, Nothing} = nothing` - Pre-existing NATS connection. When provided, `smartsend` uses this connection instead of creating a new one, avoiding the overhead of connection establishment. This is useful for high-frequency publishing scenarios.
|
||||||
# Perform FFT or AI transcription
|
|
||||||
spectrum = fft(data)
|
|
||||||
|
|
||||||
# Send results back (JSON + Arrow table)
|
**Connection Handling Logic:**
|
||||||
results = Dict("transcription" => "sample text", "spectrum" => spectrum)
|
```julia
|
||||||
await SmartSend("binary_output", results, "json")
|
if is_publish == false
|
||||||
|
# skip publish
|
||||||
|
elseif is_publish == true && NATS_connection === nothing
|
||||||
|
publish_message(broker_url, subject, env_json_str, cid) # Creates new connection
|
||||||
|
elseif is_publish == true && NATS_connection !== nothing
|
||||||
|
publish_message(NATS_connection, subject, env_json_str, cid) # Uses provided connection
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
### JavaScript (Receiver)
|
**Example with pre-existing connection:**
|
||||||
```javascript
|
```julia
|
||||||
const { smartreceive } = require('./src/NATSBridge');
|
using NATSBridge
|
||||||
|
|
||||||
// Receive binary data
|
# Create connection once
|
||||||
function process_binary(msg) {
|
conn = NATS.connect("nats://localhost:4222")
|
||||||
const result = await smartreceive(msg);
|
|
||||||
|
|
||||||
// Process the binary data
|
# Send multiple messages using the same connection
|
||||||
for (const { dataname, data, type } of result) {
|
for i in 1:100
|
||||||
if (type === "binary") {
|
data = rand(1000)
|
||||||
// data is an ArrayBuffer or Uint8Array
|
smartsend(
|
||||||
console.log(`Received binary data: ${dataname}, size: ${data.length}`);
|
"analysis_results",
|
||||||
// Perform FFT or AI transcription here
|
[("table_data", data, "table")],
|
||||||
}
|
NATS_connection=conn, # Reuse connection
|
||||||
}
|
is_publish=true
|
||||||
}
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Close connection when done
|
||||||
|
NATS.close(conn)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### publish_message Function
|
||||||
|
|
||||||
|
The `publish_message` function provides two overloads for publishing messages to NATS:
|
||||||
|
|
||||||
|
**Overload 1 - URL-based publishing (creates new connection):**
|
||||||
|
```julia
|
||||||
|
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
|
||||||
|
```
|
||||||
|
|
||||||
|
**Overload 2 - Connection-based publishing (uses pre-existing connection):**
|
||||||
|
```julia
|
||||||
|
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")
|
||||||
|
finally
|
||||||
|
NATS.drain(conn) # Ensure connection is closed properly
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
**Use Case:** Use the connection-based overload 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.
|
||||||
|
|
||||||
|
**Integration with smartsend:**
|
||||||
|
```julia
|
||||||
|
# When NATS_connection is provided to smartsend, it uses the connection-based publish_message
|
||||||
|
env, env_json_str = smartsend(
|
||||||
|
"my.subject",
|
||||||
|
[("data", payload_data, "type")],
|
||||||
|
NATS_connection=my_connection, # Pre-existing connection
|
||||||
|
is_publish=true
|
||||||
|
)
|
||||||
|
# Uses: publish_message(NATS_connection, subject, env_json_str, cid)
|
||||||
|
|
||||||
|
# When NATS_connection is not provided, it uses the URL-based publish_message
|
||||||
|
env, env_json_str = smartsend(
|
||||||
|
"my.subject",
|
||||||
|
[("data", payload_data, "type")],
|
||||||
|
broker_url="nats://localhost:4222",
|
||||||
|
is_publish=true
|
||||||
|
)
|
||||||
|
# Uses: publish_message(broker_url, subject, env_json_str, cid)
|
||||||
|
```
|
||||||
|
|
||||||
|
**API Consistency Note:**
|
||||||
|
- **Julia:** Uses `NATS_connection` keyword parameter with function overloading for automatic connection management
|
||||||
|
|
||||||
|
### Scenario 3: Live Binary Processing
|
||||||
|
|
||||||
|
**Julia (Sender/Receiver):**
|
||||||
|
```julia
|
||||||
|
using NATSBridge
|
||||||
|
|
||||||
|
# Binary data wrapped in list with type
|
||||||
|
smartsend(
|
||||||
|
"binary_input",
|
||||||
|
[("audio_chunk", binary_buffer, "binary")],
|
||||||
|
broker_url="nats://localhost:4222",
|
||||||
|
metadata=["sample_rate" => 44100, "channels" => 1]
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Scenario 4: Catch-Up (JetStream)
|
### Scenario 4: Catch-Up (JetStream)
|
||||||
|
|
||||||
#### Julia (Producer)
|
**Julia (Producer/Consumer):**
|
||||||
```julia
|
```julia
|
||||||
using NATSBridge
|
using NATSBridge
|
||||||
|
|
||||||
function publish_health_status(nats_url)
|
function publish_health_status(broker_url)
|
||||||
# Send status wrapped in a list (type is part of each tuple)
|
# Send status wrapped in list with type
|
||||||
status = Dict("cpu" => rand(), "memory" => rand())
|
status = Dict("cpu" => rand(), "memory" => rand())
|
||||||
smartsend("health", [("status", status, "dictionary")], nats_url=nats_url)
|
env, env_json_str = smartsend(
|
||||||
|
"health",
|
||||||
|
[("status", status, "dictionary")],
|
||||||
|
broker_url=broker_url
|
||||||
|
)
|
||||||
sleep(5) # Every 5 seconds
|
sleep(5) # Every 5 seconds
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
#### JavaScript (Consumer)
|
|
||||||
```javascript
|
|
||||||
const { connect } = require('nats');
|
|
||||||
const { smartreceive } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
const nc = await connect({ servers: ['nats://localhost:4222'] });
|
|
||||||
const js = nc.jetstream();
|
|
||||||
|
|
||||||
// Request replay from last 10 minutes
|
|
||||||
const consumer = await js.pullSubscribe("health", {
|
|
||||||
durable_name: "catchup",
|
|
||||||
max_batch: 100,
|
|
||||||
max_ack_wait: 30000
|
|
||||||
});
|
|
||||||
|
|
||||||
// Process historical and real-time messages
|
|
||||||
for await (const msg of consumer) {
|
|
||||||
const result = await smartreceive(msg);
|
|
||||||
// result contains the list of payloads
|
|
||||||
// Each payload has: dataname, data, type
|
|
||||||
msg.ack();
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Scenario 5: Selection (Low Bandwidth)
|
### Scenario 5: Selection (Low Bandwidth)
|
||||||
|
|
||||||
**Focus:** Small Arrow tables, Julia to JavaScript. The Action: Julia wants to send a small DataFrame to show on a JavaScript dashboard for the user to choose.
|
**Focus:** Small Arrow tables. The Action: Julia wants to send a small DataFrame to show on a receiving application for the user to choose.
|
||||||
|
|
||||||
**Julia (Sender):**
|
**Julia (Sender/Receiver):**
|
||||||
```julia
|
```julia
|
||||||
using NATSBridge
|
using NATSBridge
|
||||||
using DataFrames
|
using DataFrames
|
||||||
@@ -382,35 +474,17 @@ options_df = DataFrame(
|
|||||||
# Check payload size (< 1MB threshold)
|
# Check payload size (< 1MB threshold)
|
||||||
# Publish directly to NATS with Base64-encoded payload
|
# Publish directly to NATS with Base64-encoded payload
|
||||||
# Include metadata for dashboard selection context
|
# Include metadata for dashboard selection context
|
||||||
smartsend(
|
env, env_json_str = smartsend(
|
||||||
"dashboard.selection",
|
"dashboard.selection",
|
||||||
[("options_table", options_df, "table")],
|
[("options_table", options_df, "table")],
|
||||||
nats_url="nats://localhost:4222",
|
broker_url="nats://localhost:4222",
|
||||||
metadata=Dict("context" => "user_selection")
|
metadata=Dict("context" => "user_selection")
|
||||||
)
|
)
|
||||||
|
# env: msg_envelope_v1 with all metadata and payloads
|
||||||
|
# env_json_str: JSON string for publishing
|
||||||
```
|
```
|
||||||
|
|
||||||
**JavaScript (Receiver):**
|
**Use Case:** Julia server generates a list of available options (e.g., file selections, configuration presets) as a small DataFrame and sends to a receiving application for user selection. The selection is then sent back to Julia for processing.
|
||||||
```javascript
|
|
||||||
const { smartreceive, smartsend } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
// Receive NATS message with direct transport
|
|
||||||
const result = await smartreceive(msg);
|
|
||||||
|
|
||||||
// Decode Base64 payload (for direct transport)
|
|
||||||
// For tables, data is an array of objects
|
|
||||||
const table = result; // Array of objects
|
|
||||||
|
|
||||||
// User makes selection
|
|
||||||
const selection = uiComponent.getSelectedOption();
|
|
||||||
|
|
||||||
// Send selection back to Julia
|
|
||||||
await smartsend("dashboard.response", [
|
|
||||||
{ dataname: "selected_option", data: selection, type: "dictionary" }
|
|
||||||
]);
|
|
||||||
```
|
|
||||||
|
|
||||||
**Use Case:** Julia server generates a list of available options (e.g., file selections, configuration presets) as a small DataFrame and sends to JavaScript dashboard for user selection. The selection is then sent back to Julia for processing.
|
|
||||||
|
|
||||||
### Scenario 6: Chat System
|
### Scenario 6: Chat System
|
||||||
|
|
||||||
@@ -421,7 +495,6 @@ await smartsend("dashboard.response", [
|
|||||||
**Julia (Sender/Receiver):**
|
**Julia (Sender/Receiver):**
|
||||||
```julia
|
```julia
|
||||||
using NATSBridge
|
using NATSBridge
|
||||||
using DataFrames
|
|
||||||
|
|
||||||
# Build chat message with mixed payloads:
|
# Build chat message with mixed payloads:
|
||||||
# - Text: direct transport (Base64)
|
# - Text: direct transport (Base64)
|
||||||
@@ -445,58 +518,20 @@ chat_message = [
|
|||||||
("large_document", large_file_bytes, "binary") # Large file, link transport
|
("large_document", large_file_bytes, "binary") # Large file, link transport
|
||||||
]
|
]
|
||||||
|
|
||||||
smartsend(
|
env, env_json_str = smartsend(
|
||||||
"chat.room123",
|
"chat.room123",
|
||||||
chat_message,
|
chat_message,
|
||||||
nats_url="nats://localhost:4222",
|
broker_url="nats://localhost:4222",
|
||||||
msg_purpose="chat",
|
msg_purpose="chat",
|
||||||
reply_to="chat.room123.responses"
|
reply_to="chat.room123.responses"
|
||||||
)
|
)
|
||||||
```
|
# env: msg_envelope_v1 with all metadata and payloads
|
||||||
|
# env_json_str: JSON string for publishing
|
||||||
**JavaScript (Sender/Receiver):**
|
|
||||||
```javascript
|
|
||||||
const { smartsend, smartreceive } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
// Build chat message with mixed content:
|
|
||||||
// - User input text: direct transport
|
|
||||||
// - Selected image: check size, use appropriate transport
|
|
||||||
// - Audio recording: link transport for large files
|
|
||||||
// - File attachment: link transport
|
|
||||||
//
|
|
||||||
// Parse received message:
|
|
||||||
// - Direct payloads: decode Base64
|
|
||||||
// - Link payloads: fetch from HTTP with exponential backoff
|
|
||||||
// - Deserialize all payloads appropriately
|
|
||||||
//
|
|
||||||
// Render mixed content in chat interface
|
|
||||||
// Support bidirectional reply with claim-check delivery confirmation
|
|
||||||
|
|
||||||
// Example: Send chat with mixed content
|
|
||||||
const message = [
|
|
||||||
{
|
|
||||||
dataname: "text",
|
|
||||||
data: "Hello from JavaScript!",
|
|
||||||
type: "text"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
dataname: "image",
|
|
||||||
data: selectedImageBuffer, // Small image (ArrayBuffer or Uint8Array)
|
|
||||||
type: "image"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
dataname: "audio",
|
|
||||||
data: audioUrl, // Large audio, link transport
|
|
||||||
type: "audio"
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
await smartsend("chat.room123", message);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Use Case:** Full-featured chat system supporting rich media. User can send text, small images directly, or upload large files that get uploaded to HTTP server and referenced via URLs. Claim-check pattern ensures reliable delivery tracking for all message components.
|
**Use Case:** Full-featured chat system supporting rich media. User can send text, small images directly, or upload large files that get uploaded to HTTP server and referenced via URLs. Claim-check pattern ensures reliable delivery tracking for all message components.
|
||||||
|
|
||||||
**Implementation Note:** The `smartreceive` function iterates through all payloads in the envelope and processes each according to its transport type. See the standard API format in Section 1: `msgEnvelope_v1` supports `AbstractArray{msgPayload_v1}` for multiple payloads.
|
**Implementation Note:** The `smartreceive` function iterates through all payloads in the envelope and processes each according to its transport type. See the standard API format in Section 1: `msg_envelope_v1` supports `Vector{msg_payload_v1}` for multiple payloads.
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
@@ -512,19 +547,19 @@ await smartsend("chat.room123", message);
|
|||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"correlationId": "uuid-v4-string",
|
"correlation_id": "uuid-v4-string",
|
||||||
"msgId": "uuid-v4-string",
|
"msg_id": "uuid-v4-string",
|
||||||
"timestamp": "2024-01-15T10:30:00Z",
|
"timestamp": "2024-01-15T10:30:00Z",
|
||||||
|
|
||||||
"sendTo": "topic/subject",
|
"send_to": "topic/subject",
|
||||||
"msgPurpose": "ACK | NACK | updateStatus | shutdown | chat",
|
"msg_purpose": "ACK | NACK | updateStatus | shutdown | chat",
|
||||||
"senderName": "agent-wine-web-frontend",
|
"sender_name": "agent-wine-web-frontend",
|
||||||
"senderId": "uuid4",
|
"sender_id": "uuid4",
|
||||||
"receiverName": "agent-backend",
|
"receiver_name": "agent-backend",
|
||||||
"receiverId": "uuid4",
|
"receiver_id": "uuid4",
|
||||||
"replyTo": "topic",
|
"reply_to": "topic",
|
||||||
"replyToMsgId": "uuid4",
|
"reply_to_msg_id": "uuid4",
|
||||||
"BrokerURL": "nats://localhost:4222",
|
"broker_url": "nats://localhost:4222",
|
||||||
|
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"content_type": "application/octet-stream",
|
"content_type": "application/octet-stream",
|
||||||
@@ -535,7 +570,7 @@ await smartsend("chat.room123", message);
|
|||||||
{
|
{
|
||||||
"id": "uuid4",
|
"id": "uuid4",
|
||||||
"dataname": "login_image",
|
"dataname": "login_image",
|
||||||
"type": "image",
|
"payload_type": "image",
|
||||||
"transport": "direct",
|
"transport": "direct",
|
||||||
"encoding": "base64",
|
"encoding": "base64",
|
||||||
"size": 15433,
|
"size": 15433,
|
||||||
@@ -558,7 +593,6 @@ await smartsend("chat.room123", message);
|
|||||||
### Exponential Backoff
|
### Exponential Backoff
|
||||||
- Maximum retry count: 5
|
- Maximum retry count: 5
|
||||||
- Base delay: 100ms, max delay: 5000ms
|
- Base delay: 100ms, max delay: 5000ms
|
||||||
- Implemented in both Julia and JavaScript implementations
|
|
||||||
|
|
||||||
### Correlation ID Logging
|
### Correlation ID Logging
|
||||||
- Log correlation_id at every stage
|
- Log correlation_id at every stage
|
||||||
@@ -567,14 +601,30 @@ await smartsend("chat.room123", message);
|
|||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
|
|
||||||
Run the test scripts:
|
Run the test scripts for Julia:
|
||||||
|
|
||||||
|
### Julia Tests
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Scenario 1: Command & Control (JavaScript sender)
|
# Text message exchange
|
||||||
node test/scenario1_command_control.js
|
julia test/test_julia_to_julia_text_sender.jl
|
||||||
|
julia test/test_julia_to_julia_text_receiver.jl
|
||||||
|
|
||||||
# Scenario 2: Large Arrow Table (JavaScript sender)
|
# Dictionary exchange
|
||||||
node test/scenario2_large_table.js
|
julia test/test_julia_to_julia_dict_sender.jl
|
||||||
|
julia test/test_julia_to_julia_dict_receiver.jl
|
||||||
|
|
||||||
|
# File transfer
|
||||||
|
julia test/test_julia_to_julia_file_sender.jl
|
||||||
|
julia test/test_julia_to_julia_file_receiver.jl
|
||||||
|
|
||||||
|
# Mixed payload types
|
||||||
|
julia test/test_julia_to_julia_mix_payloads_sender.jl
|
||||||
|
julia test/test_julia_to_julia_mix_payloads_receiver.jl
|
||||||
|
|
||||||
|
# Table exchange
|
||||||
|
julia test/test_julia_to_julia_table_sender.jl
|
||||||
|
julia test/test_julia_to_julia_table_receiver.jl
|
||||||
```
|
```
|
||||||
|
|
||||||
## Troubleshooting
|
## Troubleshooting
|
||||||
@@ -583,11 +633,10 @@ node test/scenario2_large_table.js
|
|||||||
|
|
||||||
1. **NATS Connection Failed**
|
1. **NATS Connection Failed**
|
||||||
- Ensure NATS server is running
|
- Ensure NATS server is running
|
||||||
- Check NATS_URL configuration
|
|
||||||
|
|
||||||
2. **HTTP Upload Failed**
|
2. **HTTP Upload Failed**
|
||||||
- Ensure file server is running
|
- Ensure file server is running
|
||||||
- Check FILESERVER_URL configuration
|
- Check `fileserver_url` configuration
|
||||||
- Verify upload permissions
|
- Verify upload permissions
|
||||||
|
|
||||||
3. **Arrow IPC Deserialization Error**
|
3. **Arrow IPC Deserialization Error**
|
||||||
|
|||||||
24
etc.jl
24
etc.jl
@@ -1,21 +1,9 @@
|
|||||||
Check architecture.jl, NATSBridge.jl and its test files:
|
Task: Update README.md to reflect recent changes in NATSbridge package.
|
||||||
- test_julia_to_julia_table_receiver.jl
|
|
||||||
- test_julia_to_julia_table_sender.jl.
|
|
||||||
|
|
||||||
Now I want to test sending a mix-content message from Julia serviceA to Julia serviceB, for example, a chat system.
|
|
||||||
The test message must show that any combination and any number and any data size of text | json | table | image | audio | video | binary can be send and receive.
|
|
||||||
|
|
||||||
Can you write me the following test files:
|
|
||||||
- test_julia_to_julia_mix_receiver.jl
|
|
||||||
- test_julia_to_julia_mix_sender.jl
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
1. create a tutorial file "tutorial_julia.md" for NATSBridge.jl
|
|
||||||
2. create a walkthrough file "walkthrough_julia.md" for NATSBridge.jl
|
|
||||||
|
|
||||||
You may consult architecture.md for more info.
|
|
||||||
|
|
||||||
|
Context: the package has been updated with the NATS_connection keyword and the publish_message function.
|
||||||
|
|
||||||
|
Requirements:
|
||||||
|
|
||||||
|
Source of Truth: Treat the updated NATSbridge code as the definitive source. Update README.md to align exactly with these changes.
|
||||||
|
API Consistency: Ensure the Main Package API (e.g., smartsend(), publish_message()) uses consistent naming across all three supported languages.
|
||||||
|
Ecosystem Variance: Low-level native functions (e.g., NATS.connect(), JSON.read()) should follow the conventions of the specific language ecosystem and do not require cross-language consistency.
|
||||||
@@ -1,221 +0,0 @@
|
|||||||
"""
|
|
||||||
Micropython NATS Bridge - Simple Example
|
|
||||||
|
|
||||||
This example demonstrates the basic usage of the NATSBridge for Micropython.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
sys.path.insert(0, "../src")
|
|
||||||
|
|
||||||
from nats_bridge import smartsend, smartreceive, log_trace
|
|
||||||
import json
|
|
||||||
|
|
||||||
|
|
||||||
def example_simple_chat():
|
|
||||||
"""
|
|
||||||
Simple chat example: Send text messages via NATS.
|
|
||||||
|
|
||||||
Sender (this script):
|
|
||||||
- Sends a text message to NATS
|
|
||||||
- Uses direct transport (no fileserver needed)
|
|
||||||
|
|
||||||
Receiver (separate script):
|
|
||||||
- Listens to NATS
|
|
||||||
- Receives and processes the message
|
|
||||||
"""
|
|
||||||
print("=== Simple Chat Example ===")
|
|
||||||
print()
|
|
||||||
|
|
||||||
# Define the message data as list of (dataname, data, type) tuples
|
|
||||||
data = [
|
|
||||||
("message", "Hello from Micropython!", "text")
|
|
||||||
]
|
|
||||||
|
|
||||||
# Send the message
|
|
||||||
env = smartsend(
|
|
||||||
"/chat/room1",
|
|
||||||
data,
|
|
||||||
nats_url="nats://localhost:4222",
|
|
||||||
msg_purpose="chat",
|
|
||||||
sender_name="micropython-client"
|
|
||||||
)
|
|
||||||
|
|
||||||
print("Message sent!")
|
|
||||||
print(" Subject: {}".format(env.send_to))
|
|
||||||
print(" Correlation ID: {}".format(env.correlation_id))
|
|
||||||
print(" Payloads: {}".format(len(env.payloads)))
|
|
||||||
print()
|
|
||||||
|
|
||||||
# Expected receiver output:
|
|
||||||
print("Expected receiver output:")
|
|
||||||
print(" [timestamp] [Correlation: ...] Starting smartsend for subject: /chat/room1")
|
|
||||||
print(" [timestamp] [Correlation: ...] Serialized payload 'message' (type: text) size: 22 bytes")
|
|
||||||
print(" [timestamp] [Correlation: ...] Using direct transport for 22 bytes")
|
|
||||||
print(" [timestamp] [Correlation: ...] Message published to /chat/room1")
|
|
||||||
print()
|
|
||||||
|
|
||||||
return env
|
|
||||||
|
|
||||||
|
|
||||||
def example_send_json():
|
|
||||||
"""
|
|
||||||
Example: Send JSON configuration to a Micropython device.
|
|
||||||
|
|
||||||
This demonstrates sending structured data (dictionary type).
|
|
||||||
"""
|
|
||||||
print("\n=== Send JSON Configuration ===")
|
|
||||||
print()
|
|
||||||
|
|
||||||
# Define configuration as dictionary
|
|
||||||
config = {
|
|
||||||
"wifi_ssid": "MyNetwork",
|
|
||||||
"wifi_password": "password123",
|
|
||||||
"server_host": "mqtt.example.com",
|
|
||||||
"server_port": 1883,
|
|
||||||
"update_interval": 60
|
|
||||||
}
|
|
||||||
|
|
||||||
# Send configuration
|
|
||||||
data = [
|
|
||||||
("device_config", config, "dictionary")
|
|
||||||
]
|
|
||||||
|
|
||||||
env = smartsend(
|
|
||||||
"/device/config",
|
|
||||||
data,
|
|
||||||
nats_url="nats://localhost:4222",
|
|
||||||
msg_purpose="updateStatus",
|
|
||||||
sender_name="server"
|
|
||||||
)
|
|
||||||
|
|
||||||
print("Configuration sent!")
|
|
||||||
print(" Subject: {}".format(env.send_to))
|
|
||||||
print(" Payloads: {}".format(len(env.payloads)))
|
|
||||||
print()
|
|
||||||
|
|
||||||
return env
|
|
||||||
|
|
||||||
|
|
||||||
def example_receive_message(msg):
|
|
||||||
"""
|
|
||||||
Example: Receive and process a NATS message.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
msg: The NATS message received (should be dict or JSON string)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
list: List of (dataname, data, type) tuples
|
|
||||||
"""
|
|
||||||
print("\n=== Receive Message ===")
|
|
||||||
print()
|
|
||||||
|
|
||||||
# Process the message
|
|
||||||
payloads = smartreceive(
|
|
||||||
msg,
|
|
||||||
fileserver_download_handler=None, # Not needed for direct transport
|
|
||||||
max_retries=3,
|
|
||||||
base_delay=100,
|
|
||||||
max_delay=1000
|
|
||||||
)
|
|
||||||
|
|
||||||
print("Received {} payload(s):".format(len(payloads)))
|
|
||||||
for dataname, data, type in payloads:
|
|
||||||
print(" - {}: {} (type: {})".format(dataname, data, type))
|
|
||||||
|
|
||||||
return payloads
|
|
||||||
|
|
||||||
|
|
||||||
def example_mixed_content():
|
|
||||||
"""
|
|
||||||
Example: Send mixed content (text + dictionary + binary).
|
|
||||||
|
|
||||||
This demonstrates the multi-payload capability.
|
|
||||||
"""
|
|
||||||
print("\n=== Mixed Content Example ===")
|
|
||||||
print()
|
|
||||||
|
|
||||||
# Create mixed content
|
|
||||||
image_data = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR" # Example PNG header
|
|
||||||
|
|
||||||
data = [
|
|
||||||
("message_text", "Hello with image!", "text"),
|
|
||||||
("user_config", {"theme": "dark", "notifications": True}, "dictionary"),
|
|
||||||
("user_avatar", image_data, "binary")
|
|
||||||
]
|
|
||||||
|
|
||||||
env = smartsend(
|
|
||||||
"/chat/mixed",
|
|
||||||
data,
|
|
||||||
nats_url="nats://localhost:4222",
|
|
||||||
msg_purpose="chat",
|
|
||||||
sender_name="micropython-client"
|
|
||||||
)
|
|
||||||
|
|
||||||
print("Mixed content sent!")
|
|
||||||
print(" Subject: {}".format(env.send_to))
|
|
||||||
print(" Payloads:")
|
|
||||||
for p in env.payloads:
|
|
||||||
print(" - {} (transport: {}, type: {}, size: {} bytes)".format(
|
|
||||||
p.dataname, p.transport, p.type, p.size))
|
|
||||||
|
|
||||||
return env
|
|
||||||
|
|
||||||
|
|
||||||
def example_reply():
|
|
||||||
"""
|
|
||||||
Example: Send a message with reply-to functionality.
|
|
||||||
|
|
||||||
This demonstrates request-response pattern.
|
|
||||||
"""
|
|
||||||
print("\n=== Request-Response Example ===")
|
|
||||||
print()
|
|
||||||
|
|
||||||
# Send command
|
|
||||||
data = [
|
|
||||||
("command", {"action": "read_sensor", "sensor_id": "temp1"}, "dictionary")
|
|
||||||
]
|
|
||||||
|
|
||||||
env = smartsend(
|
|
||||||
"/device/command",
|
|
||||||
data,
|
|
||||||
nats_url="nats://localhost:4222",
|
|
||||||
msg_purpose="command",
|
|
||||||
sender_name="server",
|
|
||||||
reply_to="/device/response",
|
|
||||||
reply_to_msg_id="cmd-001"
|
|
||||||
)
|
|
||||||
|
|
||||||
print("Command sent!")
|
|
||||||
print(" Subject: {}".format(env.send_to))
|
|
||||||
print(" Reply To: {}".format(env.reply_to))
|
|
||||||
print(" Reply To Msg ID: {}".format(env.reply_to_msg_id))
|
|
||||||
print()
|
|
||||||
|
|
||||||
print("Expected receiver behavior:")
|
|
||||||
print(" 1. Receive command on /device/command")
|
|
||||||
print(" 2. Process command")
|
|
||||||
print(" 3. Send response to /device/response")
|
|
||||||
print(" 4. Include replyToMsgId in response")
|
|
||||||
|
|
||||||
return env
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
print("Micropython NATS Bridge Examples")
|
|
||||||
print("================================")
|
|
||||||
print()
|
|
||||||
|
|
||||||
# Run examples
|
|
||||||
example_simple_chat()
|
|
||||||
example_send_json()
|
|
||||||
example_mixed_content()
|
|
||||||
example_reply()
|
|
||||||
|
|
||||||
print("\n=== Examples Completed ===")
|
|
||||||
print()
|
|
||||||
print("To run these examples, you need:")
|
|
||||||
print(" 1. A running NATS server at nats://localhost:4222")
|
|
||||||
print(" 2. Import the nats_bridge module")
|
|
||||||
print(" 3. Call the desired example function")
|
|
||||||
print()
|
|
||||||
print("For more examples, see test/test_micropython_basic.py")
|
|
||||||
304
examples/tutorial.md
Normal file
304
examples/tutorial.md
Normal file
@@ -0,0 +1,304 @@
|
|||||||
|
# NATSBridge Tutorial
|
||||||
|
|
||||||
|
A step-by-step guide to get started with NATSBridge - a high-performance, bi-directional data bridge for **Julia**.
|
||||||
|
|
||||||
|
## 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 for Julia applications through NATS, with automatic transport selection based on payload size:
|
||||||
|
|
||||||
|
- **Direct Transport**: Payloads < 1MB are sent directly via NATS (Base64 encoded)
|
||||||
|
- **Link Transport**: Payloads >= 1MB are uploaded to an HTTP file server and referenced via URL
|
||||||
|
|
||||||
|
### Supported Payload Types
|
||||||
|
|
||||||
|
| Type | Description |
|
||||||
|
|------|-------------|
|
||||||
|
| `text` | Plain text strings |
|
||||||
|
| `dictionary` | JSON-serializable dictionaries |
|
||||||
|
| `table` | Tabular data (Arrow IPC format) |
|
||||||
|
| `image` | Image data (PNG, JPG bytes) |
|
||||||
|
| `audio` | Audio data (WAV, MP3 bytes) |
|
||||||
|
| `video` | Video data (MP4, AVI bytes) |
|
||||||
|
| `binary` | Generic binary data |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
Before you begin, ensure you have:
|
||||||
|
|
||||||
|
1. **NATS Server** running (or accessible)
|
||||||
|
2. **HTTP File Server** (optional, for large payloads > 1MB)
|
||||||
|
3. **Julia** with required packages
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
### Julia
|
||||||
|
|
||||||
|
```julia
|
||||||
|
using Pkg
|
||||||
|
Pkg.add("NATS")
|
||||||
|
Pkg.add("Arrow")
|
||||||
|
Pkg.add("JSON3")
|
||||||
|
Pkg.add("HTTP")
|
||||||
|
Pkg.add("UUIDs")
|
||||||
|
Pkg.add("Dates")
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
|
||||||
|
### Step 1: Start NATS Server
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run -p 4222:4222 nats:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 2: Start HTTP File Server (Optional)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Create a directory for file uploads
|
||||||
|
mkdir -p /tmp/fileserver
|
||||||
|
|
||||||
|
# Use any HTTP server that supports POST for file uploads
|
||||||
|
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 object with all metadata and payloads
|
||||||
|
# env_json_str: JSON string representation of the envelope for publishing
|
||||||
|
println("Message sent!")
|
||||||
|
|
||||||
|
# Or use is_publish=false to get envelope and JSON without publishing
|
||||||
|
env, env_json_str = smartsend("/chat/room1", data, broker_url="nats://localhost:4222", is_publish=false)
|
||||||
|
# env: msg_envelope_v1 object
|
||||||
|
# env_json_str: JSON string for publishing to NATS
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 4: Receive Messages
|
||||||
|
|
||||||
|
#### Julia
|
||||||
|
|
||||||
|
```julia
|
||||||
|
using NATSBridge
|
||||||
|
|
||||||
|
# Receive and process message
|
||||||
|
env = smartreceive(msg; fileserver_download_handler=_fetch_with_backoff)
|
||||||
|
for (dataname, data, type) in env["payloads"]
|
||||||
|
println("Received $dataname: $data")
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 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")
|
||||||
|
```
|
||||||
|
|
||||||
|
### 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")
|
||||||
|
```
|
||||||
|
|
||||||
|
### 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"
|
||||||
|
)
|
||||||
|
# env: msg_envelope_v1 object
|
||||||
|
# env_json_str: JSON string for publishing to NATS
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Julia (Responder)
|
||||||
|
|
||||||
|
```julia
|
||||||
|
using NATS, NATSBridge
|
||||||
|
|
||||||
|
# Configuration
|
||||||
|
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)
|
||||||
|
|
||||||
|
# Extract reply_to from the envelope metadata
|
||||||
|
reply_to = env["reply_to"]
|
||||||
|
|
||||||
|
for (dataname, data, type) in env["payloads"]
|
||||||
|
if dataname == "command" && data["action"] == "read_sensor"
|
||||||
|
response = Dict("sensor_id" => "sensor-001", "value" => 42.5)
|
||||||
|
# Send response to the reply_to subject from the request
|
||||||
|
if !isempty(reply_to)
|
||||||
|
smartsend(reply_to, [("data", response, "dictionary")])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
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"
|
||||||
|
)
|
||||||
|
|
||||||
|
# The envelope will contain the download URL
|
||||||
|
println("File uploaded to: $(env.payloads[1].data)")
|
||||||
|
```
|
||||||
|
|
||||||
|
### 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")
|
||||||
|
```
|
||||||
|
|
||||||
|
### 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, "table")]
|
||||||
|
env, env_json_str = smartsend("/data/students", data, broker_url="nats://localhost:4222")
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Next Steps
|
||||||
|
|
||||||
|
1. **Explore the test directory** for more examples
|
||||||
|
2. **Check the documentation** for advanced configuration options
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 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 (Vector{UInt8})
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
MIT
|
||||||
703
examples/walkthrough.md
Normal file
703
examples/walkthrough.md
Normal file
@@ -0,0 +1,703 @@
|
|||||||
|
# NATSBridge Walkthrough
|
||||||
|
|
||||||
|
A comprehensive guide to building real-world applications with NATSBridge.
|
||||||
|
|
||||||
|
## Table of Contents
|
||||||
|
|
||||||
|
1. [Introduction](#introduction)
|
||||||
|
2. [Architecture Overview](#architecture-overview)
|
||||||
|
3. [Building a Chat Application](#building-a-chat-application)
|
||||||
|
4. [Building a File Transfer System](#building-a-file-transfer-system)
|
||||||
|
5. [Building a Streaming Data Pipeline](#building-a-streaming-data-pipeline)
|
||||||
|
6. [Performance Optimization](#performance-optimimization)
|
||||||
|
7. [Best Practices](#best-practices)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
This walkthrough will guide you through building several real-world applications using NATSBridge. We'll cover:
|
||||||
|
|
||||||
|
- Chat applications with rich media support
|
||||||
|
- File transfer systems with claim-check pattern
|
||||||
|
- Streaming data pipelines
|
||||||
|
|
||||||
|
Each section builds on the previous one, gradually increasing in complexity.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Architecture Overview
|
||||||
|
|
||||||
|
### System Components
|
||||||
|
|
||||||
|
```
|
||||||
|
┌─────────────────────────────────────────────────────────────────┐
|
||||||
|
│ NATSBridge Architecture │
|
||||||
|
├─────────────────────────────────────────────────────────────────┤
|
||||||
|
│ ┌──────────────┐ ┌──────────────┐ │
|
||||||
|
│ │ Julia │ │ NATS │ │
|
||||||
|
│ │ (NATS.jl) │◄──►│ Server │ │
|
||||||
|
│ └──────────────┘ └──────────────┘ │
|
||||||
|
│ │ │ │
|
||||||
|
│ ▼ ▼ │
|
||||||
|
│ ┌──────────────────────────────────────┐ │
|
||||||
|
│ │ File Server │ │
|
||||||
|
│ │ (HTTP Upload) │ │
|
||||||
|
│ └──────────────────────────────────────┘ │
|
||||||
|
└─────────────────────────────────────────────────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
### Message Flow
|
||||||
|
|
||||||
|
1. **Sender** creates a message envelope with payloads
|
||||||
|
2. **NATSBridge** serializes and encodes payloads
|
||||||
|
3. **Transport Decision**: Small payloads go directly to NATS, large payloads are uploaded to file server
|
||||||
|
4. **NATS** routes messages to subscribers
|
||||||
|
5. **Receiver** fetches payloads (from NATS or file server)
|
||||||
|
6. **NATSBridge** deserializes and decodes payloads
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Building a Chat Application
|
||||||
|
|
||||||
|
Let's build a full-featured chat application that supports text, images, and file attachments.
|
||||||
|
|
||||||
|
### Step 1: Set Up the Project
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Create project directory
|
||||||
|
mkdir -p chat-app/src
|
||||||
|
cd chat-app
|
||||||
|
|
||||||
|
# Create configuration file
|
||||||
|
cat > config.json << 'EOF'
|
||||||
|
{
|
||||||
|
"nats_url": "nats://localhost:4222",
|
||||||
|
"fileserver_url": "http://localhost:8080",
|
||||||
|
"size_threshold": 1048576
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 2: Create the Chat Interface (Julia)
|
||||||
|
|
||||||
|
```julia
|
||||||
|
# src/chat_ui.jl
|
||||||
|
using NATSBridge, NATS
|
||||||
|
|
||||||
|
struct ChatUI
|
||||||
|
messages::Vector{Dict}
|
||||||
|
current_room::String
|
||||||
|
end
|
||||||
|
|
||||||
|
function ChatUI()
|
||||||
|
ChatUI(Dict[], "")
|
||||||
|
end
|
||||||
|
|
||||||
|
function send_message(ui::ChatUI, message_input::String, selected_file::Union{Nothing, String})
|
||||||
|
data = []
|
||||||
|
|
||||||
|
# Add text message
|
||||||
|
if !isempty(message_input)
|
||||||
|
push!(data, ("text", message_input, "text"))
|
||||||
|
end
|
||||||
|
|
||||||
|
# Add file if selected
|
||||||
|
if selected_file !== nothing
|
||||||
|
file_data = read(selected_file)
|
||||||
|
file_type = get_file_type(selected_file)
|
||||||
|
push!(data, ("attachment", file_data, file_type))
|
||||||
|
end
|
||||||
|
|
||||||
|
return data
|
||||||
|
end
|
||||||
|
|
||||||
|
function get_file_type(filename::String)::String
|
||||||
|
if endswith(filename, ".png") || endswith(filename, ".jpg")
|
||||||
|
return "image"
|
||||||
|
elseif endswith(filename, ".mp3") || endswith(filename, ".wav")
|
||||||
|
return "audio"
|
||||||
|
elseif endswith(filename, ".mp4") || endswith(filename, ".avi")
|
||||||
|
return "video"
|
||||||
|
else
|
||||||
|
return "binary"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function add_message(ui::ChatUI, user::String, text::String, attachment::Union{Nothing, Dict})
|
||||||
|
push!(ui.messages, Dict(
|
||||||
|
"user" => user,
|
||||||
|
"text" => text,
|
||||||
|
"attachment" => attachment
|
||||||
|
))
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 3: Create the Message Handler
|
||||||
|
|
||||||
|
```julia
|
||||||
|
# src/chat_handler.jl
|
||||||
|
using NATSBridge, NATS
|
||||||
|
|
||||||
|
struct ChatHandler
|
||||||
|
nats::NATS.Connection
|
||||||
|
ui::ChatUI
|
||||||
|
end
|
||||||
|
|
||||||
|
function ChatHandler(nats_connection::NATS.Connection)
|
||||||
|
ChatHandler(nats_connection, ChatUI())
|
||||||
|
end
|
||||||
|
|
||||||
|
function start(handler::ChatHandler)
|
||||||
|
# Subscribe to chat rooms
|
||||||
|
rooms = ["general", "tech", "random"]
|
||||||
|
|
||||||
|
for room in rooms
|
||||||
|
NATS.subscribe(handler.nats, "/chat/$room") do msg
|
||||||
|
handle_message(handler, msg)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
println("Chat handler started")
|
||||||
|
end
|
||||||
|
|
||||||
|
function handle_message(handler::ChatHandler, msg::NATS.Msg)
|
||||||
|
env = smartreceive(msg, fileserver_download_handler=_fetch_with_backoff)
|
||||||
|
|
||||||
|
# Extract sender info from envelope
|
||||||
|
sender = get(env, "sender_name", "Anonymous")
|
||||||
|
|
||||||
|
# Process each payload
|
||||||
|
for (dataname, data, type) in env["payloads"]
|
||||||
|
if type == "text"
|
||||||
|
add_message(handler.ui, sender, data, nothing)
|
||||||
|
elseif type == "image"
|
||||||
|
# Convert to data URL for display
|
||||||
|
base64_data = base64encode(data)
|
||||||
|
attachment = Dict(
|
||||||
|
"type" => "image",
|
||||||
|
"data" => "data:image/png;base64,$base64_data"
|
||||||
|
)
|
||||||
|
add_message(handler.ui, sender, "", attachment)
|
||||||
|
else
|
||||||
|
# For other types, use file server URL
|
||||||
|
attachment = Dict("type" => type, "data" => data)
|
||||||
|
add_message(handler.ui, sender, "", attachment)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function download_file(url::String, max_retries::Int, base_delay::Int, max_delay::Int, correlation_id::String)::Vector{UInt8}
|
||||||
|
# Implement exponential backoff for file server downloads
|
||||||
|
# Return downloaded data as Vector{UInt8}
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 4: Run the Application
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Start NATS
|
||||||
|
docker run -p 4222:4222 nats:latest
|
||||||
|
|
||||||
|
# Start file server
|
||||||
|
mkdir -p /tmp/fileserver
|
||||||
|
python3 -m http.server 8080 --directory /tmp/fileserver
|
||||||
|
|
||||||
|
# Run chat app
|
||||||
|
julia src/chat_ui.jl
|
||||||
|
julia src/chat_handler.jl
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Building a File Transfer System
|
||||||
|
|
||||||
|
Let's build a file transfer system that handles large files efficiently.
|
||||||
|
|
||||||
|
### Step 1: File Upload Service (Julia)
|
||||||
|
|
||||||
|
```julia
|
||||||
|
# src/file_upload_service.jl
|
||||||
|
using NATSBridge, HTTP
|
||||||
|
|
||||||
|
struct FileUploadService
|
||||||
|
broker_url::String
|
||||||
|
fileserver_url::String
|
||||||
|
end
|
||||||
|
|
||||||
|
function FileUploadService(broker_url::String, fileserver_url::String)
|
||||||
|
FileUploadService(broker_url, fileserver_url)
|
||||||
|
end
|
||||||
|
|
||||||
|
function upload_file(service::FileUploadService, file_path::String, recipient::String)::Dict
|
||||||
|
file_data = read(file_path)
|
||||||
|
file_name = basename(file_path)
|
||||||
|
|
||||||
|
data = [("file", file_data, "binary")]
|
||||||
|
|
||||||
|
env, env_json_str = smartsend(
|
||||||
|
"/files/$recipient",
|
||||||
|
data,
|
||||||
|
broker_url=service.broker_url,
|
||||||
|
fileserver_url=service.fileserver_url
|
||||||
|
)
|
||||||
|
|
||||||
|
return env
|
||||||
|
end
|
||||||
|
|
||||||
|
function upload_large_file(service::FileUploadService, file_path::String, recipient::String)::Dict
|
||||||
|
file_size = stat(file_path).size
|
||||||
|
|
||||||
|
if file_size > 100 * 1024 * 1024 # > 100MB
|
||||||
|
println("File too large for direct upload, using streaming...")
|
||||||
|
return stream_upload(service, file_path, recipient)
|
||||||
|
end
|
||||||
|
|
||||||
|
return upload_file(service, file_path, recipient)
|
||||||
|
end
|
||||||
|
|
||||||
|
function stream_upload(service::FileUploadService, file_path::String, recipient::String)::Dict
|
||||||
|
# Implement streaming upload to file server
|
||||||
|
# This would require a more sophisticated file server
|
||||||
|
# For now, we'll use the standard upload
|
||||||
|
return upload_file(service, file_path, recipient)
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 2: File Download Service (Julia)
|
||||||
|
|
||||||
|
```julia
|
||||||
|
# src/file_download_service.jl
|
||||||
|
using NATSBridge
|
||||||
|
|
||||||
|
struct FileDownloadService
|
||||||
|
nats_url::String
|
||||||
|
end
|
||||||
|
|
||||||
|
function FileDownloadService(nats_url::String)
|
||||||
|
FileDownloadService(nats_url)
|
||||||
|
end
|
||||||
|
|
||||||
|
function download_file(service::FileDownloadService, msg::NATS.Msg, sender::String, download_id::String)
|
||||||
|
# Subscribe to sender's file channel
|
||||||
|
env = smartreceive(msg, fileserver_download_handler=fetch_from_url)
|
||||||
|
|
||||||
|
# Process each payload
|
||||||
|
for (dataname, data, type) in env["payloads"]
|
||||||
|
if type == "binary"
|
||||||
|
file_path = "/downloads/$dataname"
|
||||||
|
write(file_path, data)
|
||||||
|
println("File saved to $file_path")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function fetch_from_url(url::String, max_retries::Int, base_delay::Int, max_delay::Int, correlation_id::String)::Vector{UInt8}
|
||||||
|
# Fetch data from URL with exponential backoff
|
||||||
|
# Return downloaded data as Vector{UInt8}
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 3: File Transfer CLI (Julia)
|
||||||
|
|
||||||
|
```julia
|
||||||
|
# src/cli.jl
|
||||||
|
using NATSBridge, Readlines, FileIO
|
||||||
|
|
||||||
|
function main()
|
||||||
|
config = JSON3.read(read("config.json", String))
|
||||||
|
|
||||||
|
println("File Transfer System")
|
||||||
|
println("====================")
|
||||||
|
println("1. Upload file")
|
||||||
|
println("2. Download file")
|
||||||
|
println("3. List pending downloads")
|
||||||
|
|
||||||
|
print("Enter choice: ")
|
||||||
|
choice = readline()
|
||||||
|
|
||||||
|
if choice == "1"
|
||||||
|
upload_file_cli(config)
|
||||||
|
elseif choice == "2"
|
||||||
|
download_file_cli(config)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function upload_file_cli(config)
|
||||||
|
print("Enter file path: ")
|
||||||
|
file_path = readline()
|
||||||
|
|
||||||
|
print("Enter recipient: ")
|
||||||
|
recipient = readline()
|
||||||
|
|
||||||
|
file_service = FileUploadService(config.nats_url, config.fileserver_url)
|
||||||
|
|
||||||
|
try
|
||||||
|
env = upload_file(file_service, file_path, recipient)
|
||||||
|
println("Upload successful!")
|
||||||
|
println("File ID: $(env["payloads"][1][1])")
|
||||||
|
catch error
|
||||||
|
println("Upload failed: $(error)")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function download_file_cli(config)
|
||||||
|
print("Enter sender: ")
|
||||||
|
sender = readline()
|
||||||
|
|
||||||
|
file_service = FileDownloadService(config.nats_url)
|
||||||
|
|
||||||
|
try
|
||||||
|
download_file(file_service, sender)
|
||||||
|
println("Download complete!")
|
||||||
|
catch error
|
||||||
|
println("Download failed: $(error)")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
main()
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Building a Streaming Data Pipeline
|
||||||
|
|
||||||
|
Let's build a data pipeline that processes streaming data from sensors.
|
||||||
|
|
||||||
|
### Step 1: Sensor Data Model (Julia)
|
||||||
|
|
||||||
|
```julia
|
||||||
|
# src/sensor_data.jl
|
||||||
|
using Dates, DataFrames
|
||||||
|
|
||||||
|
struct SensorReading
|
||||||
|
sensor_id::String
|
||||||
|
timestamp::String
|
||||||
|
value::Float64
|
||||||
|
unit::String
|
||||||
|
metadata::Dict{String, Any}
|
||||||
|
end
|
||||||
|
|
||||||
|
function SensorReading(sensor_id::String, value::Float64, unit::String, metadata::Dict{String, Any}=Dict())
|
||||||
|
SensorReading(
|
||||||
|
sensor_id,
|
||||||
|
ISODateTime(now(), Dates.Second) |> string,
|
||||||
|
value,
|
||||||
|
unit,
|
||||||
|
metadata
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
struct SensorBatch
|
||||||
|
readings::Vector{SensorReading}
|
||||||
|
end
|
||||||
|
|
||||||
|
function SensorBatch()
|
||||||
|
SensorBatch(SensorReading[])
|
||||||
|
end
|
||||||
|
|
||||||
|
function add_reading(batch::SensorBatch, reading::SensorReading)
|
||||||
|
push!(batch.readings, reading)
|
||||||
|
end
|
||||||
|
|
||||||
|
function to_dataframe(batch::SensorBatch)::DataFrame
|
||||||
|
data = Dict{String, Any}()
|
||||||
|
data["sensor_id"] = [r.sensor_id for r in batch.readings]
|
||||||
|
data["timestamp"] = [r.timestamp for r in batch.readings]
|
||||||
|
data["value"] = [r.value for r in batch.readings]
|
||||||
|
data["unit"] = [r.unit for r in batch.readings]
|
||||||
|
|
||||||
|
return DataFrame(data)
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 2: Sensor Sender (Julia)
|
||||||
|
|
||||||
|
```julia
|
||||||
|
# src/sensor_sender.jl
|
||||||
|
using NATSBridge, Dates, Random
|
||||||
|
|
||||||
|
struct SensorSender
|
||||||
|
broker_url::String
|
||||||
|
fileserver_url::String
|
||||||
|
end
|
||||||
|
|
||||||
|
function SensorSender(broker_url::String, fileserver_url::String)
|
||||||
|
SensorSender(broker_url, fileserver_url)
|
||||||
|
end
|
||||||
|
|
||||||
|
function send_reading(sender::SensorSender, sensor_id::String, value::Float64, unit::String)
|
||||||
|
reading = SensorReading(sensor_id, value, unit)
|
||||||
|
|
||||||
|
data = [("reading", reading.metadata, "dictionary")]
|
||||||
|
|
||||||
|
# Default: is_publish=True (automatically publishes to NATS)
|
||||||
|
smartsend(
|
||||||
|
"/sensors/$sensor_id",
|
||||||
|
data,
|
||||||
|
broker_url=sender.broker_url,
|
||||||
|
fileserver_url=sender.fileserver_url
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
function prepare_message_only(sender::SensorSender, sensor_id::String, value::Float64, unit::String)
|
||||||
|
"""Prepare a message without publishing (is_publish=False)."""
|
||||||
|
reading = SensorReading(sensor_id, value, unit)
|
||||||
|
|
||||||
|
data = [("reading", reading.metadata, "dictionary")]
|
||||||
|
|
||||||
|
# With is_publish=False, returns (env, env_json_str) without publishing
|
||||||
|
env, env_json_str = smartsend(
|
||||||
|
"/sensors/$sensor_id/prepare",
|
||||||
|
data,
|
||||||
|
broker_url=sender.broker_url,
|
||||||
|
fileserver_url=sender.fileserver_url,
|
||||||
|
is_publish=false
|
||||||
|
)
|
||||||
|
|
||||||
|
# Now you can publish manually using NATS request-reply pattern
|
||||||
|
# nc.request(subject, env_json_str, reply_to=reply_to_topic)
|
||||||
|
|
||||||
|
return env, env_json_str
|
||||||
|
end
|
||||||
|
|
||||||
|
function send_batch(sender::SensorSender, readings::Vector{SensorReading})
|
||||||
|
batch = SensorBatch()
|
||||||
|
for reading in readings
|
||||||
|
add_reading(batch, reading)
|
||||||
|
end
|
||||||
|
|
||||||
|
df = to_dataframe(batch)
|
||||||
|
|
||||||
|
# Convert to Arrow IPC format
|
||||||
|
import Arrow
|
||||||
|
table = Arrow.Table(df)
|
||||||
|
|
||||||
|
# Serialize to Arrow IPC
|
||||||
|
import IOBuffer
|
||||||
|
buf = IOBuffer()
|
||||||
|
Arrow.write(buf, table)
|
||||||
|
|
||||||
|
arrow_data = take!(buf)
|
||||||
|
|
||||||
|
# Send based on size
|
||||||
|
if length(arrow_data) < 1048576 # < 1MB
|
||||||
|
data = [("batch", arrow_data, "table")]
|
||||||
|
smartsend(
|
||||||
|
"/sensors/batch",
|
||||||
|
data,
|
||||||
|
broker_url=sender.broker_url,
|
||||||
|
fileserver_url=sender.fileserver_url
|
||||||
|
)
|
||||||
|
else
|
||||||
|
# Upload to file server
|
||||||
|
data = [("batch", arrow_data, "table")]
|
||||||
|
smartsend(
|
||||||
|
"/sensors/batch",
|
||||||
|
data,
|
||||||
|
broker_url=sender.broker_url,
|
||||||
|
fileserver_url=sender.fileserver_url
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 3: Sensor Receiver (Julia)
|
||||||
|
|
||||||
|
```julia
|
||||||
|
# src/sensor_receiver.jl
|
||||||
|
using NATSBridge, Arrow, DataFrames, IOBuffer
|
||||||
|
|
||||||
|
struct SensorReceiver
|
||||||
|
fileserver_download_handler::Function
|
||||||
|
end
|
||||||
|
|
||||||
|
function SensorReceiver(download_handler::Function)
|
||||||
|
SensorReceiver(download_handler)
|
||||||
|
end
|
||||||
|
|
||||||
|
function process_reading(receiver::SensorReceiver, msg::NATS.Msg)
|
||||||
|
env = smartreceive(msg, receiver.fileserver_download_handler)
|
||||||
|
|
||||||
|
for (dataname, data, data_type) in env["payloads"]
|
||||||
|
if data_type == "dictionary"
|
||||||
|
# Process dictionary payload
|
||||||
|
println("Received: $dataname = $data")
|
||||||
|
elseif data_type == "table"
|
||||||
|
# Deserialize Arrow IPC
|
||||||
|
buf = IOBuffer(data)
|
||||||
|
table = Arrow.read(buf)
|
||||||
|
df = DataFrame(table)
|
||||||
|
println("Received batch with $(nrow(df)) readings")
|
||||||
|
println(df)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Performance Optimization
|
||||||
|
|
||||||
|
### 1. Batch Processing
|
||||||
|
|
||||||
|
```julia
|
||||||
|
# Batch multiple readings into a single message
|
||||||
|
function send_batch_readings(sender::SensorSender, readings::Vector{Tuple{String, Float64, String}})
|
||||||
|
batch = SensorBatch()
|
||||||
|
|
||||||
|
for (sensor_id, value, unit) in readings
|
||||||
|
reading = SensorReading(sensor_id, value, unit)
|
||||||
|
add_reading(batch, reading)
|
||||||
|
end
|
||||||
|
|
||||||
|
df = to_dataframe(batch)
|
||||||
|
|
||||||
|
# Convert to Arrow IPC
|
||||||
|
import Arrow
|
||||||
|
table = Arrow.Table(df)
|
||||||
|
|
||||||
|
# Serialize to Arrow IPC
|
||||||
|
import IOBuffer
|
||||||
|
buf = IOBuffer()
|
||||||
|
Arrow.write(buf, table)
|
||||||
|
|
||||||
|
arrow_data = take!(buf)
|
||||||
|
|
||||||
|
# Send as single message
|
||||||
|
smartsend(
|
||||||
|
"/sensors/batch",
|
||||||
|
[("batch", arrow_data, "table")],
|
||||||
|
broker_url=sender.broker_url
|
||||||
|
)
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Connection Reuse
|
||||||
|
|
||||||
|
```julia
|
||||||
|
# Reuse NATS connections
|
||||||
|
function create_connection_pool()
|
||||||
|
connections = Dict{String, NATS.Connection}()
|
||||||
|
|
||||||
|
function get_connection(nats_url::String)::NATS.Connection
|
||||||
|
if !haskey(connections, nats_url)
|
||||||
|
connections[nats_url] = NATS.connect(nats_url)
|
||||||
|
end
|
||||||
|
return connections[nats_url]
|
||||||
|
end
|
||||||
|
|
||||||
|
function close_all()
|
||||||
|
for conn in values(connections)
|
||||||
|
NATS.drain(conn)
|
||||||
|
end
|
||||||
|
empty!(connections)
|
||||||
|
end
|
||||||
|
|
||||||
|
return (get_connection= get_connection, close_all=close_all)
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Caching
|
||||||
|
|
||||||
|
```julia
|
||||||
|
# Cache file server responses
|
||||||
|
using Base.Threads
|
||||||
|
|
||||||
|
const file_cache = Dict{String, Vector{UInt8}}()
|
||||||
|
|
||||||
|
function fetch_with_caching(url::String, max_retries::Int, base_delay::Int, max_delay::Int, correlation_id::String)::Vector{UInt8}
|
||||||
|
if haskey(file_cache, url)
|
||||||
|
return file_cache[url]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Fetch from file server
|
||||||
|
data = _fetch_with_backoff(url, max_retries, base_delay, max_delay, correlation_id)
|
||||||
|
|
||||||
|
# Cache the result
|
||||||
|
file_cache[url] = data
|
||||||
|
|
||||||
|
return data
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Best Practices
|
||||||
|
|
||||||
|
### 1. Error Handling
|
||||||
|
|
||||||
|
```julia
|
||||||
|
function safe_smartsend(subject::String, data::Vector{Tuple}, kwargs...)
|
||||||
|
try
|
||||||
|
return smartsend(subject, data; kwargs...)
|
||||||
|
catch error
|
||||||
|
println("Failed to send message: $(error)")
|
||||||
|
return nothing
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Logging
|
||||||
|
|
||||||
|
```julia
|
||||||
|
using Logging
|
||||||
|
|
||||||
|
function log_send(subject::String, data::Vector{Tuple}, correlation_id::String)
|
||||||
|
@info "Sending to $subject: $(length(data)) payloads, correlation_id=$correlation_id"
|
||||||
|
end
|
||||||
|
|
||||||
|
function log_receive(correlation_id::String, num_payloads::Int)
|
||||||
|
@info "Received message: $num_payloads payloads, correlation_id=$correlation_id"
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Rate Limiting
|
||||||
|
|
||||||
|
```julia
|
||||||
|
using Dates, Collections
|
||||||
|
|
||||||
|
struct RateLimiter
|
||||||
|
max_requests::Int
|
||||||
|
time_window::Float64
|
||||||
|
requests::Deque{Float64}
|
||||||
|
end
|
||||||
|
|
||||||
|
function RateLimiter(max_requests::Int, time_window::Float64)
|
||||||
|
RateLimiter(max_requests, time_window, Deque{Float64}())
|
||||||
|
end
|
||||||
|
|
||||||
|
function allow(limiter::RateLimiter)::Bool
|
||||||
|
now = time()
|
||||||
|
|
||||||
|
# Remove old requests
|
||||||
|
while !isempty(limiter.requests) && limiter.requests[1] < now - limiter.time_window
|
||||||
|
popfirst!(limiter.requests)
|
||||||
|
end
|
||||||
|
|
||||||
|
if length(limiter.requests) >= limiter.max_requests
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
push!(limiter.requests, now)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Conclusion
|
||||||
|
|
||||||
|
This walkthrough covered:
|
||||||
|
|
||||||
|
- Building a chat application with rich media support
|
||||||
|
- Building a file transfer system with claim-check pattern
|
||||||
|
- Building a streaming data pipeline for sensor data
|
||||||
|
|
||||||
|
For more information, check the [API documentation](../src/README.md) and [test examples](../test/).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
MIT
|
||||||
14
plik_fileserver/docker-compose.yml
Normal file
14
plik_fileserver/docker-compose.yml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
services:
|
||||||
|
plik:
|
||||||
|
image: rootgg/plik:latest
|
||||||
|
container_name: plik-server
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
volumes:
|
||||||
|
# # Mount the config file (created below)
|
||||||
|
# - ./plikd.cfg:/home/plik/server/plikd.cfg
|
||||||
|
# Mount local folder for uploads and database
|
||||||
|
- ./plik-data:/data
|
||||||
|
# Set user to match your host UID to avoid permission issues
|
||||||
|
user: "1000:1000"
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,706 +0,0 @@
|
|||||||
/**
|
|
||||||
* NATSBridge.js - Bi-Directional Data Bridge for JavaScript
|
|
||||||
* Implements smartsend and smartreceive for NATS communication
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
* URL-based transport for larger payloads.
|
|
||||||
*
|
|
||||||
* File Server Handler Architecture:
|
|
||||||
* The system uses handler functions to abstract file server operations, allowing support
|
|
||||||
* for different file server implementations (e.g., Plik, AWS S3, custom HTTP server).
|
|
||||||
*
|
|
||||||
* Handler Function Signatures:
|
|
||||||
*
|
|
||||||
* ```javascript
|
|
||||||
* // Upload handler - uploads data to file server and returns URL
|
|
||||||
* // The handler is passed to smartsend as fileserverUploadHandler parameter
|
|
||||||
* // It receives: (fileserver_url, dataname, data)
|
|
||||||
* // Returns: { status, uploadid, fileid, url }
|
|
||||||
* async function fileserverUploadHandler(fileserver_url, dataname, data) { ... }
|
|
||||||
*
|
|
||||||
* // Download handler - fetches data from file server URL with exponential backoff
|
|
||||||
* // The handler is passed to smartreceive as fileserverDownloadHandler parameter
|
|
||||||
* // It receives: (url, max_retries, base_delay, max_delay, correlation_id)
|
|
||||||
* // Returns: ArrayBuffer (the downloaded data)
|
|
||||||
* async function fileserverDownloadHandler(url, max_retries, base_delay, max_delay, correlation_id) { ... }
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
* Multi-Payload Support (Standard API):
|
|
||||||
* The system uses a standardized list-of-tuples format for all payload operations.
|
|
||||||
* Even when sending a single payload, the user must wrap it in a list.
|
|
||||||
*
|
|
||||||
* API Standard:
|
|
||||||
* ```javascript
|
|
||||||
* // Input format for smartsend (always a list of tuples with type info)
|
|
||||||
* [{ dataname, data, type }, ...]
|
|
||||||
*
|
|
||||||
* // Output format for smartreceive (always returns a list of tuples)
|
|
||||||
* [{ dataname, data, type }, ...]
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
* Supported types: "text", "dictionary", "table", "image", "audio", "video", "binary"
|
|
||||||
*/
|
|
||||||
|
|
||||||
// ---------------------------------------------- 100 --------------------------------------------- #
|
|
||||||
|
|
||||||
// Constants
|
|
||||||
const DEFAULT_SIZE_THRESHOLD = 1_000_000; // 1MB - threshold for switching from direct to link transport
|
|
||||||
const DEFAULT_NATS_URL = "nats://localhost:4222"; // Default NATS server URL
|
|
||||||
const DEFAULT_FILESERVER_URL = "http://localhost:8080"; // Default HTTP file server URL for link transport
|
|
||||||
|
|
||||||
// Helper: Generate UUID v4
|
|
||||||
function uuid4() {
|
|
||||||
// Simple UUID v4 generator
|
|
||||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
||||||
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
|
||||||
return v.toString(16);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper: Log with correlation ID and timestamp
|
|
||||||
function log_trace(correlation_id, message) {
|
|
||||||
const timestamp = new Date().toISOString();
|
|
||||||
console.log(`[${timestamp}] [Correlation: ${correlation_id}] ${message}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper: Get size of data in bytes
|
|
||||||
function getDataSize(data) {
|
|
||||||
if (typeof data === 'string') {
|
|
||||||
return new TextEncoder().encode(data).length;
|
|
||||||
} else if (data instanceof ArrayBuffer || data instanceof Uint8Array) {
|
|
||||||
return data.byteLength;
|
|
||||||
} else if (typeof data === 'object' && data !== null) {
|
|
||||||
// For objects, serialize to JSON and measure
|
|
||||||
return new TextEncoder().encode(JSON.stringify(data)).length;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper: Convert ArrayBuffer to Base64 string
|
|
||||||
function arrayBufferToBase64(buffer) {
|
|
||||||
const bytes = new Uint8Array(buffer);
|
|
||||||
let binary = '';
|
|
||||||
for (let i = 0; i < bytes.length; i++) {
|
|
||||||
binary += String.fromCharCode(bytes[i]);
|
|
||||||
}
|
|
||||||
return btoa(binary);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper: Convert Base64 string to ArrayBuffer
|
|
||||||
function base64ToArrayBuffer(base64) {
|
|
||||||
const binaryString = atob(base64);
|
|
||||||
const len = binaryString.length;
|
|
||||||
const bytes = new Uint8Array(len);
|
|
||||||
for (let i = 0; i < len; i++) {
|
|
||||||
bytes[i] = binaryString.charCodeAt(i);
|
|
||||||
}
|
|
||||||
return bytes.buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper: Serialize data based on type
|
|
||||||
function _serialize_data(data, type) {
|
|
||||||
/**
|
|
||||||
* Serialize data according to specified format
|
|
||||||
*
|
|
||||||
* Supported formats:
|
|
||||||
* - "text": Treats data as text and converts to UTF-8 bytes
|
|
||||||
* - "dictionary": Serializes data as JSON and returns the UTF-8 byte representation
|
|
||||||
* - "table": Serializes data as an Arrow IPC stream (table format) - NOT IMPLEMENTED (requires arrow library)
|
|
||||||
* - "image": Expects binary data (ArrayBuffer) and returns it as bytes
|
|
||||||
* - "audio": Expects binary data (ArrayBuffer) and returns it as bytes
|
|
||||||
* - "video": Expects binary data (ArrayBuffer) and returns it as bytes
|
|
||||||
* - "binary": Generic binary data (ArrayBuffer or Uint8Array) and returns bytes
|
|
||||||
*/
|
|
||||||
if (type === "text") {
|
|
||||||
if (typeof data === 'string') {
|
|
||||||
return new TextEncoder().encode(data).buffer;
|
|
||||||
} else {
|
|
||||||
throw new Error("Text data must be a String");
|
|
||||||
}
|
|
||||||
} else if (type === "dictionary") {
|
|
||||||
// JSON data - serialize directly
|
|
||||||
const jsonStr = JSON.stringify(data);
|
|
||||||
return new TextEncoder().encode(jsonStr).buffer;
|
|
||||||
} else if (type === "table") {
|
|
||||||
// Table data - convert to Arrow IPC stream (NOT IMPLEMENTED in pure JavaScript)
|
|
||||||
// This would require the apache-arrow library
|
|
||||||
throw new Error("Table serialization requires apache-arrow library");
|
|
||||||
} else if (type === "image") {
|
|
||||||
if (data instanceof ArrayBuffer || data instanceof Uint8Array) {
|
|
||||||
return data instanceof ArrayBuffer ? data : data.buffer;
|
|
||||||
} else {
|
|
||||||
throw new Error("Image data must be ArrayBuffer or Uint8Array");
|
|
||||||
}
|
|
||||||
} else if (type === "audio") {
|
|
||||||
if (data instanceof ArrayBuffer || data instanceof Uint8Array) {
|
|
||||||
return data instanceof ArrayBuffer ? data : data.buffer;
|
|
||||||
} else {
|
|
||||||
throw new Error("Audio data must be ArrayBuffer or Uint8Array");
|
|
||||||
}
|
|
||||||
} else if (type === "video") {
|
|
||||||
if (data instanceof ArrayBuffer || data instanceof Uint8Array) {
|
|
||||||
return data instanceof ArrayBuffer ? data : data.buffer;
|
|
||||||
} else {
|
|
||||||
throw new Error("Video data must be ArrayBuffer or Uint8Array");
|
|
||||||
}
|
|
||||||
} else if (type === "binary") {
|
|
||||||
if (data instanceof ArrayBuffer || data instanceof Uint8Array) {
|
|
||||||
return data instanceof ArrayBuffer ? data : data.buffer;
|
|
||||||
} else {
|
|
||||||
throw new Error("Binary data must be ArrayBuffer or Uint8Array");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new Error(`Unknown type: ${type}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper: Deserialize bytes based on type
|
|
||||||
function _deserialize_data(data, type, correlation_id) {
|
|
||||||
/**
|
|
||||||
* Deserialize bytes to data based on type
|
|
||||||
*
|
|
||||||
* Supported formats:
|
|
||||||
* - "text": Converts bytes to string
|
|
||||||
* - "dictionary": Parses JSON string
|
|
||||||
* - "table": Parses Arrow IPC stream - NOT IMPLEMENTED (requires apache-arrow library)
|
|
||||||
* - "image": Returns binary data
|
|
||||||
* - "audio": Returns binary data
|
|
||||||
* - "video": Returns binary data
|
|
||||||
* - "binary": Returns binary data
|
|
||||||
*/
|
|
||||||
if (type === "text") {
|
|
||||||
const decoder = new TextDecoder();
|
|
||||||
return decoder.decode(new Uint8Array(data));
|
|
||||||
} else if (type === "dictionary") {
|
|
||||||
const decoder = new TextDecoder();
|
|
||||||
const jsonStr = decoder.decode(new Uint8Array(data));
|
|
||||||
return JSON.parse(jsonStr);
|
|
||||||
} else if (type === "table") {
|
|
||||||
// Table data - deserialize Arrow IPC stream (NOT IMPLEMENTED in pure JavaScript)
|
|
||||||
throw new Error("Table deserialization requires apache-arrow library");
|
|
||||||
} else if (type === "image") {
|
|
||||||
return data;
|
|
||||||
} else if (type === "audio") {
|
|
||||||
return data;
|
|
||||||
} else if (type === "video") {
|
|
||||||
return data;
|
|
||||||
} else if (type === "binary") {
|
|
||||||
return data;
|
|
||||||
} else {
|
|
||||||
throw new Error(`Unknown type: ${type}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper: Upload data to file server
|
|
||||||
async function _upload_to_fileserver(fileserver_url, dataname, data, correlation_id) {
|
|
||||||
/**
|
|
||||||
* Upload data to HTTP file server (plik-like API)
|
|
||||||
*
|
|
||||||
* This function implements the plik one-shot upload mode:
|
|
||||||
* 1. Creates a one-shot upload session by sending POST request with {"OneShot": true}
|
|
||||||
* 2. Uploads the file data as multipart form data
|
|
||||||
* 3. Returns identifiers and download URL for the uploaded file
|
|
||||||
*/
|
|
||||||
log_trace(correlation_id, `Uploading ${dataname} to fileserver: ${fileserver_url}`);
|
|
||||||
|
|
||||||
// Step 1: Get upload ID and token
|
|
||||||
const url_getUploadID = `${fileserver_url}/upload`;
|
|
||||||
const headers = {
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
};
|
|
||||||
const body = JSON.stringify({ OneShot: true });
|
|
||||||
|
|
||||||
let response = await fetch(url_getUploadID, {
|
|
||||||
method: "POST",
|
|
||||||
headers: headers,
|
|
||||||
body: body
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Failed to get upload ID: ${response.status} ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const responseJson = await response.json();
|
|
||||||
const uploadid = responseJson.id;
|
|
||||||
const uploadtoken = responseJson.uploadToken;
|
|
||||||
|
|
||||||
// Step 2: Upload file data
|
|
||||||
const url_upload = `${fileserver_url}/file/${uploadid}`;
|
|
||||||
|
|
||||||
// Create multipart form data
|
|
||||||
const formData = new FormData();
|
|
||||||
// Create a Blob from the ArrayBuffer
|
|
||||||
const blob = new Blob([data], { type: "application/octet-stream" });
|
|
||||||
formData.append("file", blob, dataname);
|
|
||||||
|
|
||||||
response = await fetch(url_upload, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"X-UploadToken": uploadtoken
|
|
||||||
},
|
|
||||||
body: formData
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Failed to upload file: ${response.status} ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileResponseJson = await response.json();
|
|
||||||
const fileid = fileResponseJson.id;
|
|
||||||
|
|
||||||
// Build the download URL
|
|
||||||
const url = `${fileserver_url}/file/${uploadid}/${fileid}/${encodeURIComponent(dataname)}`;
|
|
||||||
|
|
||||||
log_trace(correlation_id, `Uploaded to URL: ${url}`);
|
|
||||||
|
|
||||||
return {
|
|
||||||
status: response.status,
|
|
||||||
uploadid: uploadid,
|
|
||||||
fileid: fileid,
|
|
||||||
url: url
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper: Fetch data from URL with exponential backoff
|
|
||||||
async function _fetch_with_backoff(url, max_retries, base_delay, max_delay, correlation_id) {
|
|
||||||
/**
|
|
||||||
* Fetch data from URL with retry logic using exponential backoff
|
|
||||||
*/
|
|
||||||
let delay = base_delay;
|
|
||||||
|
|
||||||
for (let attempt = 1; attempt <= max_retries; attempt++) {
|
|
||||||
try {
|
|
||||||
const response = await fetch(url);
|
|
||||||
|
|
||||||
if (response.status === 200) {
|
|
||||||
log_trace(correlation_id, `Successfully fetched data from ${url} on attempt ${attempt}`);
|
|
||||||
const arrayBuffer = await response.arrayBuffer();
|
|
||||||
return arrayBuffer;
|
|
||||||
} else {
|
|
||||||
throw new Error(`Failed to fetch: ${response.status} ${response.statusText}`);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
log_trace(correlation_id, `Attempt ${attempt} failed: ${e.message}`);
|
|
||||||
|
|
||||||
if (attempt < max_retries) {
|
|
||||||
// Sleep with exponential backoff
|
|
||||||
await new Promise(resolve => setTimeout(resolve, delay));
|
|
||||||
delay = Math.min(delay * 2, max_delay);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error(`Failed to fetch data after ${max_retries} attempts`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper: Get payload bytes from data
|
|
||||||
function _get_payload_bytes(data) {
|
|
||||||
if (data instanceof ArrayBuffer || data instanceof Uint8Array) {
|
|
||||||
return data instanceof ArrayBuffer ? new Uint8Array(data) : data;
|
|
||||||
} else if (typeof data === 'string') {
|
|
||||||
return new TextEncoder().encode(data);
|
|
||||||
} else {
|
|
||||||
// For objects, serialize to JSON
|
|
||||||
return new TextEncoder().encode(JSON.stringify(data));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// MessagePayload class
|
|
||||||
class MessagePayload {
|
|
||||||
/**
|
|
||||||
* Represents a single payload in the message envelope
|
|
||||||
*
|
|
||||||
* @param {Object} options - Payload options
|
|
||||||
* @param {string} options.id - ID of this payload (e.g., "uuid4")
|
|
||||||
* @param {string} options.dataname - Name of this payload (e.g., "login_image")
|
|
||||||
* @param {string} options.type - Payload type: "text", "dictionary", "table", "image", "audio", "video", "binary"
|
|
||||||
* @param {string} options.transport - "direct" or "link"
|
|
||||||
* @param {string} options.encoding - "none", "json", "base64", "arrow-ipc"
|
|
||||||
* @param {number} options.size - Data size in bytes
|
|
||||||
* @param {string|ArrayBuffer} options.data - Payload data (direct) or URL (link)
|
|
||||||
* @param {Object} options.metadata - Metadata for this payload
|
|
||||||
*/
|
|
||||||
constructor(options) {
|
|
||||||
this.id = options.id || uuid4();
|
|
||||||
this.dataname = options.dataname;
|
|
||||||
this.type = options.type;
|
|
||||||
this.transport = options.transport;
|
|
||||||
this.encoding = options.encoding;
|
|
||||||
this.size = options.size;
|
|
||||||
this.data = options.data;
|
|
||||||
this.metadata = options.metadata || {};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert to JSON object
|
|
||||||
toJSON() {
|
|
||||||
const obj = {
|
|
||||||
id: this.id,
|
|
||||||
dataname: this.dataname,
|
|
||||||
type: this.type,
|
|
||||||
transport: this.transport,
|
|
||||||
encoding: this.encoding,
|
|
||||||
size: this.size
|
|
||||||
};
|
|
||||||
|
|
||||||
// Include data based on transport type
|
|
||||||
if (this.transport === "direct" && this.data !== null) {
|
|
||||||
if (this.encoding === "base64" || this.encoding === "json") {
|
|
||||||
obj.data = this.data;
|
|
||||||
} else {
|
|
||||||
// For other encodings, use base64
|
|
||||||
const payloadBytes = _get_payload_bytes(this.data);
|
|
||||||
obj.data = arrayBufferToBase64(payloadBytes);
|
|
||||||
}
|
|
||||||
} else if (this.transport === "link" && this.data !== null) {
|
|
||||||
// For link transport, data is a URL string
|
|
||||||
obj.data = this.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Object.keys(this.metadata).length > 0) {
|
|
||||||
obj.metadata = this.metadata;
|
|
||||||
}
|
|
||||||
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// MessageEnvelope class
|
|
||||||
class MessageEnvelope {
|
|
||||||
/**
|
|
||||||
* Represents the message envelope containing metadata and payloads
|
|
||||||
*
|
|
||||||
* @param {Object} options - Envelope options
|
|
||||||
* @param {string} options.sendTo - Topic/subject the sender sends to
|
|
||||||
* @param {Array<MessagePayload>} options.payloads - Array of payloads
|
|
||||||
* @param {string} options.correlationId - Unique identifier to track messages
|
|
||||||
* @param {string} options.msgId - This message id
|
|
||||||
* @param {string} options.timestamp - Message published timestamp
|
|
||||||
* @param {string} options.msgPurpose - Purpose of this message
|
|
||||||
* @param {string} options.senderName - Name of the sender
|
|
||||||
* @param {string} options.senderId - UUID of the sender
|
|
||||||
* @param {string} options.receiverName - Name of the receiver
|
|
||||||
* @param {string} options.receiverId - UUID of the receiver
|
|
||||||
* @param {string} options.replyTo - Topic to reply to
|
|
||||||
* @param {string} options.replyToMsgId - Message id this message is replying to
|
|
||||||
* @param {string} options.brokerURL - NATS server address
|
|
||||||
* @param {Object} options.metadata - Metadata for the envelope
|
|
||||||
*/
|
|
||||||
constructor(options) {
|
|
||||||
this.correlationId = options.correlationId || uuid4();
|
|
||||||
this.msgId = options.msgId || uuid4();
|
|
||||||
this.timestamp = options.timestamp || new Date().toISOString();
|
|
||||||
this.sendTo = options.sendTo;
|
|
||||||
this.msgPurpose = options.msgPurpose || "";
|
|
||||||
this.senderName = options.senderName || "";
|
|
||||||
this.senderId = options.senderId || uuid4();
|
|
||||||
this.receiverName = options.receiverName || "";
|
|
||||||
this.receiverId = options.receiverId || "";
|
|
||||||
this.replyTo = options.replyTo || "";
|
|
||||||
this.replyToMsgId = options.replyToMsgId || "";
|
|
||||||
this.brokerURL = options.brokerURL || DEFAULT_NATS_URL;
|
|
||||||
this.metadata = options.metadata || {};
|
|
||||||
this.payloads = options.payloads || [];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert to JSON string
|
|
||||||
toJSON() {
|
|
||||||
const obj = {
|
|
||||||
correlationId: this.correlationId,
|
|
||||||
msgId: this.msgId,
|
|
||||||
timestamp: this.timestamp,
|
|
||||||
sendTo: this.sendTo,
|
|
||||||
msgPurpose: this.msgPurpose,
|
|
||||||
senderName: this.senderName,
|
|
||||||
senderId: this.senderId,
|
|
||||||
receiverName: this.receiverName,
|
|
||||||
receiverId: this.receiverId,
|
|
||||||
replyTo: this.replyTo,
|
|
||||||
replyToMsgId: this.replyToMsgId,
|
|
||||||
brokerURL: this.brokerURL
|
|
||||||
};
|
|
||||||
|
|
||||||
if (Object.keys(this.metadata).length > 0) {
|
|
||||||
obj.metadata = this.metadata;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.payloads.length > 0) {
|
|
||||||
obj.payloads = this.payloads.map(p => p.toJSON());
|
|
||||||
}
|
|
||||||
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert to JSON string
|
|
||||||
toString() {
|
|
||||||
return JSON.stringify(this.toJSON());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SmartSend function
|
|
||||||
async function smartsend(subject, data, options = {}) {
|
|
||||||
/**
|
|
||||||
* 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 and publishes only the download URL over NATS.
|
|
||||||
*
|
|
||||||
* @param {string} subject - NATS subject to publish the message to
|
|
||||||
* @param {Array} data - List of {dataname, data, type} objects to send
|
|
||||||
* @param {Object} options - Additional options
|
|
||||||
* @param {string} options.natsUrl - URL of the NATS server (default: "nats://localhost:4222")
|
|
||||||
* @param {string} options.fileserverUrl - Base URL of the file server (default: "http://localhost:8080")
|
|
||||||
* @param {Function} options.fileserverUploadHandler - Function to handle fileserver uploads
|
|
||||||
* @param {number} options.sizeThreshold - Threshold in bytes separating direct vs link transport (default: 1MB)
|
|
||||||
* @param {string} options.correlationId - Optional correlation ID for tracing
|
|
||||||
* @param {string} options.msgPurpose - Purpose of the message (default: "chat")
|
|
||||||
* @param {string} options.senderName - Name of the sender (default: "NATSBridge")
|
|
||||||
* @param {string} options.receiverName - Name of the receiver (default: "")
|
|
||||||
* @param {string} options.receiverId - UUID of the receiver (default: "")
|
|
||||||
* @param {string} options.replyTo - Topic to reply to (default: "")
|
|
||||||
* @param {string} options.replyToMsgId - Message ID this message is replying to (default: "")
|
|
||||||
*
|
|
||||||
* @returns {Promise<MessageEnvelope>} - The envelope for tracking
|
|
||||||
*/
|
|
||||||
const {
|
|
||||||
natsUrl = DEFAULT_NATS_URL,
|
|
||||||
fileserverUrl = DEFAULT_FILESERVER_URL,
|
|
||||||
fileserverUploadHandler = _upload_to_fileserver,
|
|
||||||
sizeThreshold = DEFAULT_SIZE_THRESHOLD,
|
|
||||||
correlationId = uuid4(),
|
|
||||||
msgPurpose = "chat",
|
|
||||||
senderName = "NATSBridge",
|
|
||||||
receiverName = "",
|
|
||||||
receiverId = "",
|
|
||||||
replyTo = "",
|
|
||||||
replyToMsgId = ""
|
|
||||||
} = options;
|
|
||||||
|
|
||||||
log_trace(correlationId, `Starting smartsend for subject: ${subject}`);
|
|
||||||
|
|
||||||
// Generate message metadata
|
|
||||||
const msgId = uuid4();
|
|
||||||
|
|
||||||
// Process each payload in the list
|
|
||||||
const payloads = [];
|
|
||||||
|
|
||||||
for (const payload of data) {
|
|
||||||
const dataname = payload.dataname;
|
|
||||||
const payloadData = payload.data;
|
|
||||||
const payloadType = payload.type;
|
|
||||||
|
|
||||||
// Serialize data based on type
|
|
||||||
const payloadBytes = _serialize_data(payloadData, payloadType);
|
|
||||||
const payloadSize = payloadBytes.byteLength;
|
|
||||||
|
|
||||||
log_trace(correlationId, `Serialized payload '${dataname}' (type: ${payloadType}) size: ${payloadSize} bytes`);
|
|
||||||
|
|
||||||
// Decision: Direct vs Link
|
|
||||||
if (payloadSize < sizeThreshold) {
|
|
||||||
// Direct path - Base64 encode and send via NATS
|
|
||||||
const payloadB64 = arrayBufferToBase64(payloadBytes);
|
|
||||||
log_trace(correlationId, `Using direct transport for ${payloadSize} bytes`);
|
|
||||||
|
|
||||||
// Create MessagePayload for direct transport
|
|
||||||
const payloadObj = new MessagePayload({
|
|
||||||
dataname: dataname,
|
|
||||||
type: payloadType,
|
|
||||||
transport: "direct",
|
|
||||||
encoding: "base64",
|
|
||||||
size: payloadSize,
|
|
||||||
data: payloadB64,
|
|
||||||
metadata: { payload_bytes: payloadSize }
|
|
||||||
});
|
|
||||||
payloads.push(payloadObj);
|
|
||||||
} else {
|
|
||||||
// Link path - Upload to HTTP server, send URL via NATS
|
|
||||||
log_trace(correlationId, `Using link transport, uploading to fileserver`);
|
|
||||||
|
|
||||||
// Upload to HTTP server
|
|
||||||
const response = await fileserverUploadHandler(fileserverUrl, dataname, payloadBytes, correlationId);
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
throw new Error(`Failed to upload data to fileserver: ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = response.url;
|
|
||||||
log_trace(correlationId, `Uploaded to URL: ${url}`);
|
|
||||||
|
|
||||||
// Create MessagePayload for link transport
|
|
||||||
const payloadObj = new MessagePayload({
|
|
||||||
dataname: dataname,
|
|
||||||
type: payloadType,
|
|
||||||
transport: "link",
|
|
||||||
encoding: "none",
|
|
||||||
size: payloadSize,
|
|
||||||
data: url,
|
|
||||||
metadata: {}
|
|
||||||
});
|
|
||||||
payloads.push(payloadObj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create MessageEnvelope with all payloads
|
|
||||||
const env = new MessageEnvelope({
|
|
||||||
correlationId: correlationId,
|
|
||||||
msgId: msgId,
|
|
||||||
sendTo: subject,
|
|
||||||
msgPurpose: msgPurpose,
|
|
||||||
senderName: senderName,
|
|
||||||
receiverName: receiverName,
|
|
||||||
receiverId: receiverId,
|
|
||||||
replyTo: replyTo,
|
|
||||||
replyToMsgId: replyToMsgId,
|
|
||||||
brokerURL: natsUrl,
|
|
||||||
payloads: payloads
|
|
||||||
});
|
|
||||||
|
|
||||||
// Publish message to NATS
|
|
||||||
await publish_message(natsUrl, subject, env.toString(), correlationId);
|
|
||||||
|
|
||||||
return env;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper: Publish message to NATS
|
|
||||||
async function publish_message(natsUrl, subject, message, correlation_id) {
|
|
||||||
/**
|
|
||||||
* Publish a message to a NATS subject with proper connection management
|
|
||||||
*
|
|
||||||
* @param {string} natsUrl - NATS server URL
|
|
||||||
* @param {string} subject - NATS subject to publish to
|
|
||||||
* @param {string} message - JSON message to publish
|
|
||||||
* @param {string} correlation_id - Correlation ID for logging
|
|
||||||
*/
|
|
||||||
log_trace(correlation_id, `Publishing message to ${subject}`);
|
|
||||||
|
|
||||||
// For Node.js, we would use nats.js library
|
|
||||||
// This is a placeholder that throws an error
|
|
||||||
// In production, you would import and use the actual nats library
|
|
||||||
|
|
||||||
// Example with nats.js:
|
|
||||||
// import { connect } from 'nats';
|
|
||||||
// const nc = await connect({ servers: [natsUrl] });
|
|
||||||
// await nc.publish(subject, message);
|
|
||||||
// nc.close();
|
|
||||||
|
|
||||||
// For now, just log the message
|
|
||||||
console.log(`[NATS PUBLISH] Subject: ${subject}, Message: ${message.substring(0, 100)}...`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// SmartReceive function
|
|
||||||
async function smartreceive(msg, options = {}) {
|
|
||||||
/**
|
|
||||||
* Receive and process messages from NATS
|
|
||||||
*
|
|
||||||
* This function processes incoming NATS messages, handling both direct transport
|
|
||||||
* (base64 decoded payloads) and link transport (URL-based payloads).
|
|
||||||
*
|
|
||||||
* @param {Object} msg - NATS message object with payload property
|
|
||||||
* @param {Object} options - Additional options
|
|
||||||
* @param {Function} options.fileserverDownloadHandler - Function to handle downloading data from file server URLs
|
|
||||||
* @param {number} options.maxRetries - Maximum retry attempts for fetching URL (default: 5)
|
|
||||||
* @param {number} options.baseDelay - Initial delay for exponential backoff in ms (default: 100)
|
|
||||||
* @param {number} options.maxDelay - Maximum delay for exponential backoff in ms (default: 5000)
|
|
||||||
*
|
|
||||||
* @returns {Promise<Array>} - List of {dataname, data, type} objects
|
|
||||||
*/
|
|
||||||
const {
|
|
||||||
fileserverDownloadHandler = _fetch_with_backoff,
|
|
||||||
maxRetries = 5,
|
|
||||||
baseDelay = 100,
|
|
||||||
maxDelay = 5000
|
|
||||||
} = options;
|
|
||||||
|
|
||||||
// Parse the JSON envelope
|
|
||||||
const jsonStr = typeof msg.payload === 'string' ? msg.payload : new TextDecoder().decode(msg.payload);
|
|
||||||
const json_data = JSON.parse(jsonStr);
|
|
||||||
|
|
||||||
log_trace(json_data.correlationId, `Processing received message`);
|
|
||||||
|
|
||||||
// Process all payloads in the envelope
|
|
||||||
const payloads_list = [];
|
|
||||||
|
|
||||||
// Get number of payloads
|
|
||||||
const num_payloads = json_data.payloads ? json_data.payloads.length : 0;
|
|
||||||
|
|
||||||
for (let i = 0; i < num_payloads; i++) {
|
|
||||||
const payload = json_data.payloads[i];
|
|
||||||
const transport = payload.transport;
|
|
||||||
const dataname = payload.dataname;
|
|
||||||
|
|
||||||
if (transport === "direct") {
|
|
||||||
// Direct transport - payload is in the message
|
|
||||||
log_trace(json_data.correlationId, `Direct transport - decoding payload '${dataname}'`);
|
|
||||||
|
|
||||||
// Extract base64 payload from the payload
|
|
||||||
const payload_b64 = payload.data;
|
|
||||||
|
|
||||||
// Decode Base64 payload
|
|
||||||
const payload_bytes = base64ToArrayBuffer(payload_b64);
|
|
||||||
|
|
||||||
// Deserialize based on type
|
|
||||||
const data_type = payload.type;
|
|
||||||
const data = _deserialize_data(payload_bytes, data_type, json_data.correlationId);
|
|
||||||
|
|
||||||
payloads_list.push({ dataname, data, type: data_type });
|
|
||||||
} else if (transport === "link") {
|
|
||||||
// Link transport - payload is at URL
|
|
||||||
const url = payload.data;
|
|
||||||
log_trace(json_data.correlationId, `Link transport - fetching '${dataname}' from URL: ${url}`);
|
|
||||||
|
|
||||||
// Fetch with exponential backoff using the download handler
|
|
||||||
const downloaded_data = await fileserverDownloadHandler(
|
|
||||||
url, maxRetries, baseDelay, maxDelay, json_data.correlationId
|
|
||||||
);
|
|
||||||
|
|
||||||
// Deserialize based on type
|
|
||||||
const data_type = payload.type;
|
|
||||||
const data = _deserialize_data(downloaded_data, data_type, json_data.correlationId);
|
|
||||||
|
|
||||||
payloads_list.push({ dataname, data, type: data_type });
|
|
||||||
} else {
|
|
||||||
throw new Error(`Unknown transport type for payload '${dataname}': ${transport}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return payloads_list;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Export for Node.js
|
|
||||||
if (typeof module !== 'undefined' && module.exports) {
|
|
||||||
module.exports = {
|
|
||||||
MessageEnvelope,
|
|
||||||
MessagePayload,
|
|
||||||
smartsend,
|
|
||||||
smartreceive,
|
|
||||||
_serialize_data,
|
|
||||||
_deserialize_data,
|
|
||||||
_fetch_with_backoff,
|
|
||||||
_upload_to_fileserver,
|
|
||||||
DEFAULT_SIZE_THRESHOLD,
|
|
||||||
DEFAULT_NATS_URL,
|
|
||||||
DEFAULT_FILESERVER_URL,
|
|
||||||
uuid4,
|
|
||||||
log_trace
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Export for browser
|
|
||||||
if (typeof window !== 'undefined') {
|
|
||||||
window.NATSBridge = {
|
|
||||||
MessageEnvelope,
|
|
||||||
MessagePayload,
|
|
||||||
smartsend,
|
|
||||||
smartreceive,
|
|
||||||
_serialize_data,
|
|
||||||
_deserialize_data,
|
|
||||||
_fetch_with_backoff,
|
|
||||||
_upload_to_fileserver,
|
|
||||||
DEFAULT_SIZE_THRESHOLD,
|
|
||||||
DEFAULT_NATS_URL,
|
|
||||||
DEFAULT_FILESERVER_URL,
|
|
||||||
uuid4,
|
|
||||||
log_trace
|
|
||||||
};
|
|
||||||
}
|
|
||||||
212
src/README.md
212
src/README.md
@@ -1,212 +0,0 @@
|
|||||||
# NATSBridge for Micropython
|
|
||||||
|
|
||||||
A high-performance, bi-directional data bridge for Micropython devices using NATS (Core & JetStream), implementing the Claim-Check pattern for large payloads.
|
|
||||||
|
|
||||||
## Overview
|
|
||||||
|
|
||||||
This module provides functionality for sending and receiving data over 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
|
|
||||||
|
|
||||||
## Features
|
|
||||||
|
|
||||||
- ✅ Bi-directional NATS communication
|
|
||||||
- ✅ Multi-payload support (mixed content in single message)
|
|
||||||
- ✅ Automatic transport selection based on payload size
|
|
||||||
- ✅ File server integration for large payloads
|
|
||||||
- ✅ Exponential backoff for URL fetching
|
|
||||||
- ✅ Correlation ID tracking
|
|
||||||
- ✅ Reply-to support for request-response pattern
|
|
||||||
|
|
||||||
## Supported Payload Types
|
|
||||||
|
|
||||||
| Type | Description |
|
|
||||||
|------|-------------|
|
|
||||||
| `text` | Plain text strings |
|
|
||||||
| `dictionary` | JSON-serializable dictionaries |
|
|
||||||
| `table` | Tabular data (Arrow IPC format) |
|
|
||||||
| `image` | Image data (PNG, JPG bytes) |
|
|
||||||
| `audio` | Audio data (WAV, MP3 bytes) |
|
|
||||||
| `video` | Video data (MP4, AVI bytes) |
|
|
||||||
| `binary` | Generic binary data |
|
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
1. Copy `nats_bridge.py` to your Micropython device
|
|
||||||
2. Ensure you have the following dependencies:
|
|
||||||
- `urequests` for HTTP requests
|
|
||||||
- `ubinascii` for base64 encoding
|
|
||||||
- `ujson` for JSON handling
|
|
||||||
- `usocket` for networking
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
### Basic Text Message
|
|
||||||
|
|
||||||
```python
|
|
||||||
from nats_bridge import smartsend, smartreceive
|
|
||||||
|
|
||||||
# Sender
|
|
||||||
data = [("message", "Hello World", "text")]
|
|
||||||
env = smartsend("/chat/room1", data, nats_url="nats://localhost:4222")
|
|
||||||
|
|
||||||
# Receiver
|
|
||||||
payloads = smartreceive(msg)
|
|
||||||
for dataname, data, type in payloads:
|
|
||||||
print("Received {}: {}".format(dataname, data))
|
|
||||||
```
|
|
||||||
|
|
||||||
### Sending JSON Configuration
|
|
||||||
|
|
||||||
```python
|
|
||||||
from nats_bridge import smartsend
|
|
||||||
|
|
||||||
config = {
|
|
||||||
"wifi_ssid": "MyNetwork",
|
|
||||||
"wifi_password": "password123",
|
|
||||||
"update_interval": 60
|
|
||||||
}
|
|
||||||
|
|
||||||
data = [("config", config, "dictionary")]
|
|
||||||
env = smartsend("/device/config", data, nats_url="nats://localhost:4222")
|
|
||||||
```
|
|
||||||
|
|
||||||
### Mixed Content (Chat with Text + Image)
|
|
||||||
|
|
||||||
```python
|
|
||||||
from nats_bridge import smartsend
|
|
||||||
|
|
||||||
image_data = b"\x89PNG..." # PNG bytes
|
|
||||||
|
|
||||||
data = [
|
|
||||||
("message_text", "Hello with image!", "text"),
|
|
||||||
("user_avatar", image_data, "binary")
|
|
||||||
]
|
|
||||||
|
|
||||||
env = smartsend("/chat/mixed", data, nats_url="nats://localhost:4222")
|
|
||||||
```
|
|
||||||
|
|
||||||
### Request-Response Pattern
|
|
||||||
|
|
||||||
```python
|
|
||||||
from nats_bridge import smartsend
|
|
||||||
|
|
||||||
# Send command with reply-to
|
|
||||||
data = [("command", {"action": "read_sensor"}, "dictionary")]
|
|
||||||
env = smartsend(
|
|
||||||
"/device/command",
|
|
||||||
data,
|
|
||||||
nats_url="nats://localhost:4222",
|
|
||||||
reply_to="/device/response",
|
|
||||||
reply_to_msg_id="cmd-001"
|
|
||||||
)
|
|
||||||
```
|
|
||||||
|
|
||||||
### Large Payloads (File Server)
|
|
||||||
|
|
||||||
```python
|
|
||||||
from nats_bridge import smartsend
|
|
||||||
|
|
||||||
# Large data (> 1MB)
|
|
||||||
large_data = b"A" * 2000000 # 2MB
|
|
||||||
|
|
||||||
env = smartsend(
|
|
||||||
"/data/large",
|
|
||||||
[("large_file", large_data, "binary")],
|
|
||||||
nats_url="nats://localhost:4222",
|
|
||||||
fileserver_url="http://localhost:8080",
|
|
||||||
size_threshold=1000000 # 1MB threshold
|
|
||||||
)
|
|
||||||
```
|
|
||||||
|
|
||||||
## API Reference
|
|
||||||
|
|
||||||
### `smartsend(subject, data, ...)`
|
|
||||||
|
|
||||||
Send data via NATS with automatic transport selection.
|
|
||||||
|
|
||||||
**Arguments:**
|
|
||||||
- `subject` (str): NATS subject to publish to
|
|
||||||
- `data` (list): List of `(dataname, data, type)` tuples
|
|
||||||
- `nats_url` (str): NATS server URL (default: `nats://localhost:4222`)
|
|
||||||
- `fileserver_url` (str): HTTP file server URL (default: `http://localhost:8080`)
|
|
||||||
- `size_threshold` (int): Threshold in bytes (default: 1,000,000)
|
|
||||||
- `correlation_id` (str): Optional correlation ID for tracing
|
|
||||||
- `msg_purpose` (str): Message purpose (default: `"chat"`)
|
|
||||||
- `sender_name` (str): Sender name (default: `"NATSBridge"`)
|
|
||||||
- `receiver_name` (str): Receiver name (default: `""`)
|
|
||||||
- `receiver_id` (str): Receiver ID (default: `""`)
|
|
||||||
- `reply_to` (str): Reply topic (default: `""`)
|
|
||||||
- `reply_to_msg_id` (str): Reply message ID (default: `""`)
|
|
||||||
|
|
||||||
**Returns:** `MessageEnvelope` object
|
|
||||||
|
|
||||||
### `smartreceive(msg, ...)`
|
|
||||||
|
|
||||||
Receive and process NATS messages.
|
|
||||||
|
|
||||||
**Arguments:**
|
|
||||||
- `msg`: NATS message (dict or JSON string)
|
|
||||||
- `fileserver_download_handler` (function): Function to fetch data from URLs
|
|
||||||
- `max_retries` (int): Maximum retry attempts (default: 5)
|
|
||||||
- `base_delay` (int): Initial delay in ms (default: 100)
|
|
||||||
- `max_delay` (int): Maximum delay in ms (default: 5000)
|
|
||||||
|
|
||||||
**Returns:** List of `(dataname, data, type)` tuples
|
|
||||||
|
|
||||||
### `MessageEnvelope`
|
|
||||||
|
|
||||||
Represents a complete NATS message envelope.
|
|
||||||
|
|
||||||
**Attributes:**
|
|
||||||
- `correlation_id`: Unique identifier for tracing
|
|
||||||
- `msg_id`: Unique message identifier
|
|
||||||
- `timestamp`: Message publication timestamp
|
|
||||||
- `send_to`: NATS subject
|
|
||||||
- `msg_purpose`: Message purpose
|
|
||||||
- `sender_name`: Sender name
|
|
||||||
- `sender_id`: Sender UUID
|
|
||||||
- `receiver_name`: Receiver name
|
|
||||||
- `receiver_id`: Receiver UUID
|
|
||||||
- `reply_to`: Reply topic
|
|
||||||
- `reply_to_msg_id`: Reply message ID
|
|
||||||
- `broker_url`: NATS broker URL
|
|
||||||
- `metadata`: Message-level metadata
|
|
||||||
- `payloads`: List of MessagePayload objects
|
|
||||||
|
|
||||||
### `MessagePayload`
|
|
||||||
|
|
||||||
Represents a single payload within a message envelope.
|
|
||||||
|
|
||||||
**Attributes:**
|
|
||||||
- `id`: Unique payload identifier
|
|
||||||
- `dataname`: Name of the payload
|
|
||||||
- `type`: Payload type ("text", "dictionary", etc.)
|
|
||||||
- `transport`: Transport method ("direct" or "link")
|
|
||||||
- `encoding`: Encoding method ("none", "base64", etc.)
|
|
||||||
- `size`: Payload size in bytes
|
|
||||||
- `data`: Payload data (bytes for direct, URL for link)
|
|
||||||
- `metadata`: Payload-level metadata
|
|
||||||
|
|
||||||
## Examples
|
|
||||||
|
|
||||||
See `examples/micropython_example.py` for more detailed examples.
|
|
||||||
|
|
||||||
## Testing
|
|
||||||
|
|
||||||
Run the test suite:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
python test/test_micropython_basic.py
|
|
||||||
```
|
|
||||||
|
|
||||||
## Requirements
|
|
||||||
|
|
||||||
- Micropython with networking support
|
|
||||||
- NATS server (nats.io)
|
|
||||||
- HTTP file server (optional, for large payloads)
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
MIT
|
|
||||||
@@ -1,664 +0,0 @@
|
|||||||
"""
|
|
||||||
Micropython NATS Bridge - Bi-Directional Data Bridge for Micropython
|
|
||||||
|
|
||||||
This module provides functionality for sending and receiving data over NATS
|
|
||||||
using the Claim-Check pattern for large payloads.
|
|
||||||
|
|
||||||
Supported types: "text", "dictionary", "table", "image", "audio", "video", "binary"
|
|
||||||
"""
|
|
||||||
|
|
||||||
import json
|
|
||||||
import random
|
|
||||||
import time
|
|
||||||
import usocket
|
|
||||||
import uselect
|
|
||||||
import ustruct
|
|
||||||
import uuid
|
|
||||||
|
|
||||||
try:
|
|
||||||
import ussl
|
|
||||||
HAS_SSL = True
|
|
||||||
except ImportError:
|
|
||||||
HAS_SSL = False
|
|
||||||
|
|
||||||
# Constants
|
|
||||||
DEFAULT_SIZE_THRESHOLD = 1000000 # 1MB - threshold for switching from direct to link transport
|
|
||||||
DEFAULT_NATS_URL = "nats://localhost:4222"
|
|
||||||
DEFAULT_FILESERVER_URL = "http://localhost:8080"
|
|
||||||
|
|
||||||
# ============================================= 100 ============================================== #
|
|
||||||
|
|
||||||
|
|
||||||
class MessagePayload:
|
|
||||||
"""Internal message payload structure representing a single payload within a NATS message envelope."""
|
|
||||||
|
|
||||||
def __init__(self, data, msg_type, id="", dataname="", transport="direct",
|
|
||||||
encoding="none", size=0, metadata=None):
|
|
||||||
"""
|
|
||||||
Initialize a MessagePayload.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
data: Payload data (bytes for direct, URL string for link)
|
|
||||||
msg_type: Payload type ("text", "dictionary", "table", "image", "audio", "video", "binary")
|
|
||||||
id: Unique identifier for this payload (auto-generated if empty)
|
|
||||||
dataname: Name of the payload (auto-generated UUID if empty)
|
|
||||||
transport: Transport method ("direct" or "link")
|
|
||||||
encoding: Encoding method ("none", "json", "base64", "arrow-ipc")
|
|
||||||
size: Size of the payload in bytes
|
|
||||||
metadata: Optional metadata dictionary
|
|
||||||
"""
|
|
||||||
self.id = id if id else self._generate_uuid()
|
|
||||||
self.dataname = dataname if dataname else self._generate_uuid()
|
|
||||||
self.type = msg_type
|
|
||||||
self.transport = transport
|
|
||||||
self.encoding = encoding
|
|
||||||
self.size = size
|
|
||||||
self.data = data
|
|
||||||
self.metadata = metadata if metadata else {}
|
|
||||||
|
|
||||||
def _generate_uuid(self):
|
|
||||||
"""Generate a UUID string."""
|
|
||||||
return str(uuid.uuid4())
|
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
"""Convert payload to dictionary for JSON serialization."""
|
|
||||||
payload_dict = {
|
|
||||||
"id": self.id,
|
|
||||||
"dataname": self.dataname,
|
|
||||||
"type": self.type,
|
|
||||||
"transport": self.transport,
|
|
||||||
"encoding": self.encoding,
|
|
||||||
"size": self.size,
|
|
||||||
}
|
|
||||||
|
|
||||||
# Include data based on transport type
|
|
||||||
if self.transport == "direct" and self.data is not None:
|
|
||||||
if self.encoding == "base64" or self.encoding == "json":
|
|
||||||
payload_dict["data"] = self.data
|
|
||||||
else:
|
|
||||||
# For other encodings, use base64
|
|
||||||
payload_dict["data"] = self._to_base64(self.data)
|
|
||||||
elif self.transport == "link" and self.data is not None:
|
|
||||||
# For link transport, data is a URL string
|
|
||||||
payload_dict["data"] = self.data
|
|
||||||
|
|
||||||
if self.metadata:
|
|
||||||
payload_dict["metadata"] = self.metadata
|
|
||||||
|
|
||||||
return payload_dict
|
|
||||||
|
|
||||||
def _to_base64(self, data):
|
|
||||||
"""Convert bytes to base64 string."""
|
|
||||||
if isinstance(data, bytes):
|
|
||||||
# Simple base64 encoding without library
|
|
||||||
import ubinascii
|
|
||||||
return ubinascii.b2a_base64(data).decode('utf-8').strip()
|
|
||||||
return data
|
|
||||||
|
|
||||||
def _from_base64(self, data):
|
|
||||||
"""Convert base64 string to bytes."""
|
|
||||||
import ubinascii
|
|
||||||
return ubinascii.a2b_base64(data)
|
|
||||||
|
|
||||||
|
|
||||||
class MessageEnvelope:
|
|
||||||
"""Internal message envelope structure containing multiple payloads with metadata."""
|
|
||||||
|
|
||||||
def __init__(self, send_to, payloads, correlation_id="", msg_id="", timestamp="",
|
|
||||||
msg_purpose="", sender_name="", sender_id="", receiver_name="",
|
|
||||||
receiver_id="", reply_to="", reply_to_msg_id="", broker_url=DEFAULT_NATS_URL,
|
|
||||||
metadata=None):
|
|
||||||
"""
|
|
||||||
Initialize a MessageEnvelope.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
send_to: NATS subject/topic to publish the message to
|
|
||||||
payloads: List of MessagePayload objects
|
|
||||||
correlation_id: Unique identifier to track messages (auto-generated if empty)
|
|
||||||
msg_id: Unique message identifier (auto-generated if empty)
|
|
||||||
timestamp: Message publication timestamp
|
|
||||||
msg_purpose: Purpose of the message ("ACK", "NACK", "updateStatus", "shutdown", "chat", etc.)
|
|
||||||
sender_name: Name of the sender
|
|
||||||
sender_id: UUID of the sender
|
|
||||||
receiver_name: Name of the receiver (empty means broadcast)
|
|
||||||
receiver_id: UUID of the receiver (empty means broadcast)
|
|
||||||
reply_to: Topic where receiver should reply
|
|
||||||
reply_to_msg_id: Message ID this message is replying to
|
|
||||||
broker_url: NATS broker URL
|
|
||||||
metadata: Optional message-level metadata
|
|
||||||
"""
|
|
||||||
self.correlation_id = correlation_id if correlation_id else self._generate_uuid()
|
|
||||||
self.msg_id = msg_id if msg_id else self._generate_uuid()
|
|
||||||
self.timestamp = timestamp if timestamp else self._get_timestamp()
|
|
||||||
self.send_to = send_to
|
|
||||||
self.msg_purpose = msg_purpose
|
|
||||||
self.sender_name = sender_name
|
|
||||||
self.sender_id = sender_id if sender_id else self._generate_uuid()
|
|
||||||
self.receiver_name = receiver_name
|
|
||||||
self.receiver_id = receiver_id if receiver_id else self._generate_uuid()
|
|
||||||
self.reply_to = reply_to
|
|
||||||
self.reply_to_msg_id = reply_to_msg_id
|
|
||||||
self.broker_url = broker_url
|
|
||||||
self.metadata = metadata if metadata else {}
|
|
||||||
self.payloads = payloads
|
|
||||||
|
|
||||||
def _generate_uuid(self):
|
|
||||||
"""Generate a UUID string."""
|
|
||||||
return str(uuid.uuid4())
|
|
||||||
|
|
||||||
def _get_timestamp(self):
|
|
||||||
"""Get current timestamp in ISO format."""
|
|
||||||
# Simplified timestamp - Micropython may not have full datetime
|
|
||||||
return "2026-02-21T" + time.strftime("%H:%M:%S", time.localtime())
|
|
||||||
|
|
||||||
def to_json(self):
|
|
||||||
"""Convert envelope to JSON string."""
|
|
||||||
obj = {
|
|
||||||
"correlationId": self.correlation_id,
|
|
||||||
"msgId": self.msg_id,
|
|
||||||
"timestamp": self.timestamp,
|
|
||||||
"sendTo": self.send_to,
|
|
||||||
"msgPurpose": self.msg_purpose,
|
|
||||||
"senderName": self.sender_name,
|
|
||||||
"senderId": self.sender_id,
|
|
||||||
"receiverName": self.receiver_name,
|
|
||||||
"receiverId": self.receiver_id,
|
|
||||||
"replyTo": self.reply_to,
|
|
||||||
"replyToMsgId": self.reply_to_msg_id,
|
|
||||||
"brokerURL": self.broker_url
|
|
||||||
}
|
|
||||||
|
|
||||||
# Include metadata if not empty
|
|
||||||
if self.metadata:
|
|
||||||
obj["metadata"] = self.metadata
|
|
||||||
|
|
||||||
# Convert payloads to JSON array
|
|
||||||
if self.payloads:
|
|
||||||
payloads_json = []
|
|
||||||
for payload in self.payloads:
|
|
||||||
payloads_json.append(payload.to_dict())
|
|
||||||
obj["payloads"] = payloads_json
|
|
||||||
|
|
||||||
return json.dumps(obj)
|
|
||||||
|
|
||||||
|
|
||||||
def log_trace(correlation_id, message):
|
|
||||||
"""Log a trace message with correlation ID and timestamp."""
|
|
||||||
timestamp = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime())
|
|
||||||
print("[{}] [Correlation: {}] {}".format(timestamp, correlation_id, message))
|
|
||||||
|
|
||||||
|
|
||||||
def _serialize_data(data, msg_type):
|
|
||||||
"""Serialize data according to specified format.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
data: Data to serialize
|
|
||||||
msg_type: Target format ("text", "dictionary", "table", "image", "audio", "video", "binary")
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
bytes: Binary representation of the serialized data
|
|
||||||
"""
|
|
||||||
if msg_type == "text":
|
|
||||||
if isinstance(data, str):
|
|
||||||
return data.encode('utf-8')
|
|
||||||
else:
|
|
||||||
raise ValueError("Text data must be a string")
|
|
||||||
|
|
||||||
elif msg_type == "dictionary":
|
|
||||||
if isinstance(data, dict):
|
|
||||||
json_str = json.dumps(data)
|
|
||||||
return json_str.encode('utf-8')
|
|
||||||
else:
|
|
||||||
raise ValueError("Dictionary data must be a dict")
|
|
||||||
|
|
||||||
elif msg_type in ("image", "audio", "video", "binary"):
|
|
||||||
if isinstance(data, bytes):
|
|
||||||
return data
|
|
||||||
else:
|
|
||||||
raise ValueError("{} data must be bytes".format(msg_type.capitalize()))
|
|
||||||
|
|
||||||
else:
|
|
||||||
raise ValueError("Unknown type: {}".format(msg_type))
|
|
||||||
|
|
||||||
|
|
||||||
def _deserialize_data(data_bytes, msg_type, correlation_id):
|
|
||||||
"""Deserialize bytes to data based on type.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
data_bytes: Serialized data as bytes
|
|
||||||
msg_type: Data type ("text", "dictionary", "table", "image", "audio", "video", "binary")
|
|
||||||
correlation_id: Correlation ID for logging
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Deserialized data
|
|
||||||
"""
|
|
||||||
if msg_type == "text":
|
|
||||||
return data_bytes.decode('utf-8')
|
|
||||||
|
|
||||||
elif msg_type == "dictionary":
|
|
||||||
json_str = data_bytes.decode('utf-8')
|
|
||||||
return json.loads(json_str)
|
|
||||||
|
|
||||||
elif msg_type in ("image", "audio", "video", "binary"):
|
|
||||||
return data_bytes
|
|
||||||
|
|
||||||
else:
|
|
||||||
raise ValueError("Unknown type: {}".format(msg_type))
|
|
||||||
|
|
||||||
|
|
||||||
class NATSConnection:
|
|
||||||
"""Simple NATS connection for Micropython."""
|
|
||||||
|
|
||||||
def __init__(self, url=DEFAULT_NATS_URL):
|
|
||||||
"""Initialize NATS connection.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
url: NATS server URL (e.g., "nats://localhost:4222")
|
|
||||||
"""
|
|
||||||
self.url = url
|
|
||||||
self.host = "localhost"
|
|
||||||
self.port = 4222
|
|
||||||
self.conn = None
|
|
||||||
self._parse_url(url)
|
|
||||||
|
|
||||||
def _parse_url(self, url):
|
|
||||||
"""Parse NATS URL to extract host and port."""
|
|
||||||
if url.startswith("nats://"):
|
|
||||||
url = url[7:]
|
|
||||||
elif url.startswith("tls://"):
|
|
||||||
url = url[6:]
|
|
||||||
|
|
||||||
if ":" in url:
|
|
||||||
self.host, port_str = url.split(":")
|
|
||||||
self.port = int(port_str)
|
|
||||||
else:
|
|
||||||
self.host = url
|
|
||||||
|
|
||||||
def connect(self):
|
|
||||||
"""Connect to NATS server."""
|
|
||||||
addr = usocket.getaddrinfo(self.host, self.port)[0][-1]
|
|
||||||
self.conn = usocket.socket()
|
|
||||||
self.conn.connect(addr)
|
|
||||||
log_trace("", "Connected to NATS server at {}:{}".format(self.host, self.port))
|
|
||||||
|
|
||||||
def publish(self, subject, message):
|
|
||||||
"""Publish a message to a NATS subject.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
subject: NATS subject to publish to
|
|
||||||
message: Message to publish (should be bytes or string)
|
|
||||||
"""
|
|
||||||
if isinstance(message, str):
|
|
||||||
message = message.encode('utf-8')
|
|
||||||
|
|
||||||
# Simple NATS protocol implementation
|
|
||||||
msg = "PUB {} {}\r\n".format(subject, len(message))
|
|
||||||
msg = msg.encode('utf-8') + message + b"\r\n"
|
|
||||||
self.conn.send(msg)
|
|
||||||
log_trace("", "Message published to {}".format(subject))
|
|
||||||
|
|
||||||
def subscribe(self, subject, callback):
|
|
||||||
"""Subscribe to a NATS subject.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
subject: NATS subject to subscribe to
|
|
||||||
callback: Callback function to handle incoming messages
|
|
||||||
"""
|
|
||||||
log_trace("", "Subscribed to {}".format(subject))
|
|
||||||
# Simplified subscription - in a real implementation, you'd handle SUB/PUB messages
|
|
||||||
# For Micropython, we'll use a simple polling approach
|
|
||||||
self.subscribed_subject = subject
|
|
||||||
self.subscription_callback = callback
|
|
||||||
|
|
||||||
def wait_message(self, timeout=1000):
|
|
||||||
"""Wait for incoming message.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
timeout: Timeout in milliseconds
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
NATS message object or None if timeout
|
|
||||||
"""
|
|
||||||
# Simplified message reading
|
|
||||||
# In a real implementation, you'd read from the socket
|
|
||||||
# For now, this is a placeholder
|
|
||||||
return None
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
"""Close the NATS connection."""
|
|
||||||
if self.conn:
|
|
||||||
self.conn.close()
|
|
||||||
self.conn = None
|
|
||||||
log_trace("", "NATS connection closed")
|
|
||||||
|
|
||||||
|
|
||||||
def _fetch_with_backoff(url, max_retries=5, base_delay=100, max_delay=5000, correlation_id=""):
|
|
||||||
"""Fetch data from URL with exponential backoff.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
url: URL to fetch from
|
|
||||||
max_retries: Maximum number of retry attempts
|
|
||||||
base_delay: Initial delay in milliseconds
|
|
||||||
max_delay: Maximum delay in milliseconds
|
|
||||||
correlation_id: Correlation ID for logging
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
bytes: Fetched data
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
Exception: If all retry attempts fail
|
|
||||||
"""
|
|
||||||
delay = base_delay
|
|
||||||
for attempt in range(1, max_retries + 1):
|
|
||||||
try:
|
|
||||||
# Simple HTTP GET request
|
|
||||||
# This is a simplified implementation
|
|
||||||
# For production, you'd want a proper HTTP client
|
|
||||||
import urequests
|
|
||||||
response = urequests.get(url)
|
|
||||||
if response.status_code == 200:
|
|
||||||
log_trace(correlation_id, "Successfully fetched data from {} on attempt {}".format(url, attempt))
|
|
||||||
return response.content
|
|
||||||
else:
|
|
||||||
raise Exception("Failed to fetch: {}".format(response.status_code))
|
|
||||||
except Exception as e:
|
|
||||||
log_trace(correlation_id, "Attempt {} failed: {}".format(attempt, str(e)))
|
|
||||||
if attempt < max_retries:
|
|
||||||
time.sleep(delay / 1000.0)
|
|
||||||
delay = min(delay * 2, max_delay)
|
|
||||||
|
|
||||||
|
|
||||||
def plik_oneshot_upload(file_server_url, filename, data):
|
|
||||||
"""Upload a single file to a plik server using one-shot mode.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
file_server_url: Base URL of the plik server
|
|
||||||
filename: Name of the file being uploaded
|
|
||||||
data: Raw byte data of the file content
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
dict: Dictionary with keys:
|
|
||||||
- "status": HTTP server response status
|
|
||||||
- "uploadid": ID of the one-shot upload session
|
|
||||||
- "fileid": ID of the uploaded file within the session
|
|
||||||
- "url": Full URL to download the uploaded file
|
|
||||||
"""
|
|
||||||
import urequests
|
|
||||||
import json
|
|
||||||
|
|
||||||
# Get upload ID
|
|
||||||
url_get_upload_id = "{}/upload".format(file_server_url)
|
|
||||||
headers = {"Content-Type": "application/json"}
|
|
||||||
body = json.dumps({"OneShot": True})
|
|
||||||
|
|
||||||
response = urequests.post(url_get_upload_id, headers=headers, data=body)
|
|
||||||
response_json = json.loads(response.content)
|
|
||||||
|
|
||||||
uploadid = response_json.get("id")
|
|
||||||
uploadtoken = response_json.get("uploadToken")
|
|
||||||
|
|
||||||
# Upload file
|
|
||||||
url_upload = "{}/file/{}".format(file_server_url, uploadid)
|
|
||||||
headers = {"X-UploadToken": uploadtoken}
|
|
||||||
|
|
||||||
# For Micropython, we need to construct the multipart form data manually
|
|
||||||
# This is a simplified approach
|
|
||||||
boundary = "----WebKitFormBoundary{}".format(uuid.uuid4().hex[:16])
|
|
||||||
|
|
||||||
# Create multipart body
|
|
||||||
part1 = "--{}\r\n".format(boundary)
|
|
||||||
part1 += "Content-Disposition: form-data; name=\"file\"; filename=\"{}\"\r\n".format(filename)
|
|
||||||
part1 += "Content-Type: application/octet-stream\r\n\r\n"
|
|
||||||
part1_bytes = part1.encode('utf-8')
|
|
||||||
|
|
||||||
part2 = "\r\n--{}--".format(boundary)
|
|
||||||
part2_bytes = part2.encode('utf-8')
|
|
||||||
|
|
||||||
# Combine all parts
|
|
||||||
full_body = part1_bytes + data + part2_bytes
|
|
||||||
|
|
||||||
# Set content type with boundary
|
|
||||||
content_type = "multipart/form-data; boundary={}".format(boundary)
|
|
||||||
|
|
||||||
response = urequests.post(url_upload, headers={"Content-Type": content_type}, data=full_body)
|
|
||||||
response_json = json.loads(response.content)
|
|
||||||
|
|
||||||
fileid = response_json.get("id")
|
|
||||||
url = "{}/file/{}/{}".format(file_server_url, uploadid, filename)
|
|
||||||
|
|
||||||
return {
|
|
||||||
"status": response.status_code,
|
|
||||||
"uploadid": uploadid,
|
|
||||||
"fileid": fileid,
|
|
||||||
"url": url
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def smartsend(subject, data, nats_url=DEFAULT_NATS_URL, fileserver_url=DEFAULT_FILESERVER_URL,
|
|
||||||
fileserver_upload_handler=plik_oneshot_upload, size_threshold=DEFAULT_SIZE_THRESHOLD,
|
|
||||||
correlation_id=None, msg_purpose="chat", sender_name="NATSBridge",
|
|
||||||
receiver_name="", receiver_id="", reply_to="", reply_to_msg_id=""):
|
|
||||||
"""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 and publishes
|
|
||||||
only the download URL over NATS.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
subject: NATS subject to publish the message to
|
|
||||||
data: List of (dataname, data, type) tuples to send
|
|
||||||
nats_url: URL of the NATS server
|
|
||||||
fileserver_url: URL of the HTTP file server
|
|
||||||
fileserver_upload_handler: Function to handle fileserver uploads
|
|
||||||
size_threshold: Threshold in bytes separating direct vs link transport
|
|
||||||
correlation_id: Optional correlation ID for tracing
|
|
||||||
msg_purpose: Purpose of the message
|
|
||||||
sender_name: Name of the sender
|
|
||||||
receiver_name: Name of the receiver
|
|
||||||
receiver_id: UUID of the receiver
|
|
||||||
reply_to: Topic to reply to
|
|
||||||
reply_to_msg_id: Message ID this message is replying to
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
MessageEnvelope: The envelope object for tracking
|
|
||||||
"""
|
|
||||||
# Generate correlation ID if not provided
|
|
||||||
cid = correlation_id if correlation_id else str(uuid.uuid4())
|
|
||||||
|
|
||||||
log_trace(cid, "Starting smartsend for subject: {}".format(subject))
|
|
||||||
|
|
||||||
# Generate message metadata
|
|
||||||
msg_id = str(uuid.uuid4())
|
|
||||||
|
|
||||||
# Process each payload in the list
|
|
||||||
payloads = []
|
|
||||||
|
|
||||||
for dataname, payload_data, payload_type in data:
|
|
||||||
# Serialize data based on type
|
|
||||||
payload_bytes = _serialize_data(payload_data, payload_type)
|
|
||||||
|
|
||||||
payload_size = len(payload_bytes)
|
|
||||||
log_trace(cid, "Serialized payload '{}' (type: {}) size: {} bytes".format(
|
|
||||||
dataname, payload_type, payload_size))
|
|
||||||
|
|
||||||
# Decision: Direct vs Link
|
|
||||||
if payload_size < size_threshold:
|
|
||||||
# Direct path - Base64 encode and send via NATS
|
|
||||||
payload_b64 = _serialize_data(payload_bytes, "binary") # Already bytes
|
|
||||||
# Convert to base64 string for JSON
|
|
||||||
import ubinascii
|
|
||||||
payload_b64_str = ubinascii.b2a_base64(payload_bytes).decode('utf-8').strip()
|
|
||||||
|
|
||||||
log_trace(cid, "Using direct transport for {} bytes".format(payload_size))
|
|
||||||
|
|
||||||
# Create MessagePayload for direct transport
|
|
||||||
payload = MessagePayload(
|
|
||||||
payload_b64_str,
|
|
||||||
payload_type,
|
|
||||||
id=str(uuid.uuid4()),
|
|
||||||
dataname=dataname,
|
|
||||||
transport="direct",
|
|
||||||
encoding="base64",
|
|
||||||
size=payload_size,
|
|
||||||
metadata={"payload_bytes": payload_size}
|
|
||||||
)
|
|
||||||
payloads.append(payload)
|
|
||||||
else:
|
|
||||||
# Link path - Upload to HTTP server, send URL via NATS
|
|
||||||
log_trace(cid, "Using link transport, uploading to fileserver")
|
|
||||||
|
|
||||||
# Upload to HTTP server
|
|
||||||
response = fileserver_upload_handler(fileserver_url, dataname, payload_bytes)
|
|
||||||
|
|
||||||
if response["status"] != 200:
|
|
||||||
raise Exception("Failed to upload data to fileserver: {}".format(response["status"]))
|
|
||||||
|
|
||||||
url = response["url"]
|
|
||||||
log_trace(cid, "Uploaded to URL: {}".format(url))
|
|
||||||
|
|
||||||
# Create MessagePayload for link transport
|
|
||||||
payload = MessagePayload(
|
|
||||||
url,
|
|
||||||
payload_type,
|
|
||||||
id=str(uuid.uuid4()),
|
|
||||||
dataname=dataname,
|
|
||||||
transport="link",
|
|
||||||
encoding="none",
|
|
||||||
size=payload_size,
|
|
||||||
metadata={}
|
|
||||||
)
|
|
||||||
payloads.append(payload)
|
|
||||||
|
|
||||||
# Create MessageEnvelope with all payloads
|
|
||||||
env = MessageEnvelope(
|
|
||||||
subject,
|
|
||||||
payloads,
|
|
||||||
correlation_id=cid,
|
|
||||||
msg_id=msg_id,
|
|
||||||
msg_purpose=msg_purpose,
|
|
||||||
sender_name=sender_name,
|
|
||||||
sender_id=str(uuid.uuid4()),
|
|
||||||
receiver_name=receiver_name,
|
|
||||||
receiver_id=receiver_id,
|
|
||||||
reply_to=reply_to,
|
|
||||||
reply_to_msg_id=reply_to_msg_id,
|
|
||||||
broker_url=nats_url,
|
|
||||||
metadata={}
|
|
||||||
)
|
|
||||||
|
|
||||||
msg_json = env.to_json()
|
|
||||||
|
|
||||||
# Publish to NATS
|
|
||||||
nats_conn = NATSConnection(nats_url)
|
|
||||||
nats_conn.connect()
|
|
||||||
nats_conn.publish(subject, msg_json)
|
|
||||||
nats_conn.close()
|
|
||||||
|
|
||||||
return env
|
|
||||||
|
|
||||||
|
|
||||||
def smartreceive(msg, fileserver_download_handler=_fetch_with_backoff, max_retries=5,
|
|
||||||
base_delay=100, max_delay=5000):
|
|
||||||
"""Receive and process messages from NATS.
|
|
||||||
|
|
||||||
This function processes incoming NATS messages, handling both direct transport
|
|
||||||
(base64 decoded payloads) and link transport (URL-based payloads).
|
|
||||||
|
|
||||||
Args:
|
|
||||||
msg: NATS message to process (dict with payload data)
|
|
||||||
fileserver_download_handler: Function to handle downloading data from file server URLs
|
|
||||||
max_retries: Maximum retry attempts for fetching URL
|
|
||||||
base_delay: Initial delay for exponential backoff in ms
|
|
||||||
max_delay: Maximum delay for exponential backoff in ms
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
list: List of (dataname, data, type) tuples
|
|
||||||
"""
|
|
||||||
# Parse the JSON envelope
|
|
||||||
json_data = msg if isinstance(msg, dict) else json.loads(msg)
|
|
||||||
log_trace(json_data.get("correlationId", ""), "Processing received message")
|
|
||||||
|
|
||||||
# Process all payloads in the envelope
|
|
||||||
payloads_list = []
|
|
||||||
|
|
||||||
# Get number of payloads
|
|
||||||
num_payloads = len(json_data.get("payloads", []))
|
|
||||||
|
|
||||||
for i in range(num_payloads):
|
|
||||||
payload = json_data["payloads"][i]
|
|
||||||
transport = payload.get("transport", "")
|
|
||||||
dataname = payload.get("dataname", "")
|
|
||||||
|
|
||||||
if transport == "direct":
|
|
||||||
log_trace(json_data.get("correlationId", ""),
|
|
||||||
"Direct transport - decoding payload '{}'".format(dataname))
|
|
||||||
|
|
||||||
# Extract base64 payload from the payload
|
|
||||||
payload_b64 = payload.get("data", "")
|
|
||||||
|
|
||||||
# Decode Base64 payload
|
|
||||||
import ubinascii
|
|
||||||
payload_bytes = ubinascii.a2b_base64(payload_b64.encode('utf-8'))
|
|
||||||
|
|
||||||
# Deserialize based on type
|
|
||||||
data_type = payload.get("type", "")
|
|
||||||
data = _deserialize_data(payload_bytes, data_type, json_data.get("correlationId", ""))
|
|
||||||
|
|
||||||
payloads_list.append((dataname, data, data_type))
|
|
||||||
|
|
||||||
elif transport == "link":
|
|
||||||
# Extract download URL from the payload
|
|
||||||
url = payload.get("data", "")
|
|
||||||
log_trace(json_data.get("correlationId", ""),
|
|
||||||
"Link transport - fetching '{}' from URL: {}".format(dataname, url))
|
|
||||||
|
|
||||||
# Fetch with exponential backoff
|
|
||||||
downloaded_data = fileserver_download_handler(
|
|
||||||
url, max_retries, base_delay, max_delay, json_data.get("correlationId", "")
|
|
||||||
)
|
|
||||||
|
|
||||||
# Deserialize based on type
|
|
||||||
data_type = payload.get("type", "")
|
|
||||||
data = _deserialize_data(downloaded_data, data_type, json_data.get("correlationId", ""))
|
|
||||||
|
|
||||||
payloads_list.append((dataname, data, data_type))
|
|
||||||
|
|
||||||
else:
|
|
||||||
raise ValueError("Unknown transport type for payload '{}': {}".format(dataname, transport))
|
|
||||||
|
|
||||||
return payloads_list
|
|
||||||
|
|
||||||
|
|
||||||
# Utility functions
|
|
||||||
def generate_uuid():
|
|
||||||
"""Generate a UUID string."""
|
|
||||||
return str(uuid.uuid4())
|
|
||||||
|
|
||||||
|
|
||||||
def get_timestamp():
|
|
||||||
"""Get current timestamp in ISO format."""
|
|
||||||
return time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime())
|
|
||||||
|
|
||||||
|
|
||||||
# Example usage
|
|
||||||
if __name__ == "__main__":
|
|
||||||
print("NATSBridge for Micropython")
|
|
||||||
print("=========================")
|
|
||||||
print("This module provides:")
|
|
||||||
print(" - MessageEnvelope: Message envelope structure")
|
|
||||||
print(" - MessagePayload: Payload structure")
|
|
||||||
print(" - smartsend: Send data via NATS with automatic transport selection")
|
|
||||||
print(" - smartreceive: Receive and process messages from NATS")
|
|
||||||
print(" - plik_oneshot_upload: Upload files to HTTP file server")
|
|
||||||
print(" - _fetch_with_backoff: Fetch data from URLs with retry logic")
|
|
||||||
print()
|
|
||||||
print("Usage:")
|
|
||||||
print(" from nats_bridge import smartsend, smartreceive")
|
|
||||||
print(" data = [(\"message\", \"Hello World\", \"text\")]")
|
|
||||||
print(" env = smartsend(\"my.subject\", data)")
|
|
||||||
print()
|
|
||||||
print(" # On receiver:")
|
|
||||||
print(" payloads = smartreceive(msg)")
|
|
||||||
print(" for dataname, data, type in payloads:")
|
|
||||||
print(" print(f\"Received {dataname} of type {type}: {data}\")")
|
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
// Test script for Dictionary transport testing
|
|
||||||
// Tests receiving 1 large and 1 small Dictionaries via direct and link transport
|
|
||||||
// Uses NATSBridge.js smartreceive with "dictionary" type
|
|
||||||
|
|
||||||
const { smartreceive, log_trace } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
// Configuration
|
|
||||||
const SUBJECT = "/NATSBridge_dict_test";
|
|
||||||
const NATS_URL = "nats.yiem.cc";
|
|
||||||
|
|
||||||
// Helper: Log with correlation ID
|
|
||||||
function log_trace(message) {
|
|
||||||
const timestamp = new Date().toISOString();
|
|
||||||
console.log(`[${timestamp}] ${message}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Receiver: Listen for messages and verify Dictionary handling
|
|
||||||
async function test_dict_receive() {
|
|
||||||
// Connect to NATS
|
|
||||||
const { connect } = require('nats');
|
|
||||||
const nc = await connect({ servers: [NATS_URL] });
|
|
||||||
|
|
||||||
// Subscribe to the subject
|
|
||||||
const sub = nc.subscribe(SUBJECT);
|
|
||||||
|
|
||||||
for await (const msg of sub) {
|
|
||||||
log_trace(`Received message on ${msg.subject}`);
|
|
||||||
|
|
||||||
// Use NATSBridge.smartreceive to handle the data
|
|
||||||
const result = await smartreceive(
|
|
||||||
msg,
|
|
||||||
{
|
|
||||||
maxRetries: 5,
|
|
||||||
baseDelay: 100,
|
|
||||||
maxDelay: 5000
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// Result is a list of {dataname, data, type} objects
|
|
||||||
for (const { dataname, data, type } of result) {
|
|
||||||
if (typeof data === 'object' && data !== null && !Array.isArray(data)) {
|
|
||||||
log_trace(`Received Dictionary '${dataname}' of type ${type}`);
|
|
||||||
|
|
||||||
// Display dictionary contents
|
|
||||||
console.log(" Contents:");
|
|
||||||
for (const [key, value] of Object.entries(data)) {
|
|
||||||
console.log(` ${key} => ${value}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save to JSON file
|
|
||||||
const fs = require('fs');
|
|
||||||
const output_path = `./received_${dataname}.json`;
|
|
||||||
const json_str = JSON.stringify(data, null, 2);
|
|
||||||
fs.writeFileSync(output_path, json_str);
|
|
||||||
log_trace(`Saved Dictionary to ${output_path}`);
|
|
||||||
} else {
|
|
||||||
log_trace(`Received unexpected data type for '${dataname}': ${typeof data}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep listening for 10 seconds
|
|
||||||
setTimeout(() => {
|
|
||||||
nc.close();
|
|
||||||
process.exit(0);
|
|
||||||
}, 120000);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run the test
|
|
||||||
console.log("Starting Dictionary transport test...");
|
|
||||||
console.log("Note: This receiver will wait for messages from the sender.");
|
|
||||||
console.log("Run test_js_to_js_dict_sender.js first to send test data.");
|
|
||||||
|
|
||||||
// Run receiver
|
|
||||||
console.log("testing smartreceive");
|
|
||||||
test_dict_receive();
|
|
||||||
|
|
||||||
console.log("Test completed.");
|
|
||||||
@@ -1,164 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
// Test script for Dictionary transport testing
|
|
||||||
// Tests sending 1 large and 1 small Dictionaries via direct and link transport
|
|
||||||
// Uses NATSBridge.js smartsend with "dictionary" type
|
|
||||||
|
|
||||||
const { smartsend, uuid4, log_trace } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
// Configuration
|
|
||||||
const SUBJECT = "/NATSBridge_dict_test";
|
|
||||||
const NATS_URL = "nats.yiem.cc";
|
|
||||||
const FILESERVER_URL = "http://192.168.88.104:8080";
|
|
||||||
|
|
||||||
// Create correlation ID for tracing
|
|
||||||
const correlation_id = uuid4();
|
|
||||||
|
|
||||||
// Helper: Log with correlation ID
|
|
||||||
function log_trace(message) {
|
|
||||||
const timestamp = new Date().toISOString();
|
|
||||||
console.log(`[${timestamp}] [Correlation: ${correlation_id}] ${message}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// File upload handler for plik server
|
|
||||||
async function plik_upload_handler(fileserver_url, dataname, data, correlation_id) {
|
|
||||||
// Get upload ID
|
|
||||||
const url_getUploadID = `${fileserver_url}/upload`;
|
|
||||||
const headers = {
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
};
|
|
||||||
const body = JSON.stringify({ OneShot: true });
|
|
||||||
|
|
||||||
let response = await fetch(url_getUploadID, {
|
|
||||||
method: "POST",
|
|
||||||
headers: headers,
|
|
||||||
body: body
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Failed to get upload ID: ${response.status} ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const responseJson = await response.json();
|
|
||||||
const uploadid = responseJson.id;
|
|
||||||
const uploadtoken = responseJson.uploadToken;
|
|
||||||
|
|
||||||
// Upload file
|
|
||||||
const formData = new FormData();
|
|
||||||
const blob = new Blob([data], { type: "application/octet-stream" });
|
|
||||||
formData.append("file", blob, dataname);
|
|
||||||
|
|
||||||
response = await fetch(`${fileserver_url}/file/${uploadid}`, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"X-UploadToken": uploadtoken
|
|
||||||
},
|
|
||||||
body: formData
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Failed to upload file: ${response.status} ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileResponseJson = await response.json();
|
|
||||||
const fileid = fileResponseJson.id;
|
|
||||||
|
|
||||||
const url = `${fileserver_url}/file/${uploadid}/${fileid}/${encodeURIComponent(dataname)}`;
|
|
||||||
|
|
||||||
return {
|
|
||||||
status: response.status,
|
|
||||||
uploadid: uploadid,
|
|
||||||
fileid: fileid,
|
|
||||||
url: url
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sender: Send Dictionaries via smartsend
|
|
||||||
async function test_dict_send() {
|
|
||||||
// Create a small Dictionary (will use direct transport)
|
|
||||||
const small_dict = {
|
|
||||||
name: "Alice",
|
|
||||||
age: 30,
|
|
||||||
scores: [95, 88, 92],
|
|
||||||
metadata: {
|
|
||||||
height: 155,
|
|
||||||
weight: 55
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Create a large Dictionary (will use link transport if > 1MB)
|
|
||||||
const large_dict_ids = [];
|
|
||||||
const large_dict_names = [];
|
|
||||||
const large_dict_scores = [];
|
|
||||||
const large_dict_categories = [];
|
|
||||||
|
|
||||||
for (let i = 0; i < 50000; i++) {
|
|
||||||
large_dict_ids.push(i + 1);
|
|
||||||
large_dict_names.push(`User_${i}`);
|
|
||||||
large_dict_scores.push(Math.floor(Math.random() * 100) + 1);
|
|
||||||
large_dict_categories.push(`Category_${Math.floor(Math.random() * 10) + 1}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const large_dict = {
|
|
||||||
ids: large_dict_ids,
|
|
||||||
names: large_dict_names,
|
|
||||||
scores: large_dict_scores,
|
|
||||||
categories: large_dict_categories,
|
|
||||||
metadata: {
|
|
||||||
source: "test_generator",
|
|
||||||
timestamp: new Date().toISOString()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Test data 1: small Dictionary
|
|
||||||
const data1 = { dataname: "small_dict", data: small_dict, type: "dictionary" };
|
|
||||||
|
|
||||||
// Test data 2: large Dictionary
|
|
||||||
const data2 = { dataname: "large_dict", data: large_dict, type: "dictionary" };
|
|
||||||
|
|
||||||
// Use smartsend with dictionary type
|
|
||||||
// For small Dictionary: will use direct transport (JSON encoded)
|
|
||||||
// For large Dictionary: will use link transport (uploaded to fileserver)
|
|
||||||
const env = await smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
[data1, data2],
|
|
||||||
{
|
|
||||||
natsUrl: NATS_URL,
|
|
||||||
fileserverUrl: FILESERVER_URL,
|
|
||||||
fileserverUploadHandler: plik_upload_handler,
|
|
||||||
sizeThreshold: 1_000_000,
|
|
||||||
correlationId: correlation_id,
|
|
||||||
msgPurpose: "chat",
|
|
||||||
senderName: "dict_sender",
|
|
||||||
receiverName: "",
|
|
||||||
receiverId: "",
|
|
||||||
replyTo: "",
|
|
||||||
replyToMsgId: ""
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
log_trace(`Sent message with ${env.payloads.length} payloads`);
|
|
||||||
|
|
||||||
// Log transport type for each payload
|
|
||||||
for (let i = 0; i < env.payloads.length; i++) {
|
|
||||||
const payload = env.payloads[i];
|
|
||||||
log_trace(`Payload ${i + 1} ('${payload.dataname}'):`);
|
|
||||||
log_trace(` Transport: ${payload.transport}`);
|
|
||||||
log_trace(` Type: ${payload.type}`);
|
|
||||||
log_trace(` Size: ${payload.size} bytes`);
|
|
||||||
log_trace(` Encoding: ${payload.encoding}`);
|
|
||||||
|
|
||||||
if (payload.transport === "link") {
|
|
||||||
log_trace(` URL: ${payload.data}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run the test
|
|
||||||
console.log("Starting Dictionary transport test...");
|
|
||||||
console.log(`Correlation ID: ${correlation_id}`);
|
|
||||||
|
|
||||||
// Run sender
|
|
||||||
console.log("start smartsend for dictionaries");
|
|
||||||
test_dict_send();
|
|
||||||
|
|
||||||
console.log("Test completed.");
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
// Test script for large payload testing using binary transport
|
|
||||||
// Tests receiving a large file (> 1MB) via smartsend with binary type
|
|
||||||
|
|
||||||
const { smartreceive, log_trace } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
// Configuration
|
|
||||||
const SUBJECT = "/NATSBridge_test";
|
|
||||||
const NATS_URL = "nats.yiem.cc";
|
|
||||||
|
|
||||||
// Helper: Log with correlation ID
|
|
||||||
function log_trace(message) {
|
|
||||||
const timestamp = new Date().toISOString();
|
|
||||||
console.log(`[${timestamp}] ${message}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Receiver: Listen for messages and verify large payload handling
|
|
||||||
async function test_large_binary_receive() {
|
|
||||||
// Connect to NATS
|
|
||||||
const { connect } = require('nats');
|
|
||||||
const nc = await connect({ servers: [NATS_URL] });
|
|
||||||
|
|
||||||
// Subscribe to the subject
|
|
||||||
const sub = nc.subscribe(SUBJECT);
|
|
||||||
|
|
||||||
for await (const msg of sub) {
|
|
||||||
log_trace(`Received message on ${msg.subject}`);
|
|
||||||
|
|
||||||
// Use NATSBridge.smartreceive to handle the data
|
|
||||||
const result = await smartreceive(
|
|
||||||
msg,
|
|
||||||
{
|
|
||||||
maxRetries: 5,
|
|
||||||
baseDelay: 100,
|
|
||||||
maxDelay: 5000
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// Result is a list of {dataname, data, type} objects
|
|
||||||
for (const { dataname, data, type } of result) {
|
|
||||||
if (data instanceof Uint8Array || Array.isArray(data)) {
|
|
||||||
const file_size = data.length;
|
|
||||||
log_trace(`Received ${file_size} bytes of binary data for '${dataname}' of type ${type}`);
|
|
||||||
|
|
||||||
// Save received data to a test file
|
|
||||||
const fs = require('fs');
|
|
||||||
const output_path = `./new_${dataname}`;
|
|
||||||
fs.writeFileSync(output_path, Buffer.from(data));
|
|
||||||
log_trace(`Saved received data to ${output_path}`);
|
|
||||||
} else {
|
|
||||||
log_trace(`Received unexpected data type for '${dataname}': ${typeof data}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep listening for 10 seconds
|
|
||||||
setTimeout(() => {
|
|
||||||
nc.close();
|
|
||||||
process.exit(0);
|
|
||||||
}, 120000);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run the test
|
|
||||||
console.log("Starting large binary payload test...");
|
|
||||||
|
|
||||||
// Run receiver
|
|
||||||
console.log("testing smartreceive");
|
|
||||||
test_large_binary_receive();
|
|
||||||
|
|
||||||
console.log("Test completed.");
|
|
||||||
@@ -1,143 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
// Test script for large payload testing using binary transport
|
|
||||||
// Tests sending a large file (> 1MB) via smartsend with binary type
|
|
||||||
|
|
||||||
const { smartsend, uuid4, log_trace } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
// Configuration
|
|
||||||
const SUBJECT = "/NATSBridge_test";
|
|
||||||
const NATS_URL = "nats.yiem.cc";
|
|
||||||
const FILESERVER_URL = "http://192.168.88.104:8080";
|
|
||||||
|
|
||||||
// Create correlation ID for tracing
|
|
||||||
const correlation_id = uuid4();
|
|
||||||
|
|
||||||
// Helper: Log with correlation ID
|
|
||||||
function log_trace(message) {
|
|
||||||
const timestamp = new Date().toISOString();
|
|
||||||
console.log(`[${timestamp}] [Correlation: ${correlation_id}] ${message}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// File upload handler for plik server
|
|
||||||
async function plik_upload_handler(fileserver_url, dataname, data, correlation_id) {
|
|
||||||
log_trace(correlation_id, `Uploading ${dataname} to fileserver: ${fileserver_url}`);
|
|
||||||
|
|
||||||
// Step 1: Get upload ID and token
|
|
||||||
const url_getUploadID = `${fileserver_url}/upload`;
|
|
||||||
const headers = {
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
};
|
|
||||||
const body = JSON.stringify({ OneShot: true });
|
|
||||||
|
|
||||||
let response = await fetch(url_getUploadID, {
|
|
||||||
method: "POST",
|
|
||||||
headers: headers,
|
|
||||||
body: body
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Failed to get upload ID: ${response.status} ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const responseJson = await response.json();
|
|
||||||
const uploadid = responseJson.id;
|
|
||||||
const uploadtoken = responseJson.uploadToken;
|
|
||||||
|
|
||||||
// Step 2: Upload file data
|
|
||||||
const url_upload = `${fileserver_url}/file/${uploadid}`;
|
|
||||||
|
|
||||||
// Create multipart form data
|
|
||||||
const formData = new FormData();
|
|
||||||
const blob = new Blob([data], { type: "application/octet-stream" });
|
|
||||||
formData.append("file", blob, dataname);
|
|
||||||
|
|
||||||
response = await fetch(url_upload, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"X-UploadToken": uploadtoken
|
|
||||||
},
|
|
||||||
body: formData
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Failed to upload file: ${response.status} ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileResponseJson = await response.json();
|
|
||||||
const fileid = fileResponseJson.id;
|
|
||||||
|
|
||||||
// Build the download URL
|
|
||||||
const url = `${fileserver_url}/file/${uploadid}/${fileid}/${encodeURIComponent(dataname)}`;
|
|
||||||
|
|
||||||
log_trace(correlation_id, `Uploaded to URL: ${url}`);
|
|
||||||
|
|
||||||
return {
|
|
||||||
status: response.status,
|
|
||||||
uploadid: uploadid,
|
|
||||||
fileid: fileid,
|
|
||||||
url: url
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sender: Send large binary file via smartsend
|
|
||||||
async function test_large_binary_send() {
|
|
||||||
// Read the large file as binary data
|
|
||||||
const fs = require('fs');
|
|
||||||
|
|
||||||
// Test data 1
|
|
||||||
const file_path1 = './testFile_large.zip';
|
|
||||||
const file_data1 = fs.readFileSync(file_path1);
|
|
||||||
const filename1 = 'testFile_large.zip';
|
|
||||||
const data1 = { dataname: filename1, data: file_data1, type: "binary" };
|
|
||||||
|
|
||||||
// Test data 2
|
|
||||||
const file_path2 = './testFile_small.zip';
|
|
||||||
const file_data2 = fs.readFileSync(file_path2);
|
|
||||||
const filename2 = 'testFile_small.zip';
|
|
||||||
const data2 = { dataname: filename2, data: file_data2, type: "binary" };
|
|
||||||
|
|
||||||
// Use smartsend with binary type - will automatically use link transport
|
|
||||||
// if file size exceeds the threshold (1MB by default)
|
|
||||||
const env = await smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
[data1, data2],
|
|
||||||
{
|
|
||||||
natsUrl: NATS_URL,
|
|
||||||
fileserverUrl: FILESERVER_URL,
|
|
||||||
fileserverUploadHandler: plik_upload_handler,
|
|
||||||
sizeThreshold: 1_000_000,
|
|
||||||
correlationId: correlation_id,
|
|
||||||
msgPurpose: "chat",
|
|
||||||
senderName: "sender",
|
|
||||||
receiverName: "",
|
|
||||||
receiverId: "",
|
|
||||||
replyTo: "",
|
|
||||||
replyToMsgId: ""
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
log_trace(`Sent message with transport: ${env.payloads[0].transport}`);
|
|
||||||
log_trace(`Envelope type: ${env.payloads[0].type}`);
|
|
||||||
|
|
||||||
// Check if link transport was used
|
|
||||||
if (env.payloads[0].transport === "link") {
|
|
||||||
log_trace("Using link transport - file uploaded to HTTP server");
|
|
||||||
log_trace(`URL: ${env.payloads[0].data}`);
|
|
||||||
} else {
|
|
||||||
log_trace("Using direct transport - payload sent via NATS");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run the test
|
|
||||||
console.log("Starting large binary payload test...");
|
|
||||||
console.log(`Correlation ID: ${correlation_id}`);
|
|
||||||
|
|
||||||
// Run sender first
|
|
||||||
console.log("start smartsend");
|
|
||||||
test_large_binary_send();
|
|
||||||
|
|
||||||
// Run receiver
|
|
||||||
// console.log("testing smartreceive");
|
|
||||||
// test_large_binary_receive();
|
|
||||||
|
|
||||||
console.log("Test completed.");
|
|
||||||
@@ -1,276 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
// Test script for mixed-content message testing
|
|
||||||
// Tests sending a mix of text, json, table, image, audio, video, and binary data
|
|
||||||
// from JavaScript serviceA to JavaScript serviceB using NATSBridge.js smartsend
|
|
||||||
//
|
|
||||||
// This test demonstrates that any combination and any number of mixed content
|
|
||||||
// can be sent and received correctly.
|
|
||||||
|
|
||||||
const { smartsend, uuid4, log_trace, _serialize_data } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
// Configuration
|
|
||||||
const SUBJECT = "/NATSBridge_mix_test";
|
|
||||||
const NATS_URL = "nats.yiem.cc";
|
|
||||||
const FILESERVER_URL = "http://192.168.88.104:8080";
|
|
||||||
|
|
||||||
// Create correlation ID for tracing
|
|
||||||
const correlation_id = uuid4();
|
|
||||||
|
|
||||||
// Helper: Log with correlation ID
|
|
||||||
function log_trace(message) {
|
|
||||||
const timestamp = new Date().toISOString();
|
|
||||||
console.log(`[${timestamp}] [Correlation: ${correlation_id}] ${message}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// File upload handler for plik server
|
|
||||||
async function plik_upload_handler(fileserver_url, dataname, data, correlation_id) {
|
|
||||||
log_trace(correlation_id, `Uploading ${dataname} to fileserver: ${fileserver_url}`);
|
|
||||||
|
|
||||||
// Step 1: Get upload ID and token
|
|
||||||
const url_getUploadID = `${fileserver_url}/upload`;
|
|
||||||
const headers = {
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
};
|
|
||||||
const body = JSON.stringify({ OneShot: true });
|
|
||||||
|
|
||||||
let response = await fetch(url_getUploadID, {
|
|
||||||
method: "POST",
|
|
||||||
headers: headers,
|
|
||||||
body: body
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Failed to get upload ID: ${response.status} ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const responseJson = await response.json();
|
|
||||||
const uploadid = responseJson.id;
|
|
||||||
const uploadtoken = responseJson.uploadToken;
|
|
||||||
|
|
||||||
// Step 2: Upload file data
|
|
||||||
const url_upload = `${fileserver_url}/file/${uploadid}`;
|
|
||||||
|
|
||||||
// Create multipart form data
|
|
||||||
const formData = new FormData();
|
|
||||||
const blob = new Blob([data], { type: "application/octet-stream" });
|
|
||||||
formData.append("file", blob, dataname);
|
|
||||||
|
|
||||||
response = await fetch(url_upload, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"X-UploadToken": uploadtoken
|
|
||||||
},
|
|
||||||
body: formData
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Failed to upload file: ${response.status} ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileResponseJson = await response.json();
|
|
||||||
const fileid = fileResponseJson.id;
|
|
||||||
|
|
||||||
// Build the download URL
|
|
||||||
const url = `${fileserver_url}/file/${uploadid}/${fileid}/${encodeURIComponent(dataname)}`;
|
|
||||||
|
|
||||||
log_trace(correlation_id, `Uploaded to URL: ${url}`);
|
|
||||||
|
|
||||||
return {
|
|
||||||
status: response.status,
|
|
||||||
uploadid: uploadid,
|
|
||||||
fileid: fileid,
|
|
||||||
url: url
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper: Create sample data for each type
|
|
||||||
function create_sample_data() {
|
|
||||||
// Text data (small - direct transport)
|
|
||||||
const text_data = "Hello! This is a test chat message. 🎉\nHow are you doing today? 😊";
|
|
||||||
|
|
||||||
// Dictionary/JSON data (medium - could be direct or link)
|
|
||||||
const dict_data = {
|
|
||||||
type: "chat",
|
|
||||||
sender: "serviceA",
|
|
||||||
receiver: "serviceB",
|
|
||||||
metadata: {
|
|
||||||
timestamp: new Date().toISOString(),
|
|
||||||
priority: "high",
|
|
||||||
tags: ["urgent", "chat", "test"]
|
|
||||||
},
|
|
||||||
content: {
|
|
||||||
text: "This is a JSON-formatted chat message with nested structure.",
|
|
||||||
format: "markdown",
|
|
||||||
mentions: ["user1", "user2"]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Table data (small - direct transport) - NOT IMPLEMENTED (requires apache-arrow)
|
|
||||||
// const table_data_small = {...};
|
|
||||||
|
|
||||||
// Table data (large - link transport) - NOT IMPLEMENTED (requires apache-arrow)
|
|
||||||
// const table_data_large = {...};
|
|
||||||
|
|
||||||
// Image data (small binary - direct transport)
|
|
||||||
// Create a simple 10x10 pixel PNG-like data
|
|
||||||
const image_width = 10;
|
|
||||||
const image_height = 10;
|
|
||||||
let image_data = new Uint8Array(128); // PNG header + pixel data
|
|
||||||
// PNG header
|
|
||||||
image_data[0] = 0x89;
|
|
||||||
image_data[1] = 0x50;
|
|
||||||
image_data[2] = 0x4E;
|
|
||||||
image_data[3] = 0x47;
|
|
||||||
image_data[4] = 0x0D;
|
|
||||||
image_data[5] = 0x0A;
|
|
||||||
image_data[6] = 0x1A;
|
|
||||||
image_data[7] = 0x0A;
|
|
||||||
// Simple RGB data (10*10*3 = 300 bytes)
|
|
||||||
for (let i = 0; i < 300; i++) {
|
|
||||||
image_data[i + 8] = 0xFF; // Red pixel
|
|
||||||
}
|
|
||||||
|
|
||||||
// Image data (large - link transport)
|
|
||||||
const large_image_width = 500;
|
|
||||||
const large_image_height = 1000;
|
|
||||||
const large_image_data = new Uint8Array(large_image_width * large_image_height * 3 + 8);
|
|
||||||
// PNG header
|
|
||||||
large_image_data[0] = 0x89;
|
|
||||||
large_image_data[1] = 0x50;
|
|
||||||
large_image_data[2] = 0x4E;
|
|
||||||
large_image_data[3] = 0x47;
|
|
||||||
large_image_data[4] = 0x0D;
|
|
||||||
large_image_data[5] = 0x0A;
|
|
||||||
large_image_data[6] = 0x1A;
|
|
||||||
large_image_data[7] = 0x0A;
|
|
||||||
// Random RGB data
|
|
||||||
for (let i = 0; i < large_image_width * large_image_height * 3; i++) {
|
|
||||||
large_image_data[i + 8] = Math.floor(Math.random() * 255);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Audio data (small binary - direct transport)
|
|
||||||
const audio_data = new Uint8Array(100);
|
|
||||||
for (let i = 0; i < 100; i++) {
|
|
||||||
audio_data[i] = Math.floor(Math.random() * 255);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Audio data (large - link transport)
|
|
||||||
const large_audio_data = new Uint8Array(1_500_000);
|
|
||||||
for (let i = 0; i < 1_500_000; i++) {
|
|
||||||
large_audio_data[i] = Math.floor(Math.random() * 255);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Video data (small binary - direct transport)
|
|
||||||
const video_data = new Uint8Array(150);
|
|
||||||
for (let i = 0; i < 150; i++) {
|
|
||||||
video_data[i] = Math.floor(Math.random() * 255);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Video data (large - link transport)
|
|
||||||
const large_video_data = new Uint8Array(1_500_000);
|
|
||||||
for (let i = 0; i < 1_500_000; i++) {
|
|
||||||
large_video_data[i] = Math.floor(Math.random() * 255);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Binary data (small - direct transport)
|
|
||||||
const binary_data = new Uint8Array(200);
|
|
||||||
for (let i = 0; i < 200; i++) {
|
|
||||||
binary_data[i] = Math.floor(Math.random() * 255);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Binary data (large - link transport)
|
|
||||||
const large_binary_data = new Uint8Array(1_500_000);
|
|
||||||
for (let i = 0; i < 1_500_000; i++) {
|
|
||||||
large_binary_data[i] = Math.floor(Math.random() * 255);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
text_data,
|
|
||||||
dict_data,
|
|
||||||
// table_data_small,
|
|
||||||
// table_data_large,
|
|
||||||
image_data,
|
|
||||||
large_image_data,
|
|
||||||
audio_data,
|
|
||||||
large_audio_data,
|
|
||||||
video_data,
|
|
||||||
large_video_data,
|
|
||||||
binary_data,
|
|
||||||
large_binary_data
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sender: Send mixed content via smartsend
|
|
||||||
async function test_mix_send() {
|
|
||||||
// Create sample data
|
|
||||||
const { text_data, dict_data, image_data, large_image_data, audio_data, large_audio_data, video_data, large_video_data, binary_data, large_binary_data } = create_sample_data();
|
|
||||||
|
|
||||||
// Create payloads list - mixed content with both small and large data
|
|
||||||
// Small data uses direct transport, large data uses link transport
|
|
||||||
const payloads = [
|
|
||||||
// Small data (direct transport) - text, dictionary
|
|
||||||
{ dataname: "chat_text", data: text_data, type: "text" },
|
|
||||||
{ dataname: "chat_json", data: dict_data, type: "dictionary" },
|
|
||||||
// { dataname: "chat_table_small", data: table_data_small, type: "table" },
|
|
||||||
|
|
||||||
// Large data (link transport) - large image, large audio, large video, large binary
|
|
||||||
// { dataname: "chat_table_large", data: table_data_large, type: "table" },
|
|
||||||
{ dataname: "user_image_large", data: large_image_data, type: "image" },
|
|
||||||
{ dataname: "audio_clip_large", data: large_audio_data, type: "audio" },
|
|
||||||
{ dataname: "video_clip_large", data: large_video_data, type: "video" },
|
|
||||||
{ dataname: "binary_file_large", data: large_binary_data, type: "binary" }
|
|
||||||
];
|
|
||||||
|
|
||||||
// Use smartsend with mixed content
|
|
||||||
const env = await smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
payloads,
|
|
||||||
{
|
|
||||||
natsUrl: NATS_URL,
|
|
||||||
fileserverUrl: FILESERVER_URL,
|
|
||||||
fileserverUploadHandler: plik_upload_handler,
|
|
||||||
sizeThreshold: 1_000_000,
|
|
||||||
correlationId: correlation_id,
|
|
||||||
msgPurpose: "chat",
|
|
||||||
senderName: "mix_sender",
|
|
||||||
receiverName: "",
|
|
||||||
receiverId: "",
|
|
||||||
replyTo: "",
|
|
||||||
replyToMsgId: ""
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
log_trace(`Sent message with ${env.payloads.length} payloads`);
|
|
||||||
|
|
||||||
// Log transport type for each payload
|
|
||||||
for (let i = 0; i < env.payloads.length; i++) {
|
|
||||||
const payload = env.payloads[i];
|
|
||||||
log_trace(`Payload ${i + 1} ('${payload.dataname}'):`);
|
|
||||||
log_trace(` Transport: ${payload.transport}`);
|
|
||||||
log_trace(` Type: ${payload.type}`);
|
|
||||||
log_trace(` Size: ${payload.size} bytes`);
|
|
||||||
log_trace(` Encoding: ${payload.encoding}`);
|
|
||||||
|
|
||||||
if (payload.transport === "link") {
|
|
||||||
log_trace(` URL: ${payload.data}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Summary
|
|
||||||
console.log("\n--- Transport Summary ---");
|
|
||||||
const direct_count = env.payloads.filter(p => p.transport === "direct").length;
|
|
||||||
const link_count = env.payloads.filter(p => p.transport === "link").length;
|
|
||||||
log_trace(`Direct transport: ${direct_count} payloads`);
|
|
||||||
log_trace(`Link transport: ${link_count} payloads`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run the test
|
|
||||||
console.log("Starting mixed-content transport test...");
|
|
||||||
console.log(`Correlation ID: ${correlation_id}`);
|
|
||||||
|
|
||||||
// Run sender
|
|
||||||
console.log("start smartsend for mixed content");
|
|
||||||
test_mix_send();
|
|
||||||
|
|
||||||
console.log("\nTest completed.");
|
|
||||||
console.log("Note: Run test_js_to_js_mix_receiver.js to receive the messages.");
|
|
||||||
@@ -1,172 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
// Test script for mixed-content message testing
|
|
||||||
// Tests receiving a mix of text, json, table, image, audio, video, and binary data
|
|
||||||
// from JavaScript serviceA to JavaScript serviceB using NATSBridge.js smartreceive
|
|
||||||
//
|
|
||||||
// This test demonstrates that any combination and any number of mixed content
|
|
||||||
// can be sent and received correctly.
|
|
||||||
|
|
||||||
const { smartreceive, log_trace } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
// Configuration
|
|
||||||
const SUBJECT = "/NATSBridge_mix_test";
|
|
||||||
const NATS_URL = "nats.yiem.cc";
|
|
||||||
|
|
||||||
// Helper: Log with correlation ID
|
|
||||||
function log_trace(message) {
|
|
||||||
const timestamp = new Date().toISOString();
|
|
||||||
console.log(`[${timestamp}] ${message}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Receiver: Listen for messages and verify mixed content handling
|
|
||||||
async function test_mix_receive() {
|
|
||||||
// Connect to NATS
|
|
||||||
const { connect } = require('nats');
|
|
||||||
const nc = await connect({ servers: [NATS_URL] });
|
|
||||||
|
|
||||||
// Subscribe to the subject
|
|
||||||
const sub = nc.subscribe(SUBJECT);
|
|
||||||
|
|
||||||
for await (const msg of sub) {
|
|
||||||
log_trace(`Received message on ${msg.subject}`);
|
|
||||||
|
|
||||||
// Use NATSBridge.smartreceive to handle the data
|
|
||||||
const result = await smartreceive(
|
|
||||||
msg,
|
|
||||||
{
|
|
||||||
maxRetries: 5,
|
|
||||||
baseDelay: 100,
|
|
||||||
maxDelay: 5000
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
log_trace(`Received ${result.length} payloads`);
|
|
||||||
|
|
||||||
// Result is a list of {dataname, data, type} objects
|
|
||||||
for (const { dataname, data, type } of result) {
|
|
||||||
log_trace(`\n=== Payload: ${dataname} (type: ${type}) ===`);
|
|
||||||
|
|
||||||
// Handle different data types
|
|
||||||
if (type === "text") {
|
|
||||||
// Text data - should be a String
|
|
||||||
if (typeof data === 'string') {
|
|
||||||
log_trace(` Type: String`);
|
|
||||||
log_trace(` Length: ${data.length} characters`);
|
|
||||||
|
|
||||||
// Display first 200 characters
|
|
||||||
if (data.length > 200) {
|
|
||||||
log_trace(` First 200 chars: ${data.substring(0, 200)}...`);
|
|
||||||
} else {
|
|
||||||
log_trace(` Content: ${data}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save to file
|
|
||||||
const fs = require('fs');
|
|
||||||
const output_path = `./received_${dataname}.txt`;
|
|
||||||
fs.writeFileSync(output_path, data);
|
|
||||||
log_trace(` Saved to: ${output_path}`);
|
|
||||||
} else {
|
|
||||||
log_trace(` ERROR: Expected String, got ${typeof data}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (type === "dictionary") {
|
|
||||||
// Dictionary data - should be an object
|
|
||||||
if (typeof data === 'object' && data !== null && !Array.isArray(data)) {
|
|
||||||
log_trace(` Type: Object`);
|
|
||||||
log_trace(` Keys: ${Object.keys(data).join(', ')}`);
|
|
||||||
|
|
||||||
// Display nested content
|
|
||||||
for (const [key, value] of Object.entries(data)) {
|
|
||||||
log_trace(` ${key} => ${value}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save to JSON file
|
|
||||||
const fs = require('fs');
|
|
||||||
const output_path = `./received_${dataname}.json`;
|
|
||||||
const json_str = JSON.stringify(data, null, 2);
|
|
||||||
fs.writeFileSync(output_path, json_str);
|
|
||||||
log_trace(` Saved to: ${output_path}`);
|
|
||||||
} else {
|
|
||||||
log_trace(` ERROR: Expected Object, got ${typeof data}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (type === "table") {
|
|
||||||
// Table data - should be an array of objects (requires apache-arrow)
|
|
||||||
log_trace(` Type: Array (requires apache-arrow for full deserialization)`);
|
|
||||||
if (Array.isArray(data)) {
|
|
||||||
log_trace(` Length: ${data.length} items`);
|
|
||||||
log_trace(` First item: ${JSON.stringify(data[0])}`);
|
|
||||||
} else {
|
|
||||||
log_trace(` ERROR: Expected Array, got ${typeof data}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (type === "image" || type === "audio" || type === "video" || type === "binary") {
|
|
||||||
// Binary data - should be Uint8Array
|
|
||||||
if (data instanceof Uint8Array || Array.isArray(data)) {
|
|
||||||
log_trace(` Type: Uint8Array (binary)`);
|
|
||||||
log_trace(` Size: ${data.length} bytes`);
|
|
||||||
|
|
||||||
// Save to file
|
|
||||||
const fs = require('fs');
|
|
||||||
const output_path = `./received_${dataname}.bin`;
|
|
||||||
fs.writeFileSync(output_path, Buffer.from(data));
|
|
||||||
log_trace(` Saved to: ${output_path}`);
|
|
||||||
} else {
|
|
||||||
log_trace(` ERROR: Expected Uint8Array, got ${typeof data}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
log_trace(` ERROR: Unknown data type '${type}'`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Summary
|
|
||||||
console.log("\n=== Verification Summary ===");
|
|
||||||
const text_count = result.filter(x => x.type === "text").length;
|
|
||||||
const dict_count = result.filter(x => x.type === "dictionary").length;
|
|
||||||
const table_count = result.filter(x => x.type === "table").length;
|
|
||||||
const image_count = result.filter(x => x.type === "image").length;
|
|
||||||
const audio_count = result.filter(x => x.type === "audio").length;
|
|
||||||
const video_count = result.filter(x => x.type === "video").length;
|
|
||||||
const binary_count = result.filter(x => x.type === "binary").length;
|
|
||||||
|
|
||||||
log_trace(`Text payloads: ${text_count}`);
|
|
||||||
log_trace(`Dictionary payloads: ${dict_count}`);
|
|
||||||
log_trace(`Table payloads: ${table_count}`);
|
|
||||||
log_trace(`Image payloads: ${image_count}`);
|
|
||||||
log_trace(`Audio payloads: ${audio_count}`);
|
|
||||||
log_trace(`Video payloads: ${video_count}`);
|
|
||||||
log_trace(`Binary payloads: ${binary_count}`);
|
|
||||||
|
|
||||||
// Print transport type info for each payload if available
|
|
||||||
console.log("\n=== Payload Details ===");
|
|
||||||
for (const { dataname, data, type } of result) {
|
|
||||||
if (["image", "audio", "video", "binary"].includes(type)) {
|
|
||||||
log_trace(`${dataname}: ${data.length} bytes (binary)`);
|
|
||||||
} else if (type === "table") {
|
|
||||||
log_trace(`${dataname}: ${data.length} items (Array)`);
|
|
||||||
} else if (type === "dictionary") {
|
|
||||||
log_trace(`${dataname}: ${JSON.stringify(data).length} bytes (Object)`);
|
|
||||||
} else if (type === "text") {
|
|
||||||
log_trace(`${dataname}: ${data.length} characters (String)`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep listening for 2 minutes
|
|
||||||
setTimeout(() => {
|
|
||||||
nc.close();
|
|
||||||
process.exit(0);
|
|
||||||
}, 120000);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run the test
|
|
||||||
console.log("Starting mixed-content transport test...");
|
|
||||||
console.log("Note: This receiver will wait for messages from the sender.");
|
|
||||||
console.log("Run test_js_to_js_mix_sender.js first to send test data.");
|
|
||||||
|
|
||||||
// Run receiver
|
|
||||||
console.log("\ntesting smartreceive for mixed content");
|
|
||||||
test_mix_receive();
|
|
||||||
|
|
||||||
console.log("\nTest completed.");
|
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
// Test script for Table transport testing
|
|
||||||
// Tests receiving 1 large and 1 small Tables via direct and link transport
|
|
||||||
// Uses NATSBridge.js smartreceive with "table" type
|
|
||||||
//
|
|
||||||
// Note: This test requires the apache-arrow library to deserialize table data.
|
|
||||||
// The JavaScript implementation uses apache-arrow for Arrow IPC deserialization.
|
|
||||||
|
|
||||||
const { smartreceive, log_trace } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
// Configuration
|
|
||||||
const SUBJECT = "/NATSBridge_table_test";
|
|
||||||
const NATS_URL = "nats.yiem.cc";
|
|
||||||
|
|
||||||
// Helper: Log with correlation ID
|
|
||||||
function log_trace(message) {
|
|
||||||
const timestamp = new Date().toISOString();
|
|
||||||
console.log(`[${timestamp}] ${message}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Receiver: Listen for messages and verify Table handling
|
|
||||||
async function test_table_receive() {
|
|
||||||
// Connect to NATS
|
|
||||||
const { connect } = require('nats');
|
|
||||||
const nc = await connect({ servers: [NATS_URL] });
|
|
||||||
|
|
||||||
// Subscribe to the subject
|
|
||||||
const sub = nc.subscribe(SUBJECT);
|
|
||||||
|
|
||||||
for await (const msg of sub) {
|
|
||||||
log_trace(`Received message on ${msg.subject}`);
|
|
||||||
|
|
||||||
// Use NATSBridge.smartreceive to handle the data
|
|
||||||
const result = await smartreceive(
|
|
||||||
msg,
|
|
||||||
{
|
|
||||||
maxRetries: 5,
|
|
||||||
baseDelay: 100,
|
|
||||||
maxDelay: 5000
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// Result is a list of {dataname, data, type} objects
|
|
||||||
for (const { dataname, data, type } of result) {
|
|
||||||
if (Array.isArray(data)) {
|
|
||||||
log_trace(`Received Table '${dataname}' of type ${type}`);
|
|
||||||
|
|
||||||
// Display table contents
|
|
||||||
console.log(` Dimensions: ${data.length} rows x ${data.length > 0 ? Object.keys(data[0]).length : 0} columns`);
|
|
||||||
console.log(` Columns: ${data.length > 0 ? Object.keys(data[0]).join(', ') : ''}`);
|
|
||||||
|
|
||||||
// Display first few rows
|
|
||||||
console.log(` First 5 rows:`);
|
|
||||||
for (let i = 0; i < Math.min(5, data.length); i++) {
|
|
||||||
console.log(` Row ${i}: ${JSON.stringify(data[i])}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save to JSON file
|
|
||||||
const fs = require('fs');
|
|
||||||
const output_path = `./received_${dataname}.json`;
|
|
||||||
const json_str = JSON.stringify(data, null, 2);
|
|
||||||
fs.writeFileSync(output_path, json_str);
|
|
||||||
log_trace(`Saved Table to ${output_path}`);
|
|
||||||
} else {
|
|
||||||
log_trace(`Received unexpected data type for '${dataname}': ${typeof data}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep listening for 10 seconds
|
|
||||||
setTimeout(() => {
|
|
||||||
nc.close();
|
|
||||||
process.exit(0);
|
|
||||||
}, 120000);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run the test
|
|
||||||
console.log("Starting Table transport test...");
|
|
||||||
console.log("Note: This receiver will wait for messages from the sender.");
|
|
||||||
console.log("Run test_js_to_js_table_sender.js first to send test data.");
|
|
||||||
|
|
||||||
// Run receiver
|
|
||||||
console.log("testing smartreceive");
|
|
||||||
test_table_receive();
|
|
||||||
|
|
||||||
console.log("Test completed.");
|
|
||||||
@@ -1,164 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
// Test script for Table transport testing
|
|
||||||
// Tests sending 1 large and 1 small Tables via direct and link transport
|
|
||||||
// Uses NATSBridge.js smartsend with "table" type
|
|
||||||
//
|
|
||||||
// Note: This test requires the apache-arrow library to serialize/deserialize table data.
|
|
||||||
// The JavaScript implementation uses apache-arrow for Arrow IPC serialization.
|
|
||||||
|
|
||||||
const { smartsend, uuid4, log_trace } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
// Configuration
|
|
||||||
const SUBJECT = "/NATSBridge_table_test";
|
|
||||||
const NATS_URL = "nats.yiem.cc";
|
|
||||||
const FILESERVER_URL = "http://192.168.88.104:8080";
|
|
||||||
|
|
||||||
// Create correlation ID for tracing
|
|
||||||
const correlation_id = uuid4();
|
|
||||||
|
|
||||||
// Helper: Log with correlation ID
|
|
||||||
function log_trace(message) {
|
|
||||||
const timestamp = new Date().toISOString();
|
|
||||||
console.log(`[${timestamp}] [Correlation: ${correlation_id}] ${message}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// File upload handler for plik server
|
|
||||||
async function plik_upload_handler(fileserver_url, dataname, data, correlation_id) {
|
|
||||||
log_trace(correlation_id, `Uploading ${dataname} to fileserver: ${fileserver_url}`);
|
|
||||||
|
|
||||||
// Step 1: Get upload ID and token
|
|
||||||
const url_getUploadID = `${fileserver_url}/upload`;
|
|
||||||
const headers = {
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
};
|
|
||||||
const body = JSON.stringify({ OneShot: true });
|
|
||||||
|
|
||||||
let response = await fetch(url_getUploadID, {
|
|
||||||
method: "POST",
|
|
||||||
headers: headers,
|
|
||||||
body: body
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Failed to get upload ID: ${response.status} ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const responseJson = await response.json();
|
|
||||||
const uploadid = responseJson.id;
|
|
||||||
const uploadtoken = responseJson.uploadToken;
|
|
||||||
|
|
||||||
// Step 2: Upload file data
|
|
||||||
const url_upload = `${fileserver_url}/file/${uploadid}`;
|
|
||||||
|
|
||||||
// Create multipart form data
|
|
||||||
const formData = new FormData();
|
|
||||||
const blob = new Blob([data], { type: "application/octet-stream" });
|
|
||||||
formData.append("file", blob, dataname);
|
|
||||||
|
|
||||||
response = await fetch(url_upload, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"X-UploadToken": uploadtoken
|
|
||||||
},
|
|
||||||
body: formData
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Failed to upload file: ${response.status} ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileResponseJson = await response.json();
|
|
||||||
const fileid = fileResponseJson.id;
|
|
||||||
|
|
||||||
// Build the download URL
|
|
||||||
const url = `${fileserver_url}/file/${uploadid}/${fileid}/${encodeURIComponent(dataname)}`;
|
|
||||||
|
|
||||||
log_trace(correlation_id, `Uploaded to URL: ${url}`);
|
|
||||||
|
|
||||||
return {
|
|
||||||
status: response.status,
|
|
||||||
uploadid: uploadid,
|
|
||||||
fileid: fileid,
|
|
||||||
url: url
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sender: Send Tables via smartsend
|
|
||||||
async function test_table_send() {
|
|
||||||
// Note: This test requires apache-arrow library to create Arrow IPC data.
|
|
||||||
// For now, we'll use a simple array of objects as table data.
|
|
||||||
// In production, you would use the apache-arrow library to create Arrow IPC data.
|
|
||||||
|
|
||||||
// Create a small Table (will use direct transport)
|
|
||||||
const small_table = [
|
|
||||||
{ id: 1, name: "Alice", score: 95 },
|
|
||||||
{ id: 2, name: "Bob", score: 88 },
|
|
||||||
{ id: 3, name: "Charlie", score: 92 }
|
|
||||||
];
|
|
||||||
|
|
||||||
// Create a large Table (will use link transport if > 1MB)
|
|
||||||
// Generate a larger dataset (~2MB to ensure link transport)
|
|
||||||
const large_table = [];
|
|
||||||
for (let i = 0; i < 50000; i++) {
|
|
||||||
large_table.push({
|
|
||||||
id: i,
|
|
||||||
message: `msg_${i}`,
|
|
||||||
sender: `sender_${i}`,
|
|
||||||
timestamp: new Date().toISOString(),
|
|
||||||
priority: Math.floor(Math.random() * 3) + 1
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test data 1: small Table
|
|
||||||
const data1 = { dataname: "small_table", data: small_table, type: "table" };
|
|
||||||
|
|
||||||
// Test data 2: large Table
|
|
||||||
const data2 = { dataname: "large_table", data: large_table, type: "table" };
|
|
||||||
|
|
||||||
// Use smartsend with table type
|
|
||||||
// For small Table: will use direct transport (Arrow IPC encoded)
|
|
||||||
// For large Table: will use link transport (uploaded to fileserver)
|
|
||||||
const env = await smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
[data1, data2],
|
|
||||||
{
|
|
||||||
natsUrl: NATS_URL,
|
|
||||||
fileserverUrl: FILESERVER_URL,
|
|
||||||
fileserverUploadHandler: plik_upload_handler,
|
|
||||||
sizeThreshold: 1_000_000,
|
|
||||||
correlationId: correlation_id,
|
|
||||||
msgPurpose: "chat",
|
|
||||||
senderName: "table_sender",
|
|
||||||
receiverName: "",
|
|
||||||
receiverId: "",
|
|
||||||
replyTo: "",
|
|
||||||
replyToMsgId: ""
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
log_trace(`Sent message with ${env.payloads.length} payloads`);
|
|
||||||
|
|
||||||
// Log transport type for each payload
|
|
||||||
for (let i = 0; i < env.payloads.length; i++) {
|
|
||||||
const payload = env.payloads[i];
|
|
||||||
log_trace(`Payload ${i + 1} ('${payload.dataname}'):`);
|
|
||||||
log_trace(` Transport: ${payload.transport}`);
|
|
||||||
log_trace(` Type: ${payload.type}`);
|
|
||||||
log_trace(` Size: ${payload.size} bytes`);
|
|
||||||
log_trace(` Encoding: ${payload.encoding}`);
|
|
||||||
|
|
||||||
if (payload.transport === "link") {
|
|
||||||
log_trace(` URL: ${payload.data}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run the test
|
|
||||||
console.log("Starting Table transport test...");
|
|
||||||
console.log(`Correlation ID: ${correlation_id}`);
|
|
||||||
|
|
||||||
// Run sender
|
|
||||||
console.log("start smartsend for tables");
|
|
||||||
test_table_send();
|
|
||||||
|
|
||||||
console.log("Test completed.");
|
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
// Test script for text transport testing
|
|
||||||
// Tests receiving 1 large and 1 small text from JavaScript serviceA to JavaScript serviceB
|
|
||||||
// Uses NATSBridge.js smartreceive with "text" type
|
|
||||||
|
|
||||||
const { smartreceive, log_trace } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
// Configuration
|
|
||||||
const SUBJECT = "/NATSBridge_text_test";
|
|
||||||
const NATS_URL = "nats.yiem.cc";
|
|
||||||
|
|
||||||
// Helper: Log with correlation ID
|
|
||||||
function log_trace(message) {
|
|
||||||
const timestamp = new Date().toISOString();
|
|
||||||
console.log(`[${timestamp}] ${message}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Receiver: Listen for messages and verify text handling
|
|
||||||
async function test_text_receive() {
|
|
||||||
// Connect to NATS
|
|
||||||
const { connect } = require('nats');
|
|
||||||
const nc = await connect({ servers: [NATS_URL] });
|
|
||||||
|
|
||||||
// Subscribe to the subject
|
|
||||||
const sub = nc.subscribe(SUBJECT);
|
|
||||||
|
|
||||||
for await (const msg of sub) {
|
|
||||||
log_trace(`Received message on ${msg.subject}`);
|
|
||||||
|
|
||||||
// Use NATSBridge.smartreceive to handle the data
|
|
||||||
const result = await smartreceive(
|
|
||||||
msg,
|
|
||||||
{
|
|
||||||
maxRetries: 5,
|
|
||||||
baseDelay: 100,
|
|
||||||
maxDelay: 5000
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// Result is a list of {dataname, data, type} objects
|
|
||||||
for (const { dataname, data, type } of result) {
|
|
||||||
if (typeof data === 'string') {
|
|
||||||
log_trace(`Received text '${dataname}' of type ${type}`);
|
|
||||||
log_trace(` Length: ${data.length} characters`);
|
|
||||||
|
|
||||||
// Display first 100 characters
|
|
||||||
if (data.length > 100) {
|
|
||||||
log_trace(` First 100 characters: ${data.substring(0, 100)}...`);
|
|
||||||
} else {
|
|
||||||
log_trace(` Content: ${data}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save to file
|
|
||||||
const fs = require('fs');
|
|
||||||
const output_path = `./received_${dataname}.txt`;
|
|
||||||
fs.writeFileSync(output_path, data);
|
|
||||||
log_trace(`Saved text to ${output_path}`);
|
|
||||||
} else {
|
|
||||||
log_trace(`Received unexpected data type for '${dataname}': ${typeof data}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep listening for 10 seconds
|
|
||||||
setTimeout(() => {
|
|
||||||
nc.close();
|
|
||||||
process.exit(0);
|
|
||||||
}, 120000);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run the test
|
|
||||||
console.log("Starting text transport test...");
|
|
||||||
console.log("Note: This receiver will wait for messages from the sender.");
|
|
||||||
console.log("Run test_js_to_js_text_sender.js first to send test data.");
|
|
||||||
|
|
||||||
// Run receiver
|
|
||||||
console.log("testing smartreceive for text");
|
|
||||||
test_text_receive();
|
|
||||||
|
|
||||||
console.log("Test completed.");
|
|
||||||
@@ -1,140 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
// Test script for text transport testing
|
|
||||||
// Tests sending 1 large and 1 small text from JavaScript serviceA to JavaScript serviceB
|
|
||||||
// Uses NATSBridge.js smartsend with "text" type
|
|
||||||
|
|
||||||
const { smartsend, uuid4, log_trace } = require('./src/NATSBridge');
|
|
||||||
|
|
||||||
// Configuration
|
|
||||||
const SUBJECT = "/NATSBridge_text_test";
|
|
||||||
const NATS_URL = "nats.yiem.cc";
|
|
||||||
const FILESERVER_URL = "http://192.168.88.104:8080";
|
|
||||||
|
|
||||||
// Create correlation ID for tracing
|
|
||||||
const correlation_id = uuid4();
|
|
||||||
|
|
||||||
// Helper: Log with correlation ID
|
|
||||||
function log_trace(message) {
|
|
||||||
const timestamp = new Date().toISOString();
|
|
||||||
console.log(`[${timestamp}] [Correlation: ${correlation_id}] ${message}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// File upload handler for plik server
|
|
||||||
async function plik_upload_handler(fileserver_url, dataname, data, correlation_id) {
|
|
||||||
// Get upload ID
|
|
||||||
const url_getUploadID = `${fileserver_url}/upload`;
|
|
||||||
const headers = {
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
};
|
|
||||||
const body = JSON.stringify({ OneShot: true });
|
|
||||||
|
|
||||||
let response = await fetch(url_getUploadID, {
|
|
||||||
method: "POST",
|
|
||||||
headers: headers,
|
|
||||||
body: body
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Failed to get upload ID: ${response.status} ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const responseJson = await response.json();
|
|
||||||
const uploadid = responseJson.id;
|
|
||||||
const uploadtoken = responseJson.uploadToken;
|
|
||||||
|
|
||||||
// Upload file
|
|
||||||
const formData = new FormData();
|
|
||||||
const blob = new Blob([data], { type: "application/octet-stream" });
|
|
||||||
formData.append("file", blob, dataname);
|
|
||||||
|
|
||||||
response = await fetch(`${fileserver_url}/file/${uploadid}`, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"X-UploadToken": uploadtoken
|
|
||||||
},
|
|
||||||
body: formData
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Failed to upload file: ${response.status} ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileResponseJson = await response.json();
|
|
||||||
const fileid = fileResponseJson.id;
|
|
||||||
|
|
||||||
const url = `${fileserver_url}/file/${uploadid}/${fileid}/${encodeURIComponent(dataname)}`;
|
|
||||||
|
|
||||||
return {
|
|
||||||
status: response.status,
|
|
||||||
uploadid: uploadid,
|
|
||||||
fileid: fileid,
|
|
||||||
url: url
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sender: Send text via smartsend
|
|
||||||
async function test_text_send() {
|
|
||||||
// Create a small text (will use direct transport)
|
|
||||||
const small_text = "Hello, this is a small text message. Testing direct transport via NATS.";
|
|
||||||
|
|
||||||
// Create a large text (will use link transport if > 1MB)
|
|
||||||
// Generate a larger text (~2MB to ensure link transport)
|
|
||||||
const large_text_lines = [];
|
|
||||||
for (let i = 0; i < 50000; i++) {
|
|
||||||
large_text_lines.push(`Line ${i}: This is a sample text line with some content to pad the size. `);
|
|
||||||
}
|
|
||||||
const large_text = large_text_lines.join("");
|
|
||||||
|
|
||||||
// Test data 1: small text
|
|
||||||
const data1 = { dataname: "small_text", data: small_text, type: "text" };
|
|
||||||
|
|
||||||
// Test data 2: large text
|
|
||||||
const data2 = { dataname: "large_text", data: large_text, type: "text" };
|
|
||||||
|
|
||||||
// Use smartsend with text type
|
|
||||||
// For small text: will use direct transport (Base64 encoded UTF-8)
|
|
||||||
// For large text: will use link transport (uploaded to fileserver)
|
|
||||||
const env = await smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
[data1, data2],
|
|
||||||
{
|
|
||||||
natsUrl: NATS_URL,
|
|
||||||
fileserverUrl: FILESERVER_URL,
|
|
||||||
fileserverUploadHandler: plik_upload_handler,
|
|
||||||
sizeThreshold: 1_000_000,
|
|
||||||
correlationId: correlation_id,
|
|
||||||
msgPurpose: "chat",
|
|
||||||
senderName: "text_sender",
|
|
||||||
receiverName: "",
|
|
||||||
receiverId: "",
|
|
||||||
replyTo: "",
|
|
||||||
replyToMsgId: ""
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
log_trace(`Sent message with ${env.payloads.length} payloads`);
|
|
||||||
|
|
||||||
// Log transport type for each payload
|
|
||||||
for (let i = 0; i < env.payloads.length; i++) {
|
|
||||||
const payload = env.payloads[i];
|
|
||||||
log_trace(`Payload ${i + 1} ('${payload.dataname}'):`);
|
|
||||||
log_trace(` Transport: ${payload.transport}`);
|
|
||||||
log_trace(` Type: ${payload.type}`);
|
|
||||||
log_trace(` Size: ${payload.size} bytes`);
|
|
||||||
log_trace(` Encoding: ${payload.encoding}`);
|
|
||||||
|
|
||||||
if (payload.transport === "link") {
|
|
||||||
log_trace(` URL: ${payload.data}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run the test
|
|
||||||
console.log("Starting text transport test...");
|
|
||||||
console.log(`Correlation ID: ${correlation_id}`);
|
|
||||||
|
|
||||||
// Run sender
|
|
||||||
console.log("start smartsend for text");
|
|
||||||
test_text_send();
|
|
||||||
|
|
||||||
console.log("Test completed.");
|
|
||||||
@@ -42,8 +42,8 @@ function test_dict_receive()
|
|||||||
max_delay = 5000
|
max_delay = 5000
|
||||||
)
|
)
|
||||||
|
|
||||||
# Result is a list of (dataname, data, data_type) tuples
|
# Result is an envelope dictionary with payloads field containing list of (dataname, data, data_type) tuples
|
||||||
for (dataname, data, data_type) in result
|
for (dataname, data, data_type) in result["payloads"]
|
||||||
if isa(data, JSON.Object{String, Any})
|
if isa(data, JSON.Object{String, Any})
|
||||||
log_trace("Received Dictionary '$dataname' of type $data_type")
|
log_trace("Received Dictionary '$dataname' of type $data_type")
|
||||||
|
|
||||||
@@ -92,12 +92,12 @@ function test_dict_send()
|
|||||||
# Use smartsend with dictionary type
|
# Use smartsend with dictionary type
|
||||||
# For small Dictionary: will use direct transport (JSON encoded)
|
# For small Dictionary: will use direct transport (JSON encoded)
|
||||||
# For large Dictionary: will use link transport (uploaded to fileserver)
|
# For large Dictionary: will use link transport (uploaded to fileserver)
|
||||||
env = NATSBridge.smartsend(
|
env, env_json_str = NATSBridge.smartsend(
|
||||||
SUBJECT,
|
SUBJECT,
|
||||||
[data1, data2], # List of (dataname, data, type) tuples
|
[data1, data2]; # List of (dataname, data, type) tuples
|
||||||
nats_url = NATS_URL,
|
broker_url = NATS_URL,
|
||||||
fileserver_url = FILESERVER_URL,
|
fileserver_url = FILESERVER_URL,
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
fileserver_upload_handler = plik_upload_handler,
|
||||||
size_threshold = 1_000_000, # 1MB threshold
|
size_threshold = 1_000_000, # 1MB threshold
|
||||||
correlation_id = correlation_id,
|
correlation_id = correlation_id,
|
||||||
msg_purpose = "chat",
|
msg_purpose = "chat",
|
||||||
@@ -105,7 +105,8 @@ function test_dict_send()
|
|||||||
receiver_name = "",
|
receiver_name = "",
|
||||||
receiver_id = "",
|
receiver_id = "",
|
||||||
reply_to = "",
|
reply_to = "",
|
||||||
reply_to_msg_id = ""
|
reply_to_msg_id = "",
|
||||||
|
is_publish = true # Publish the message to NATS
|
||||||
)
|
)
|
||||||
|
|
||||||
log_trace("Sent message with $(length(env.payloads)) payloads")
|
log_trace("Sent message with $(length(env.payloads)) payloads")
|
||||||
@@ -114,7 +115,7 @@ function test_dict_send()
|
|||||||
for (i, payload) in enumerate(env.payloads)
|
for (i, payload) in enumerate(env.payloads)
|
||||||
log_trace("Payload $i ('$payload.dataname'):")
|
log_trace("Payload $i ('$payload.dataname'):")
|
||||||
log_trace(" Transport: $(payload.transport)")
|
log_trace(" Transport: $(payload.transport)")
|
||||||
log_trace(" Type: $(payload.type)")
|
log_trace(" Type: $(payload.payload_type)")
|
||||||
log_trace(" Size: $(payload.size) bytes")
|
log_trace(" Size: $(payload.size) bytes")
|
||||||
log_trace(" Encoding: $(payload.encoding)")
|
log_trace(" Encoding: $(payload.encoding)")
|
||||||
|
|
||||||
@@ -44,8 +44,8 @@ function test_large_binary_receive()
|
|||||||
max_delay = 5000
|
max_delay = 5000
|
||||||
)
|
)
|
||||||
|
|
||||||
# Result is a list of (dataname, data) tuples
|
# Result is an envelope dictionary with payloads field containing list of (dataname, data, data_type) tuples
|
||||||
for (dataname, data, data_type) in result
|
for (dataname, data, data_type) in result["payloads"]
|
||||||
# Check transport type from the envelope
|
# Check transport type from the envelope
|
||||||
# For link transport, data is the URL string
|
# For link transport, data is the URL string
|
||||||
# For direct transport, data is the actual payload bytes
|
# For direct transport, data is the actual payload bytes
|
||||||
@@ -79,12 +79,12 @@ function test_large_binary_send()
|
|||||||
# Use smartsend with binary type - will automatically use link transport
|
# Use smartsend with binary type - will automatically use link transport
|
||||||
# if file size exceeds the threshold (1MB by default)
|
# if file size exceeds the threshold (1MB by default)
|
||||||
# API: smartsend(subject, [(dataname, data, type), ...]; keywords...)
|
# API: smartsend(subject, [(dataname, data, type), ...]; keywords...)
|
||||||
env = NATSBridge.smartsend(
|
env, env_json_str = NATSBridge.smartsend(
|
||||||
SUBJECT,
|
SUBJECT,
|
||||||
[data1, data2], # List of (dataname, data, type) tuples
|
[data1, data2]; # List of (dataname, data, type) tuples
|
||||||
nats_url = NATS_URL;
|
broker_url = NATS_URL;
|
||||||
fileserver_url = FILESERVER_URL,
|
fileserver_url = FILESERVER_URL,
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
fileserver_upload_handler = plik_upload_handler,
|
||||||
size_threshold = 1_000_000,
|
size_threshold = 1_000_000,
|
||||||
correlation_id = correlation_id,
|
correlation_id = correlation_id,
|
||||||
msg_purpose = "chat",
|
msg_purpose = "chat",
|
||||||
@@ -92,11 +92,12 @@ function test_large_binary_send()
|
|||||||
receiver_name = "",
|
receiver_name = "",
|
||||||
receiver_id = "",
|
receiver_id = "",
|
||||||
reply_to = "",
|
reply_to = "",
|
||||||
reply_to_msg_id = ""
|
reply_to_msg_id = "",
|
||||||
|
is_publish = true # Publish the message to NATS
|
||||||
)
|
)
|
||||||
|
|
||||||
log_trace("Sent message with transport: $(env.payloads[1].transport)")
|
log_trace("Sent message with transport: $(env.payloads[1].transport)")
|
||||||
log_trace("Envelope type: $(env.payloads[1].type)")
|
log_trace("Envelope type: $(env.payloads[1].payload_type)")
|
||||||
|
|
||||||
# Check if link transport was used
|
# Check if link transport was used
|
||||||
if env.payloads[1].transport == "link"
|
if env.payloads[1].transport == "link"
|
||||||
@@ -45,10 +45,10 @@ function test_mix_receive()
|
|||||||
max_delay = 5000
|
max_delay = 5000
|
||||||
)
|
)
|
||||||
|
|
||||||
log_trace("Received $(length(result)) payloads")
|
log_trace("Received $(length(result["payloads"])) payloads")
|
||||||
|
|
||||||
# Result is a list of (dataname, data, data_type) tuples
|
# Result is an envelope dictionary with payloads field containing list of (dataname, data, data_type) tuples
|
||||||
for (dataname, data, data_type) in result
|
for (dataname, data, data_type) in result["payloads"]
|
||||||
log_trace("\n=== Payload: $dataname (type: $data_type) ===")
|
log_trace("\n=== Payload: $dataname (type: $data_type) ===")
|
||||||
|
|
||||||
# Handle different data types
|
# Handle different data types
|
||||||
@@ -178,13 +178,13 @@ function test_mix_receive()
|
|||||||
|
|
||||||
# Summary
|
# Summary
|
||||||
println("\n=== Verification Summary ===")
|
println("\n=== Verification Summary ===")
|
||||||
text_count = count(x -> x[3] == "text", result)
|
text_count = count(x -> x[3] == "text", result["payloads"])
|
||||||
dict_count = count(x -> x[3] == "dictionary", result)
|
dict_count = count(x -> x[3] == "dictionary", result["payloads"])
|
||||||
table_count = count(x -> x[3] == "table", result)
|
table_count = count(x -> x[3] == "table", result["payloads"])
|
||||||
image_count = count(x -> x[3] == "image", result)
|
image_count = count(x -> x[3] == "image", result["payloads"])
|
||||||
audio_count = count(x -> x[3] == "audio", result)
|
audio_count = count(x -> x[3] == "audio", result["payloads"])
|
||||||
video_count = count(x -> x[3] == "video", result)
|
video_count = count(x -> x[3] == "video", result["payloads"])
|
||||||
binary_count = count(x -> x[3] == "binary", result)
|
binary_count = count(x -> x[3] == "binary", result["payloads"])
|
||||||
|
|
||||||
log_trace("Text payloads: $text_count")
|
log_trace("Text payloads: $text_count")
|
||||||
log_trace("Dictionary payloads: $dict_count")
|
log_trace("Dictionary payloads: $dict_count")
|
||||||
@@ -196,7 +196,7 @@ function test_mix_receive()
|
|||||||
|
|
||||||
# Print transport type info for each payload if available
|
# Print transport type info for each payload if available
|
||||||
println("\n=== Payload Details ===")
|
println("\n=== Payload Details ===")
|
||||||
for (dataname, data, data_type) in result
|
for (dataname, data, data_type) in result["payloads"]
|
||||||
if data_type in ["image", "audio", "video", "binary"]
|
if data_type in ["image", "audio", "video", "binary"]
|
||||||
log_trace("$dataname: $(length(data)) bytes (binary)")
|
log_trace("$dataname: $(length(data)) bytes (binary)")
|
||||||
elseif data_type == "table"
|
elseif data_type == "table"
|
||||||
@@ -186,12 +186,12 @@ function test_mix_send()
|
|||||||
]
|
]
|
||||||
|
|
||||||
# Use smartsend with mixed content
|
# Use smartsend with mixed content
|
||||||
env = NATSBridge.smartsend(
|
env, env_json_str = NATSBridge.smartsend(
|
||||||
SUBJECT,
|
SUBJECT,
|
||||||
payloads, # List of (dataname, data, type) tuples
|
payloads; # List of (dataname, data, type) tuples
|
||||||
nats_url = NATS_URL,
|
broker_url = NATS_URL,
|
||||||
fileserver_url = FILESERVER_URL,
|
fileserver_url = FILESERVER_URL,
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
fileserver_upload_handler = plik_upload_handler,
|
||||||
size_threshold = 1_000_000, # 1MB threshold
|
size_threshold = 1_000_000, # 1MB threshold
|
||||||
correlation_id = correlation_id,
|
correlation_id = correlation_id,
|
||||||
msg_purpose = "chat",
|
msg_purpose = "chat",
|
||||||
@@ -199,7 +199,8 @@ function test_mix_send()
|
|||||||
receiver_name = "",
|
receiver_name = "",
|
||||||
receiver_id = "",
|
receiver_id = "",
|
||||||
reply_to = "",
|
reply_to = "",
|
||||||
reply_to_msg_id = ""
|
reply_to_msg_id = "",
|
||||||
|
is_publish = true # Publish the message to NATS
|
||||||
)
|
)
|
||||||
|
|
||||||
log_trace("Sent message with $(length(env.payloads)) payloads")
|
log_trace("Sent message with $(length(env.payloads)) payloads")
|
||||||
@@ -208,7 +209,7 @@ function test_mix_send()
|
|||||||
for (i, payload) in enumerate(env.payloads)
|
for (i, payload) in enumerate(env.payloads)
|
||||||
log_trace("Payload $i ('$payload.dataname'):")
|
log_trace("Payload $i ('$payload.dataname'):")
|
||||||
log_trace(" Transport: $(payload.transport)")
|
log_trace(" Transport: $(payload.transport)")
|
||||||
log_trace(" Type: $(payload.type)")
|
log_trace(" Type: $(payload.payload_type)")
|
||||||
log_trace(" Size: $(payload.size) bytes")
|
log_trace(" Size: $(payload.size) bytes")
|
||||||
log_trace(" Encoding: $(payload.encoding)")
|
log_trace(" Encoding: $(payload.encoding)")
|
||||||
|
|
||||||
@@ -42,8 +42,8 @@ function test_table_receive()
|
|||||||
max_delay = 5000
|
max_delay = 5000
|
||||||
)
|
)
|
||||||
|
|
||||||
# Result is a list of (dataname, data, data_type) tuples
|
# Result is an envelope dictionary with payloads field containing list of (dataname, data, data_type) tuples
|
||||||
for (dataname, data, data_type) in result
|
for (dataname, data, data_type) in result["payloads"]
|
||||||
data = DataFrame(data)
|
data = DataFrame(data)
|
||||||
if isa(data, DataFrame)
|
if isa(data, DataFrame)
|
||||||
log_trace("Received DataFrame '$dataname' of type $data_type")
|
log_trace("Received DataFrame '$dataname' of type $data_type")
|
||||||
@@ -90,12 +90,12 @@ function test_table_send()
|
|||||||
# Use smartsend with table type
|
# Use smartsend with table type
|
||||||
# For small DataFrame: will use direct transport (Base64 encoded Arrow IPC)
|
# For small DataFrame: will use direct transport (Base64 encoded Arrow IPC)
|
||||||
# For large DataFrame: will use link transport (uploaded to fileserver)
|
# For large DataFrame: will use link transport (uploaded to fileserver)
|
||||||
env = NATSBridge.smartsend(
|
env, env_json_str = NATSBridge.smartsend(
|
||||||
SUBJECT,
|
SUBJECT,
|
||||||
[data1, data2], # List of (dataname, data, type) tuples
|
[data1, data2]; # List of (dataname, data, type) tuples
|
||||||
nats_url = NATS_URL,
|
broker_url = NATS_URL,
|
||||||
fileserver_url = FILESERVER_URL,
|
fileserver_url = FILESERVER_URL,
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
fileserver_upload_handler = plik_upload_handler,
|
||||||
size_threshold = 1_000_000, # 1MB threshold
|
size_threshold = 1_000_000, # 1MB threshold
|
||||||
correlation_id = correlation_id,
|
correlation_id = correlation_id,
|
||||||
msg_purpose = "chat",
|
msg_purpose = "chat",
|
||||||
@@ -103,7 +103,8 @@ function test_table_send()
|
|||||||
receiver_name = "",
|
receiver_name = "",
|
||||||
receiver_id = "",
|
receiver_id = "",
|
||||||
reply_to = "",
|
reply_to = "",
|
||||||
reply_to_msg_id = ""
|
reply_to_msg_id = "",
|
||||||
|
is_publish = true # Publish the message to NATS
|
||||||
)
|
)
|
||||||
|
|
||||||
log_trace("Sent message with $(length(env.payloads)) payloads")
|
log_trace("Sent message with $(length(env.payloads)) payloads")
|
||||||
@@ -112,7 +113,7 @@ function test_table_send()
|
|||||||
for (i, payload) in enumerate(env.payloads)
|
for (i, payload) in enumerate(env.payloads)
|
||||||
log_trace("Payload $i ('$payload.dataname'):")
|
log_trace("Payload $i ('$payload.dataname'):")
|
||||||
log_trace(" Transport: $(payload.transport)")
|
log_trace(" Transport: $(payload.transport)")
|
||||||
log_trace(" Type: $(payload.type)")
|
log_trace(" Type: $(payload.payload_type)")
|
||||||
log_trace(" Size: $(payload.size) bytes")
|
log_trace(" Size: $(payload.size) bytes")
|
||||||
log_trace(" Encoding: $(payload.encoding)")
|
log_trace(" Encoding: $(payload.encoding)")
|
||||||
|
|
||||||
@@ -42,8 +42,8 @@ function test_text_receive()
|
|||||||
max_delay = 5000
|
max_delay = 5000
|
||||||
)
|
)
|
||||||
|
|
||||||
# Result is a list of (dataname, data, data_type) tuples
|
# Result is an envelope dictionary with payloads field containing list of (dataname, data, data_type) tuples
|
||||||
for (dataname, data, data_type) in result
|
for (dataname, data, data_type) in result["payloads"]
|
||||||
if isa(data, String)
|
if isa(data, String)
|
||||||
log_trace("Received text '$dataname' of type $data_type")
|
log_trace("Received text '$dataname' of type $data_type")
|
||||||
log_trace(" Length: $(length(data)) characters")
|
log_trace(" Length: $(length(data)) characters")
|
||||||
@@ -75,12 +75,12 @@ function test_text_send()
|
|||||||
# Use smartsend with text type
|
# Use smartsend with text type
|
||||||
# For small text: will use direct transport (Base64 encoded UTF-8)
|
# For small text: will use direct transport (Base64 encoded UTF-8)
|
||||||
# For large text: will use link transport (uploaded to fileserver)
|
# For large text: will use link transport (uploaded to fileserver)
|
||||||
env = NATSBridge.smartsend(
|
env, env_json_str = NATSBridge.smartsend(
|
||||||
SUBJECT,
|
SUBJECT,
|
||||||
[data1, data2], # List of (dataname, data, type) tuples
|
[data1, data2]; # List of (dataname, data, type) tuples
|
||||||
nats_url = NATS_URL,
|
broker_url = NATS_URL,
|
||||||
fileserver_url = FILESERVER_URL,
|
fileserver_url = FILESERVER_URL,
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
fileserver_upload_handler = plik_upload_handler,
|
||||||
size_threshold = 1_000_000, # 1MB threshold
|
size_threshold = 1_000_000, # 1MB threshold
|
||||||
correlation_id = correlation_id,
|
correlation_id = correlation_id,
|
||||||
msg_purpose = "chat",
|
msg_purpose = "chat",
|
||||||
@@ -88,7 +88,8 @@ function test_text_send()
|
|||||||
receiver_name = "",
|
receiver_name = "",
|
||||||
receiver_id = "",
|
receiver_id = "",
|
||||||
reply_to = "",
|
reply_to = "",
|
||||||
reply_to_msg_id = ""
|
reply_to_msg_id = "",
|
||||||
|
is_publish = true # Publish the message to NATS
|
||||||
)
|
)
|
||||||
|
|
||||||
log_trace("Sent message with $(length(env.payloads)) payloads")
|
log_trace("Sent message with $(length(env.payloads)) payloads")
|
||||||
@@ -97,7 +98,7 @@ function test_text_send()
|
|||||||
for (i, payload) in enumerate(env.payloads)
|
for (i, payload) in enumerate(env.payloads)
|
||||||
log_trace("Payload $i ('$payload.dataname'):")
|
log_trace("Payload $i ('$payload.dataname'):")
|
||||||
log_trace(" Transport: $(payload.transport)")
|
log_trace(" Transport: $(payload.transport)")
|
||||||
log_trace(" Type: $(payload.type)")
|
log_trace(" Type: $(payload.payload_type)")
|
||||||
log_trace(" Size: $(payload.size) bytes")
|
log_trace(" Size: $(payload.size) bytes")
|
||||||
log_trace(" Encoding: $(payload.encoding)")
|
log_trace(" Encoding: $(payload.encoding)")
|
||||||
|
|
||||||
@@ -1,220 +0,0 @@
|
|||||||
"""
|
|
||||||
Micropython NATS Bridge - Basic Test Examples
|
|
||||||
|
|
||||||
This module demonstrates basic usage of the NATSBridge for Micropython.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
sys.path.insert(0, "../src")
|
|
||||||
|
|
||||||
from nats_bridge import MessageEnvelope, MessagePayload, smartsend, smartreceive, log_trace
|
|
||||||
import json
|
|
||||||
|
|
||||||
# ============================================= 100 ============================================== #
|
|
||||||
|
|
||||||
|
|
||||||
def test_text_message():
|
|
||||||
"""Test sending and receiving text messages."""
|
|
||||||
print("\n=== Test 1: Text Message ===")
|
|
||||||
|
|
||||||
# Send text message
|
|
||||||
data = [
|
|
||||||
("message", "Hello World", "text"),
|
|
||||||
("greeting", "Good morning!", "text")
|
|
||||||
]
|
|
||||||
|
|
||||||
env = smartsend(
|
|
||||||
"/test/text",
|
|
||||||
data,
|
|
||||||
nats_url="nats://localhost:4222",
|
|
||||||
size_threshold=1000000
|
|
||||||
)
|
|
||||||
|
|
||||||
print("Sent envelope:")
|
|
||||||
print(" Subject: {}".format(env.send_to))
|
|
||||||
print(" Correlation ID: {}".format(env.correlation_id))
|
|
||||||
print(" Payloads: {}".format(len(env.payloads)))
|
|
||||||
|
|
||||||
# Expected output on receiver:
|
|
||||||
# payloads = smartreceive(msg)
|
|
||||||
# for dataname, data, type in payloads:
|
|
||||||
# print("Received {}: {}".format(dataname, data))
|
|
||||||
|
|
||||||
|
|
||||||
def test_dictionary_message():
|
|
||||||
"""Test sending and receiving dictionary messages."""
|
|
||||||
print("\n=== Test 2: Dictionary Message ===")
|
|
||||||
|
|
||||||
# Send dictionary message
|
|
||||||
config = {
|
|
||||||
"step_size": 0.01,
|
|
||||||
"iterations": 1000,
|
|
||||||
"threshold": 0.5
|
|
||||||
}
|
|
||||||
|
|
||||||
data = [
|
|
||||||
("config", config, "dictionary")
|
|
||||||
]
|
|
||||||
|
|
||||||
env = smartsend(
|
|
||||||
"/test/dictionary",
|
|
||||||
data,
|
|
||||||
nats_url="nats://localhost:4222",
|
|
||||||
size_threshold=1000000
|
|
||||||
)
|
|
||||||
|
|
||||||
print("Sent envelope:")
|
|
||||||
print(" Subject: {}".format(env.send_to))
|
|
||||||
print(" Payloads: {}".format(len(env.payloads)))
|
|
||||||
|
|
||||||
# Expected output on receiver:
|
|
||||||
# payloads = smartreceive(msg)
|
|
||||||
# for dataname, data, type in payloads:
|
|
||||||
# if type == "dictionary":
|
|
||||||
# print("Config: {}".format(data))
|
|
||||||
|
|
||||||
|
|
||||||
def test_mixed_payloads():
|
|
||||||
"""Test sending mixed payload types in a single message."""
|
|
||||||
print("\n=== Test 3: Mixed Payloads ===")
|
|
||||||
|
|
||||||
# Mixed content: text, dictionary, and binary
|
|
||||||
image_data = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR" # PNG header (example)
|
|
||||||
|
|
||||||
data = [
|
|
||||||
("message_text", "Hello!", "text"),
|
|
||||||
("user_config", {"theme": "dark", "volume": 80}, "dictionary"),
|
|
||||||
("user_image", image_data, "binary")
|
|
||||||
]
|
|
||||||
|
|
||||||
env = smartsend(
|
|
||||||
"/test/mixed",
|
|
||||||
data,
|
|
||||||
nats_url="nats://localhost:4222",
|
|
||||||
size_threshold=1000000
|
|
||||||
)
|
|
||||||
|
|
||||||
print("Sent envelope:")
|
|
||||||
print(" Subject: {}".format(env.send_to))
|
|
||||||
print(" Payloads: {}".format(len(env.payloads)))
|
|
||||||
|
|
||||||
# Expected output on receiver:
|
|
||||||
# payloads = smartreceive(msg)
|
|
||||||
# for dataname, data, type in payloads:
|
|
||||||
# print("Received {}: {} (type: {})".format(dataname, data if type != "binary" else len(data), type))
|
|
||||||
|
|
||||||
|
|
||||||
def test_large_payload():
|
|
||||||
"""Test sending large payloads that require fileserver upload."""
|
|
||||||
print("\n=== Test 4: Large Payload (Link Transport) ===")
|
|
||||||
|
|
||||||
# Create large data (> 1MB would trigger link transport)
|
|
||||||
# For testing, we'll use a smaller size but configure threshold lower
|
|
||||||
large_data = b"A" * 100000 # 100KB
|
|
||||||
|
|
||||||
data = [
|
|
||||||
("large_data", large_data, "binary")
|
|
||||||
]
|
|
||||||
|
|
||||||
# Use a lower threshold for testing
|
|
||||||
env = smartsend(
|
|
||||||
"/test/large",
|
|
||||||
data,
|
|
||||||
nats_url="nats://localhost:4222",
|
|
||||||
fileserver_url="http://localhost:8080",
|
|
||||||
size_threshold=50000 # 50KB threshold for testing
|
|
||||||
)
|
|
||||||
|
|
||||||
print("Sent envelope:")
|
|
||||||
print(" Subject: {}".format(env.send_to))
|
|
||||||
print(" Payloads: {}".format(len(env.payloads)))
|
|
||||||
for p in env.payloads:
|
|
||||||
print(" - Transport: {}, Type: {}".format(p.transport, p.type))
|
|
||||||
|
|
||||||
|
|
||||||
def test_reply_to():
|
|
||||||
"""Test sending messages with reply-to functionality."""
|
|
||||||
print("\n=== Test 5: Reply To ===")
|
|
||||||
|
|
||||||
data = [
|
|
||||||
("command", {"action": "start"}, "dictionary")
|
|
||||||
]
|
|
||||||
|
|
||||||
env = smartsend(
|
|
||||||
"/test/command",
|
|
||||||
data,
|
|
||||||
nats_url="nats://localhost:4222",
|
|
||||||
reply_to="/test/response",
|
|
||||||
reply_to_msg_id="reply-123",
|
|
||||||
msg_purpose="command"
|
|
||||||
)
|
|
||||||
|
|
||||||
print("Sent envelope:")
|
|
||||||
print(" Subject: {}".format(env.send_to))
|
|
||||||
print(" Reply To: {}".format(env.reply_to))
|
|
||||||
print(" Reply To Msg ID: {}".format(env.reply_to_msg_id))
|
|
||||||
|
|
||||||
|
|
||||||
def test_correlation_id():
|
|
||||||
"""Test using custom correlation IDs for tracing."""
|
|
||||||
print("\n=== Test 6: Custom Correlation ID ===")
|
|
||||||
|
|
||||||
custom_cid = "trace-abc123"
|
|
||||||
data = [
|
|
||||||
("message", "Test with correlation ID", "text")
|
|
||||||
]
|
|
||||||
|
|
||||||
env = smartsend(
|
|
||||||
"/test/correlation",
|
|
||||||
data,
|
|
||||||
nats_url="nats://localhost:4222",
|
|
||||||
correlation_id=custom_cid
|
|
||||||
)
|
|
||||||
|
|
||||||
print("Sent envelope with correlation ID: {}".format(env.correlation_id))
|
|
||||||
print("This ID can be used to trace the message flow.")
|
|
||||||
|
|
||||||
|
|
||||||
def test_multiple_payloads():
|
|
||||||
"""Test sending multiple payloads in one message."""
|
|
||||||
print("\n=== Test 7: Multiple Payloads ===")
|
|
||||||
|
|
||||||
data = [
|
|
||||||
("text_message", "Hello", "text"),
|
|
||||||
("json_data", {"key": "value", "number": 42}, "dictionary"),
|
|
||||||
("table_data", b"\x01\x02\x03\x04", "binary"),
|
|
||||||
("audio_data", b"\x00\x01\x02\x03", "binary")
|
|
||||||
]
|
|
||||||
|
|
||||||
env = smartsend(
|
|
||||||
"/test/multiple",
|
|
||||||
data,
|
|
||||||
nats_url="nats://localhost:4222",
|
|
||||||
size_threshold=1000000
|
|
||||||
)
|
|
||||||
|
|
||||||
print("Sent {} payloads in one message".format(len(env.payloads)))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
print("Micropython NATS Bridge Test Suite")
|
|
||||||
print("==================================")
|
|
||||||
print()
|
|
||||||
|
|
||||||
# Run tests
|
|
||||||
test_text_message()
|
|
||||||
test_dictionary_message()
|
|
||||||
test_mixed_payloads()
|
|
||||||
test_large_payload()
|
|
||||||
test_reply_to()
|
|
||||||
test_correlation_id()
|
|
||||||
test_multiple_payloads()
|
|
||||||
|
|
||||||
print("\n=== All tests completed ===")
|
|
||||||
print()
|
|
||||||
print("Note: These tests require:")
|
|
||||||
print(" 1. A running NATS server at nats://localhost:4222")
|
|
||||||
print(" 2. An HTTP file server at http://localhost:8080 (for large payloads)")
|
|
||||||
print()
|
|
||||||
print("To run the tests:")
|
|
||||||
print(" python test_micropython_basic.py")
|
|
||||||
@@ -1,634 +0,0 @@
|
|||||||
# NATSBridge.jl Tutorial
|
|
||||||
|
|
||||||
A comprehensive tutorial for learning how to use NATSBridge.jl for bi-directional communication between Julia and JavaScript services using NATS.
|
|
||||||
|
|
||||||
## Table of Contents
|
|
||||||
|
|
||||||
1. [What is NATSBridge.jl?](#what-is-natsbridgejl)
|
|
||||||
2. [Key Concepts](#key-concepts)
|
|
||||||
3. [Installation](#installation)
|
|
||||||
4. [Basic Usage](#basic-usage)
|
|
||||||
5. [Payload Types](#payload-types)
|
|
||||||
6. [Transport Strategies](#transport-strategies)
|
|
||||||
7. [Advanced Features](#advanced-features)
|
|
||||||
8. [Complete Examples](#complete-examples)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## What is NATSBridge.jl?
|
|
||||||
|
|
||||||
NATSBridge.jl is a Julia module that provides a high-level API for sending and receiving data across network boundaries using NATS as the message bus. It implements the **Claim-Check pattern** for handling large payloads efficiently.
|
|
||||||
|
|
||||||
### Core Features
|
|
||||||
|
|
||||||
- **Bi-directional communication**: Julia ↔ JavaScript
|
|
||||||
- **Smart transport selection**: Automatic direct vs link transport based on payload size
|
|
||||||
- **Multi-payload support**: Send multiple payloads of different types in a single message
|
|
||||||
- **Claim-check pattern**: Upload large files to HTTP server, send only URLs via NATS
|
|
||||||
- **Type-aware serialization**: Different serialization strategies for different data types
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Key Concepts
|
|
||||||
|
|
||||||
### 1. msgEnvelope_v1 (Message Envelope)
|
|
||||||
|
|
||||||
The `msgEnvelope_v1` structure provides a comprehensive message format for bidirectional communication:
|
|
||||||
|
|
||||||
```julia
|
|
||||||
struct msgEnvelope_v1
|
|
||||||
correlationId::String # Unique identifier to track messages
|
|
||||||
msgId::String # This message id
|
|
||||||
timestamp::String # Message published timestamp
|
|
||||||
|
|
||||||
sendTo::String # Topic/subject the sender sends to
|
|
||||||
msgPurpose::String # Purpose (ACK | NACK | updateStatus | shutdown | chat)
|
|
||||||
senderName::String # Sender name (e.g., "agent-wine-web-frontend")
|
|
||||||
senderId::String # Sender id (uuid4)
|
|
||||||
receiverName::String # Message receiver name (e.g., "agent-backend")
|
|
||||||
receiverId::String # Message receiver id (uuid4 or nothing for broadcast)
|
|
||||||
replyTo::String # Topic to reply to
|
|
||||||
replyToMsgId::String # Message id this message is replying to
|
|
||||||
brokerURL::String # NATS server address
|
|
||||||
|
|
||||||
metadata::Dict{String, Any}
|
|
||||||
payloads::AbstractArray{msgPayload_v1} # Multiple payloads stored here
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2. msgPayload_v1 (Payload Structure)
|
|
||||||
|
|
||||||
The `msgPayload_v1` structure provides flexible payload handling:
|
|
||||||
|
|
||||||
```julia
|
|
||||||
struct msgPayload_v1
|
|
||||||
id::String # Id of this payload (e.g., "uuid4")
|
|
||||||
dataname::String # Name of this payload (e.g., "login_image")
|
|
||||||
type::String # "text | dictionary | table | image | audio | video | binary"
|
|
||||||
transport::String # "direct | link"
|
|
||||||
encoding::String # "none | json | base64 | arrow-ipc"
|
|
||||||
size::Integer # Data size in bytes
|
|
||||||
data::Any # Payload data in case of direct transport or a URL in case of link
|
|
||||||
metadata::Dict{String, Any} # Dict("checksum" => "sha256_hash", ...)
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3. Standard API Format
|
|
||||||
|
|
||||||
The system uses a **standardized list-of-tuples format** for all payload operations:
|
|
||||||
|
|
||||||
```julia
|
|
||||||
# Input format for smartsend (always a list of tuples with type info)
|
|
||||||
[(dataname1, data1, type1), (dataname2, data2, type2), ...]
|
|
||||||
|
|
||||||
# Output format for smartreceive (always returns a list of tuples)
|
|
||||||
[(dataname1, data1, type1), (dataname2, data2, type2), ...]
|
|
||||||
```
|
|
||||||
|
|
||||||
**Important**: Even when sending a single payload, you must wrap it in a list.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
```julia
|
|
||||||
using Pkg
|
|
||||||
Pkg.add("NATS")
|
|
||||||
Pkg.add("JSON")
|
|
||||||
Pkg.add("Arrow")
|
|
||||||
Pkg.add("HTTP")
|
|
||||||
Pkg.add("UUIDs")
|
|
||||||
Pkg.add("Dates")
|
|
||||||
Pkg.add("Base64")
|
|
||||||
Pkg.add("PrettyPrinting")
|
|
||||||
Pkg.add("DataFrames")
|
|
||||||
```
|
|
||||||
|
|
||||||
Then include the NATSBridge module:
|
|
||||||
|
|
||||||
```julia
|
|
||||||
include("NATSBridge.jl")
|
|
||||||
using .NATSBridge
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Basic Usage
|
|
||||||
|
|
||||||
### Sending Data (smartsend)
|
|
||||||
|
|
||||||
```julia
|
|
||||||
using NATSBridge
|
|
||||||
|
|
||||||
# Send a simple dictionary
|
|
||||||
data = Dict("key" => "value")
|
|
||||||
env = NATSBridge.smartsend("my.subject", [("dataname1", data, "dictionary")])
|
|
||||||
```
|
|
||||||
|
|
||||||
### Receiving Data (smartreceive)
|
|
||||||
|
|
||||||
```julia
|
|
||||||
using NATSBridge
|
|
||||||
|
|
||||||
# Subscribe to a NATS subject
|
|
||||||
NATS.subscribe("my.subject") do msg
|
|
||||||
# Process the message
|
|
||||||
result = NATSBridge.smartreceive(
|
|
||||||
msg,
|
|
||||||
max_retries = 5,
|
|
||||||
base_delay = 100,
|
|
||||||
max_delay = 5000
|
|
||||||
)
|
|
||||||
|
|
||||||
# result is a list of (dataname, data, type) tuples
|
|
||||||
for (dataname, data, type) in result
|
|
||||||
println("Received $dataname of type $type")
|
|
||||||
println("Data: $data")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Payload Types
|
|
||||||
|
|
||||||
NATSBridge.jl supports the following payload types:
|
|
||||||
|
|
||||||
| Type | Description | Serialization |
|
|
||||||
|------|-------------|---------------|
|
|
||||||
| `text` | Plain text | UTF-8 encoding |
|
|
||||||
| `dictionary` | JSON-serializable data (Dict, NamedTuple) | JSON |
|
|
||||||
| `table` | Tabular data (DataFrame, array of structs) | Apache Arrow IPC |
|
|
||||||
| `image` | Image data (Bitmap, PNG/JPG bytes) | Binary |
|
|
||||||
| `audio` | Audio data (WAV, MP3 bytes) | Binary |
|
|
||||||
| `video` | Video data (MP4, AVI bytes) | Binary |
|
|
||||||
| `binary` | Generic binary data | Binary |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Transport Strategies
|
|
||||||
|
|
||||||
NATSBridge.jl automatically selects the appropriate transport strategy based on payload size:
|
|
||||||
|
|
||||||
### Direct Transport (< 1MB)
|
|
||||||
|
|
||||||
Small payloads are encoded as Base64 and sent directly over NATS.
|
|
||||||
|
|
||||||
```julia
|
|
||||||
# Small data (< 1MB) - uses direct transport
|
|
||||||
small_data = rand(1000) # ~8KB
|
|
||||||
env = NATSBridge.smartsend("small", [("data", small_data, "table")])
|
|
||||||
```
|
|
||||||
|
|
||||||
### Link Transport (≥ 1MB)
|
|
||||||
|
|
||||||
Large payloads are uploaded to an HTTP file server, and only the URL is sent via NATS.
|
|
||||||
|
|
||||||
```julia
|
|
||||||
# Large data (≥ 1MB) - uses link transport
|
|
||||||
large_data = rand(10_000_000) # ~80MB
|
|
||||||
env = NATSBridge.smartsend("large", [("data", large_data, "table")])
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Complete Examples
|
|
||||||
|
|
||||||
### Example 1: Text Message
|
|
||||||
|
|
||||||
**Sender:**
|
|
||||||
```julia
|
|
||||||
using NATSBridge
|
|
||||||
using UUIDs
|
|
||||||
|
|
||||||
const SUBJECT = "/NATSBridge_text_test"
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
const FILESERVER_URL = "http://localhost:8080"
|
|
||||||
|
|
||||||
function plik_upload_handler(fileserver_url::String, dataname::String, data::Vector{UInt8})::Dict{String, Any}
|
|
||||||
url_getUploadID = "$fileserver_url/upload"
|
|
||||||
headers = ["Content-Type" => "application/json"]
|
|
||||||
body = """{ "OneShot" : true }"""
|
|
||||||
httpResponse = HTTP.request("POST", url_getUploadID, headers, body; body_is_form=false)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
uploadid = responseJson["id"]
|
|
||||||
uploadtoken = responseJson["uploadToken"]
|
|
||||||
|
|
||||||
file_multipart = HTTP.Multipart(dataname, IOBuffer(data), "application/octet-stream")
|
|
||||||
url_upload = "$fileserver_url/file/$uploadid"
|
|
||||||
headers = ["X-UploadToken" => uploadtoken]
|
|
||||||
|
|
||||||
form = HTTP.Form(Dict("file" => file_multipart))
|
|
||||||
httpResponse = HTTP.post(url_upload, headers, form)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
|
|
||||||
fileid = responseJson["id"]
|
|
||||||
url = "$fileserver_url/file/$uploadid/$fileid/$dataname"
|
|
||||||
|
|
||||||
return Dict("status" => httpResponse.status, "uploadid" => uploadid, "fileid" => fileid, "url" => url)
|
|
||||||
end
|
|
||||||
|
|
||||||
function test_text_send()
|
|
||||||
small_text = "Hello, this is a small text message."
|
|
||||||
large_text = join(["Line $i: " for i in 1:50000], "")
|
|
||||||
|
|
||||||
data1 = ("small_text", small_text, "text")
|
|
||||||
data2 = ("large_text", large_text, "text")
|
|
||||||
|
|
||||||
env = NATSBridge.smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
[data1, data2],
|
|
||||||
nats_url = NATS_URL,
|
|
||||||
fileserver_url = FILESERVER_URL,
|
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
|
||||||
size_threshold = 1_000_000,
|
|
||||||
correlation_id = string(uuid4()),
|
|
||||||
msg_purpose = "chat",
|
|
||||||
sender_name = "text_sender"
|
|
||||||
)
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
**Receiver:**
|
|
||||||
```julia
|
|
||||||
using NATSBridge
|
|
||||||
|
|
||||||
const SUBJECT = "/NATSBridge_text_test"
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
|
|
||||||
function test_text_receive()
|
|
||||||
conn = NATS.connect(NATS_URL)
|
|
||||||
NATS.subscribe(conn, SUBJECT) do msg
|
|
||||||
result = NATSBridge.smartreceive(
|
|
||||||
msg,
|
|
||||||
max_retries = 5,
|
|
||||||
base_delay = 100,
|
|
||||||
max_delay = 5000
|
|
||||||
)
|
|
||||||
|
|
||||||
for (dataname, data, data_type) in result
|
|
||||||
if data_type == "text"
|
|
||||||
println("Received text: $data")
|
|
||||||
write("./received_$dataname.txt", data)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
sleep(120)
|
|
||||||
NATS.drain(conn)
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
### Example 2: Dictionary (JSON) Message
|
|
||||||
|
|
||||||
**Sender:**
|
|
||||||
```julia
|
|
||||||
using NATSBridge
|
|
||||||
using UUIDs
|
|
||||||
|
|
||||||
const SUBJECT = "/NATSBridge_dict_test"
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
const FILESERVER_URL = "http://localhost:8080"
|
|
||||||
|
|
||||||
function plik_upload_handler(fileserver_url::String, dataname::String, data::Vector{UInt8})::Dict{String, Any}
|
|
||||||
url_getUploadID = "$fileserver_url/upload"
|
|
||||||
headers = ["Content-Type" => "application/json"]
|
|
||||||
body = """{ "OneShot" : true }"""
|
|
||||||
httpResponse = HTTP.request("POST", url_getUploadID, headers, body; body_is_form=false)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
uploadid = responseJson["id"]
|
|
||||||
uploadtoken = responseJson["uploadToken"]
|
|
||||||
|
|
||||||
file_multipart = HTTP.Multipart(dataname, IOBuffer(data), "application/octet-stream")
|
|
||||||
url_upload = "$fileserver_url/file/$uploadid"
|
|
||||||
headers = ["X-UploadToken" => uploadtoken]
|
|
||||||
|
|
||||||
form = HTTP.Form(Dict("file" => file_multipart))
|
|
||||||
httpResponse = HTTP.post(url_upload, headers, form)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
|
|
||||||
fileid = responseJson["id"]
|
|
||||||
url = "$fileserver_url/file/$uploadid/$fileid/$dataname"
|
|
||||||
|
|
||||||
return Dict("status" => httpResponse.status, "uploadid" => uploadid, "fileid" => fileid, "url" => url)
|
|
||||||
end
|
|
||||||
|
|
||||||
function test_dict_send()
|
|
||||||
small_dict = Dict("name" => "Alice", "age" => 30)
|
|
||||||
large_dict = Dict("ids" => collect(1:50000), "names" => ["User_$i" for i in 1:50000])
|
|
||||||
|
|
||||||
data1 = ("small_dict", small_dict, "dictionary")
|
|
||||||
data2 = ("large_dict", large_dict, "dictionary")
|
|
||||||
|
|
||||||
env = NATSBridge.smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
[data1, data2],
|
|
||||||
nats_url = NATS_URL,
|
|
||||||
fileserver_url = FILESERVER_URL,
|
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
|
||||||
size_threshold = 1_000_000,
|
|
||||||
correlation_id = string(uuid4()),
|
|
||||||
msg_purpose = "chat"
|
|
||||||
)
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
**Receiver:**
|
|
||||||
```julia
|
|
||||||
using NATSBridge
|
|
||||||
|
|
||||||
const SUBJECT = "/NATSBridge_dict_test"
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
|
|
||||||
function test_dict_receive()
|
|
||||||
conn = NATS.connect(NATS_URL)
|
|
||||||
NATS.subscribe(conn, SUBJECT) do msg
|
|
||||||
result = NATSBridge.smartreceive(
|
|
||||||
msg,
|
|
||||||
max_retries = 5,
|
|
||||||
base_delay = 100,
|
|
||||||
max_delay = 5000
|
|
||||||
)
|
|
||||||
|
|
||||||
for (dataname, data, data_type) in result
|
|
||||||
if data_type == "dictionary"
|
|
||||||
println("Received dictionary: $data")
|
|
||||||
write("./received_$dataname.json", JSON.json(data, 2))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
sleep(120)
|
|
||||||
NATS.drain(conn)
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
### Example 3: DataFrame (Table) Message
|
|
||||||
|
|
||||||
**Sender:**
|
|
||||||
```julia
|
|
||||||
using NATSBridge
|
|
||||||
using DataFrames
|
|
||||||
using UUIDs
|
|
||||||
|
|
||||||
const SUBJECT = "/NATSBridge_table_test"
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
const FILESERVER_URL = "http://localhost:8080"
|
|
||||||
|
|
||||||
function plik_upload_handler(fileserver_url::String, dataname::String, data::Vector{UInt8})::Dict{String, Any}
|
|
||||||
url_getUploadID = "$fileserver_url/upload"
|
|
||||||
headers = ["Content-Type" => "application/json"]
|
|
||||||
body = """{ "OneShot" : true }"""
|
|
||||||
httpResponse = HTTP.request("POST", url_getUploadID, headers, body; body_is_form=false)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
uploadid = responseJson["id"]
|
|
||||||
uploadtoken = responseJson["uploadToken"]
|
|
||||||
|
|
||||||
file_multipart = HTTP.Multipart(dataname, IOBuffer(data), "application/octet-stream")
|
|
||||||
url_upload = "$fileserver_url/file/$uploadid"
|
|
||||||
headers = ["X-UploadToken" => uploadtoken]
|
|
||||||
|
|
||||||
form = HTTP.Form(Dict("file" => file_multipart))
|
|
||||||
httpResponse = HTTP.post(url_upload, headers, form)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
|
|
||||||
fileid = responseJson["id"]
|
|
||||||
url = "$fileserver_url/file/$uploadid/$fileid/$dataname"
|
|
||||||
|
|
||||||
return Dict("status" => httpResponse.status, "uploadid" => uploadid, "fileid" => fileid, "url" => url)
|
|
||||||
end
|
|
||||||
|
|
||||||
function test_table_send()
|
|
||||||
small_df = DataFrame(id = 1:10, name = ["Alice", "Bob", "Charlie"], score = [95, 88, 92])
|
|
||||||
large_df = DataFrame(id = 1:50000, name = ["User_$i" for i in 1:50000], score = rand(1:100, 50000))
|
|
||||||
|
|
||||||
data1 = ("small_table", small_df, "table")
|
|
||||||
data2 = ("large_table", large_df, "table")
|
|
||||||
|
|
||||||
env = NATSBridge.smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
[data1, data2],
|
|
||||||
nats_url = NATS_URL,
|
|
||||||
fileserver_url = FILESERVER_URL,
|
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
|
||||||
size_threshold = 1_000_000,
|
|
||||||
correlation_id = string(uuid4()),
|
|
||||||
msg_purpose = "chat"
|
|
||||||
)
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
**Receiver:**
|
|
||||||
```julia
|
|
||||||
using NATSBridge
|
|
||||||
using DataFrames
|
|
||||||
|
|
||||||
const SUBJECT = "/NATSBridge_table_test"
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
|
|
||||||
function test_table_receive()
|
|
||||||
conn = NATS.connect(NATS_URL)
|
|
||||||
NATS.subscribe(conn, SUBJECT) do msg
|
|
||||||
result = NATSBridge.smartreceive(
|
|
||||||
msg,
|
|
||||||
max_retries = 5,
|
|
||||||
base_delay = 100,
|
|
||||||
max_delay = 5000
|
|
||||||
)
|
|
||||||
|
|
||||||
for (dataname, data, data_type) in result
|
|
||||||
if data_type == "table"
|
|
||||||
data = DataFrame(data)
|
|
||||||
println("Received DataFrame with $(size(data, 1)) rows")
|
|
||||||
display(data[1:min(5, size(data, 1)), :])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
sleep(120)
|
|
||||||
NATS.drain(conn)
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
### Example 4: Mixed Content (Chat with Text, Image, Audio)
|
|
||||||
|
|
||||||
**Sender:**
|
|
||||||
```julia
|
|
||||||
using NATSBridge
|
|
||||||
using DataFrames
|
|
||||||
using UUIDs
|
|
||||||
|
|
||||||
const SUBJECT = "/NATSBridge_mix_test"
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
const FILESERVER_URL = "http://localhost:8080"
|
|
||||||
|
|
||||||
function plik_upload_handler(fileserver_url::String, dataname::String, data::Vector{UInt8})::Dict{String, Any}
|
|
||||||
url_getUploadID = "$fileserver_url/upload"
|
|
||||||
headers = ["Content-Type" => "application/json"]
|
|
||||||
body = """{ "OneShot" : true }"""
|
|
||||||
httpResponse = HTTP.request("POST", url_getUploadID, headers, body; body_is_form=false)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
uploadid = responseJson["id"]
|
|
||||||
uploadtoken = responseJson["uploadToken"]
|
|
||||||
|
|
||||||
file_multipart = HTTP.Multipart(dataname, IOBuffer(data), "application/octet-stream")
|
|
||||||
url_upload = "$fileserver_url/file/$uploadid"
|
|
||||||
headers = ["X-UploadToken" => uploadtoken]
|
|
||||||
|
|
||||||
form = HTTP.Form(Dict("file" => file_multipart))
|
|
||||||
httpResponse = HTTP.post(url_upload, headers, form)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
|
|
||||||
fileid = responseJson["id"]
|
|
||||||
url = "$fileserver_url/file/$uploadid/$fileid/$dataname"
|
|
||||||
|
|
||||||
return Dict("status" => httpResponse.status, "uploadid" => uploadid, "fileid" => fileid, "url" => url)
|
|
||||||
end
|
|
||||||
|
|
||||||
function test_mix_send()
|
|
||||||
# Text data
|
|
||||||
text_data = "Hello! This is a test chat message. 🎉"
|
|
||||||
|
|
||||||
# Dictionary data
|
|
||||||
dict_data = Dict("type" => "chat", "sender" => "serviceA")
|
|
||||||
|
|
||||||
# Small table data
|
|
||||||
table_data_small = DataFrame(id = 1:10, name = ["msg_$i" for i in 1:10])
|
|
||||||
|
|
||||||
# Large table data (link transport)
|
|
||||||
table_data_large = DataFrame(id = 1:150_000, name = ["msg_$i" for i in 1:150_000])
|
|
||||||
|
|
||||||
# Small image data (direct transport)
|
|
||||||
image_data = UInt8[rand(1:255) for _ in 1:100]
|
|
||||||
|
|
||||||
# Large image data (link transport)
|
|
||||||
large_image_data = UInt8[rand(1:255) for _ in 1:1_500_000]
|
|
||||||
|
|
||||||
# Small audio data (direct transport)
|
|
||||||
audio_data = UInt8[rand(1:255) for _ in 1:100]
|
|
||||||
|
|
||||||
# Large audio data (link transport)
|
|
||||||
large_audio_data = UInt8[rand(1:255) for _ in 1:1_500_000]
|
|
||||||
|
|
||||||
# Small video data (direct transport)
|
|
||||||
video_data = UInt8[rand(1:255) for _ in 1:150]
|
|
||||||
|
|
||||||
# Large video data (link transport)
|
|
||||||
large_video_data = UInt8[rand(1:255) for _ in 1:1_500_000]
|
|
||||||
|
|
||||||
# Small binary data (direct transport)
|
|
||||||
binary_data = UInt8[rand(1:255) for _ in 1:200]
|
|
||||||
|
|
||||||
# Large binary data (link transport)
|
|
||||||
large_binary_data = UInt8[rand(1:255) for _ in 1:1_500_000]
|
|
||||||
|
|
||||||
# Create payloads list - mixed content
|
|
||||||
payloads = [
|
|
||||||
# Small data (direct transport)
|
|
||||||
("chat_text", text_data, "text"),
|
|
||||||
("chat_json", dict_data, "dictionary"),
|
|
||||||
("chat_table_small", table_data_small, "table"),
|
|
||||||
|
|
||||||
# Large data (link transport)
|
|
||||||
("chat_table_large", table_data_large, "table"),
|
|
||||||
("user_image_large", large_image_data, "image"),
|
|
||||||
("audio_clip_large", large_audio_data, "audio"),
|
|
||||||
("video_clip_large", large_video_data, "video"),
|
|
||||||
("binary_file_large", large_binary_data, "binary")
|
|
||||||
]
|
|
||||||
|
|
||||||
env = NATSBridge.smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
payloads,
|
|
||||||
nats_url = NATS_URL,
|
|
||||||
fileserver_url = FILESERVER_URL,
|
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
|
||||||
size_threshold = 1_000_000,
|
|
||||||
correlation_id = string(uuid4()),
|
|
||||||
msg_purpose = "chat",
|
|
||||||
sender_name = "mix_sender"
|
|
||||||
)
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
**Receiver:**
|
|
||||||
```julia
|
|
||||||
using NATSBridge
|
|
||||||
using DataFrames
|
|
||||||
|
|
||||||
const SUBJECT = "/NATSBridge_mix_test"
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
|
|
||||||
function test_mix_receive()
|
|
||||||
conn = NATS.connect(NATS_URL)
|
|
||||||
NATS.subscribe(conn, SUBJECT) do msg
|
|
||||||
result = NATSBridge.smartreceive(
|
|
||||||
msg,
|
|
||||||
max_retries = 5,
|
|
||||||
base_delay = 100,
|
|
||||||
max_delay = 5000
|
|
||||||
)
|
|
||||||
|
|
||||||
println("Received $(length(result)) payloads")
|
|
||||||
|
|
||||||
for (dataname, data, data_type) in result
|
|
||||||
println("\n=== Payload: $dataname (type: $data_type) ===")
|
|
||||||
|
|
||||||
if data_type == "text"
|
|
||||||
println(" Type: String")
|
|
||||||
println(" Length: $(length(data)) characters")
|
|
||||||
|
|
||||||
elseif data_type == "dictionary"
|
|
||||||
println(" Type: JSON Object")
|
|
||||||
println(" Keys: $(keys(data))")
|
|
||||||
|
|
||||||
elseif data_type == "table"
|
|
||||||
data = DataFrame(data)
|
|
||||||
println(" Type: DataFrame")
|
|
||||||
println(" Dimensions: $(size(data, 1)) rows x $(size(data, 2)) columns")
|
|
||||||
|
|
||||||
elseif data_type == "image"
|
|
||||||
println(" Type: Vector{UInt8}")
|
|
||||||
println(" Size: $(length(data)) bytes")
|
|
||||||
write("./received_$dataname.bin", data)
|
|
||||||
|
|
||||||
elseif data_type == "audio"
|
|
||||||
println(" Type: Vector{UInt8}")
|
|
||||||
println(" Size: $(length(data)) bytes")
|
|
||||||
write("./received_$dataname.bin", data)
|
|
||||||
|
|
||||||
elseif data_type == "video"
|
|
||||||
println(" Type: Vector{UInt8}")
|
|
||||||
println(" Size: $(length(data)) bytes")
|
|
||||||
write("./received_$dataname.bin", data)
|
|
||||||
|
|
||||||
elseif data_type == "binary"
|
|
||||||
println(" Type: Vector{UInt8}")
|
|
||||||
println(" Size: $(length(data)) bytes")
|
|
||||||
write("./received_$dataname.bin", data)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
sleep(120)
|
|
||||||
NATS.drain(conn)
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Best Practices
|
|
||||||
|
|
||||||
1. **Always wrap payloads in a list** - Even for single payloads: `[("dataname", data, "type")]`
|
|
||||||
2. **Use appropriate transport** - Let NATSBridge handle size-based routing (default 1MB threshold)
|
|
||||||
3. **Customize size threshold** - Use `size_threshold` parameter to adjust the direct/link split
|
|
||||||
4. **Provide fileserver handler** - Implement `fileserverUploadHandler` for link transport
|
|
||||||
5. **Include correlation IDs** - Track messages across distributed systems
|
|
||||||
6. **Handle errors** - Implement proper error handling for network failures
|
|
||||||
7. **Close connections** - Ensure NATS connections are properly closed using `NATS.drain()`
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Conclusion
|
|
||||||
|
|
||||||
NATSBridge.jl provides a powerful abstraction for bi-directional communication between Julia and JavaScript services. By understanding the key concepts and following the best practices, you can build robust, scalable applications that leverage the full power of NATS messaging.
|
|
||||||
|
|
||||||
For more information, see:
|
|
||||||
- [`docs/architecture.md`](./architecture.md) - Detailed architecture documentation
|
|
||||||
- [`docs/implementation.md`](./implementation.md) - Implementation details
|
|
||||||
@@ -1,939 +0,0 @@
|
|||||||
# NATSBridge.jl Walkthrough: Building a Chat System
|
|
||||||
|
|
||||||
A step-by-step guided walkthrough for building a real-time chat system using NATSBridge.jl with mixed content support (text, images, audio, video, and files).
|
|
||||||
|
|
||||||
## Prerequisites
|
|
||||||
|
|
||||||
- Julia 1.7+
|
|
||||||
- NATS server running
|
|
||||||
- HTTP file server (Plik) running
|
|
||||||
|
|
||||||
## Step 1: Understanding the Chat System Architecture
|
|
||||||
|
|
||||||
### System Components
|
|
||||||
|
|
||||||
```
|
|
||||||
┌─────────────────────────────────────────────────────────────────────────────┐
|
|
||||||
│ Chat System │
|
|
||||||
├─────────────────────────────────────────────────────────────────────────────┤
|
|
||||||
│ │
|
|
||||||
│ ┌──────────────┐ NATS ┌──────────────┐ │
|
|
||||||
│ │ Julia │◄───────┬───────► │ JavaScript │ │
|
|
||||||
│ │ Service │ │ │ Client │ │
|
|
||||||
│ │ │ │ │ │ │
|
|
||||||
│ │ - Text │ │ │ - Text │ │
|
|
||||||
│ │ - Images │ │ │ - Images │ │
|
|
||||||
│ │ - Audio │ ▼ │ - Audio │ │
|
|
||||||
│ │ - Video │ NATSBridge.jl │ - Files │ │
|
|
||||||
│ │ - Files │ │ │ - Tables │ │
|
|
||||||
│ └──────────────┘ │ └──────────────┘ │
|
|
||||||
│ │ │
|
|
||||||
│ ┌───────┴───────┐ │
|
|
||||||
│ │ NATS │ │
|
|
||||||
│ │ Server │ │
|
|
||||||
│ └─────────────┘ │
|
|
||||||
│ │
|
|
||||||
└─────────────────────────────────────────────────────────────────────────────┘
|
|
||||||
|
|
||||||
For large payloads (> 1MB):
|
|
||||||
┌─────────────────────────────────────────────────────────────────────────────┐
|
|
||||||
│ File Server (Plik) │
|
|
||||||
│ │
|
|
||||||
│ Julia Service ──► Upload ──► File Server ──► Download ◄── JavaScript Client│
|
|
||||||
│ │
|
|
||||||
└─────────────────────────────────────────────────────────────────────────────┘
|
|
||||||
```
|
|
||||||
|
|
||||||
### Message Format
|
|
||||||
|
|
||||||
Each chat message is an envelope containing multiple payloads:
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"correlationId": "uuid4",
|
|
||||||
"msgId": "uuid4",
|
|
||||||
"timestamp": "2024-01-15T10:30:00Z",
|
|
||||||
"sendTo": "/chat/room1",
|
|
||||||
"msgPurpose": "chat",
|
|
||||||
"senderName": "user-1",
|
|
||||||
"senderId": "uuid4",
|
|
||||||
"receiverName": "user-2",
|
|
||||||
"receiverId": "uuid4",
|
|
||||||
"brokerURL": "nats://localhost:4222",
|
|
||||||
"payloads": [
|
|
||||||
{
|
|
||||||
"id": "uuid4",
|
|
||||||
"dataname": "message_text",
|
|
||||||
"type": "text",
|
|
||||||
"transport": "direct",
|
|
||||||
"encoding": "base64",
|
|
||||||
"size": 256,
|
|
||||||
"data": "SGVsbG8gV29ybGQh",
|
|
||||||
"metadata": {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "uuid4",
|
|
||||||
"dataname": "user_image",
|
|
||||||
"type": "image",
|
|
||||||
"transport": "link",
|
|
||||||
"encoding": "none",
|
|
||||||
"size": 15433,
|
|
||||||
"data": "http://localhost:8080/file/UPLOAD_ID/FILE_ID/image.jpg",
|
|
||||||
"metadata": {}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 2: Setting Up the Environment
|
|
||||||
|
|
||||||
### 1. Start NATS Server
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Using Docker
|
|
||||||
docker run -d -p 4222:4222 -p 8222:8222 --name nats-server nats:latest
|
|
||||||
|
|
||||||
# Or download from https://github.com/nats-io/nats-server/releases
|
|
||||||
./nats-server
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2. Start HTTP File Server (Plik)
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Using Docker
|
|
||||||
docker run -d -p 8080:8080 --name plik plik/plik:latest
|
|
||||||
|
|
||||||
# Or download from https://github.com/arnaud-lb/plik/releases
|
|
||||||
./plikd -d
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3. Install Julia Dependencies
|
|
||||||
|
|
||||||
```julia
|
|
||||||
using Pkg
|
|
||||||
Pkg.add("NATS")
|
|
||||||
Pkg.add("JSON")
|
|
||||||
Pkg.add("Arrow")
|
|
||||||
Pkg.add("HTTP")
|
|
||||||
Pkg.add("UUIDs")
|
|
||||||
Pkg.add("Dates")
|
|
||||||
Pkg.add("Base64")
|
|
||||||
Pkg.add("PrettyPrinting")
|
|
||||||
Pkg.add("DataFrames")
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 3: Basic Text-Only Chat
|
|
||||||
|
|
||||||
### Sender (User 1)
|
|
||||||
|
|
||||||
```julia
|
|
||||||
using NATS
|
|
||||||
using JSON
|
|
||||||
using UUIDs
|
|
||||||
using Dates
|
|
||||||
using PrettyPrinting
|
|
||||||
using DataFrames
|
|
||||||
using Arrow
|
|
||||||
using HTTP
|
|
||||||
using Base64
|
|
||||||
|
|
||||||
# Include the bridge module
|
|
||||||
include("NATSBridge.jl")
|
|
||||||
using .NATSBridge
|
|
||||||
|
|
||||||
# Configuration
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
const FILESERVER_URL = "http://localhost:8080"
|
|
||||||
const SUBJECT = "/chat/room1"
|
|
||||||
|
|
||||||
# File upload handler for plik server
|
|
||||||
function plik_upload_handler(fileserver_url::String, dataname::String, data::Vector{UInt8})::Dict{String, Any}
|
|
||||||
url_getUploadID = "$fileserver_url/upload"
|
|
||||||
headers = ["Content-Type" => "application/json"]
|
|
||||||
body = """{ "OneShot" : true }"""
|
|
||||||
httpResponse = HTTP.request("POST", url_getUploadID, headers, body; body_is_form=false)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
uploadid = responseJson["id"]
|
|
||||||
uploadtoken = responseJson["uploadToken"]
|
|
||||||
|
|
||||||
file_multipart = HTTP.Multipart(dataname, IOBuffer(data), "application/octet-stream")
|
|
||||||
url_upload = "$fileserver_url/file/$uploadid"
|
|
||||||
headers = ["X-UploadToken" => uploadtoken]
|
|
||||||
|
|
||||||
form = HTTP.Form(Dict("file" => file_multipart))
|
|
||||||
httpResponse = HTTP.post(url_upload, headers, form)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
|
|
||||||
fileid = responseJson["id"]
|
|
||||||
url = "$fileserver_url/file/$uploadid/$fileid/$dataname"
|
|
||||||
|
|
||||||
return Dict("status" => httpResponse.status, "uploadid" => uploadid, "fileid" => fileid, "url" => url)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Send a simple text message
|
|
||||||
function send_text_message()
|
|
||||||
message_text = "Hello, how are you today?"
|
|
||||||
|
|
||||||
env = NATSBridge.smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
[("message", message_text, "text")],
|
|
||||||
nats_url = NATS_URL,
|
|
||||||
fileserver_url = FILESERVER_URL,
|
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
|
||||||
size_threshold = 1_000_000,
|
|
||||||
correlation_id = string(uuid4()),
|
|
||||||
msg_purpose = "chat",
|
|
||||||
sender_name = "user-1"
|
|
||||||
)
|
|
||||||
|
|
||||||
println("Sent text message with correlation ID: $(env.correlationId)")
|
|
||||||
end
|
|
||||||
|
|
||||||
send_text_message()
|
|
||||||
```
|
|
||||||
|
|
||||||
### Receiver (User 2)
|
|
||||||
|
|
||||||
```julia
|
|
||||||
using NATS
|
|
||||||
using JSON
|
|
||||||
using UUIDs
|
|
||||||
using Dates
|
|
||||||
using PrettyPrinting
|
|
||||||
using DataFrames
|
|
||||||
using Arrow
|
|
||||||
using HTTP
|
|
||||||
using Base64
|
|
||||||
|
|
||||||
# Include the bridge module
|
|
||||||
include("NATSBridge.jl")
|
|
||||||
using .NATSBridge
|
|
||||||
|
|
||||||
# Configuration
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
const SUBJECT = "/chat/room1"
|
|
||||||
|
|
||||||
# Message handler
|
|
||||||
function message_handler(msg::NATS.Msg)
|
|
||||||
payloads = NATSBridge.smartreceive(
|
|
||||||
msg,
|
|
||||||
max_retries = 5,
|
|
||||||
base_delay = 100,
|
|
||||||
max_delay = 5000
|
|
||||||
)
|
|
||||||
|
|
||||||
# Extract the text message
|
|
||||||
for (dataname, data, data_type) in payloads
|
|
||||||
if data_type == "text"
|
|
||||||
println("Received message: $data")
|
|
||||||
# Save to file
|
|
||||||
write("./received_$dataname.txt", data)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Subscribe to the chat room
|
|
||||||
NATS.subscribe(SUBJECT) do msg
|
|
||||||
message_handler(msg)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Keep the program running
|
|
||||||
while true
|
|
||||||
sleep(1)
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 4: Adding Image Support
|
|
||||||
|
|
||||||
### Sending an Image
|
|
||||||
|
|
||||||
```julia
|
|
||||||
using NATS
|
|
||||||
using JSON
|
|
||||||
using UUIDs
|
|
||||||
using Dates
|
|
||||||
using PrettyPrinting
|
|
||||||
using DataFrames
|
|
||||||
using Arrow
|
|
||||||
using HTTP
|
|
||||||
using Base64
|
|
||||||
|
|
||||||
include("NATSBridge.jl")
|
|
||||||
using .NATSBridge
|
|
||||||
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
const FILESERVER_URL = "http://localhost:8080"
|
|
||||||
const SUBJECT = "/chat/room1"
|
|
||||||
|
|
||||||
function plik_upload_handler(fileserver_url::String, dataname::String, data::Vector{UInt8})::Dict{String, Any}
|
|
||||||
url_getUploadID = "$fileserver_url/upload"
|
|
||||||
headers = ["Content-Type" => "application/json"]
|
|
||||||
body = """{ "OneShot" : true }"""
|
|
||||||
httpResponse = HTTP.request("POST", url_getUploadID, headers, body; body_is_form=false)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
uploadid = responseJson["id"]
|
|
||||||
uploadtoken = responseJson["uploadToken"]
|
|
||||||
|
|
||||||
file_multipart = HTTP.Multipart(dataname, IOBuffer(data), "application/octet-stream")
|
|
||||||
url_upload = "$fileserver_url/file/$uploadid"
|
|
||||||
headers = ["X-UploadToken" => uploadtoken]
|
|
||||||
|
|
||||||
form = HTTP.Form(Dict("file" => file_multipart))
|
|
||||||
httpResponse = HTTP.post(url_upload, headers, form)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
|
|
||||||
fileid = responseJson["id"]
|
|
||||||
url = "$fileserver_url/file/$uploadid/$fileid/$dataname"
|
|
||||||
|
|
||||||
return Dict("status" => httpResponse.status, "uploadid" => uploadid, "fileid" => fileid, "url" => url)
|
|
||||||
end
|
|
||||||
|
|
||||||
function send_image()
|
|
||||||
# Read image file
|
|
||||||
image_data = read("screenshot.png", Vector{UInt8})
|
|
||||||
|
|
||||||
# Send with text message
|
|
||||||
env = NATSBridge.smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
[
|
|
||||||
("text", "Check out this screenshot!", "text"),
|
|
||||||
("screenshot", image_data, "image")
|
|
||||||
],
|
|
||||||
nats_url = NATS_URL,
|
|
||||||
fileserver_url = FILESERVER_URL,
|
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
|
||||||
size_threshold = 1_000_000,
|
|
||||||
correlation_id = string(uuid4()),
|
|
||||||
msg_purpose = "chat",
|
|
||||||
sender_name = "user-1"
|
|
||||||
)
|
|
||||||
|
|
||||||
println("Sent image with correlation ID: $(env.correlationId)")
|
|
||||||
end
|
|
||||||
|
|
||||||
send_image()
|
|
||||||
```
|
|
||||||
|
|
||||||
### Receiving an Image
|
|
||||||
|
|
||||||
```julia
|
|
||||||
using NATS
|
|
||||||
using JSON
|
|
||||||
using UUIDs
|
|
||||||
using Dates
|
|
||||||
using PrettyPrinting
|
|
||||||
using DataFrames
|
|
||||||
using Arrow
|
|
||||||
using HTTP
|
|
||||||
using Base64
|
|
||||||
|
|
||||||
include("NATSBridge.jl")
|
|
||||||
using .NATSBridge
|
|
||||||
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
const SUBJECT = "/chat/room1"
|
|
||||||
|
|
||||||
function message_handler(msg::NATS.Msg)
|
|
||||||
payloads = NATSBridge.smartreceive(
|
|
||||||
msg,
|
|
||||||
max_retries = 5,
|
|
||||||
base_delay = 100,
|
|
||||||
max_delay = 5000
|
|
||||||
)
|
|
||||||
|
|
||||||
for (dataname, data, data_type) in payloads
|
|
||||||
if data_type == "text"
|
|
||||||
println("Text: $data")
|
|
||||||
elseif data_type == "image"
|
|
||||||
# Save image to file
|
|
||||||
filename = "received_$dataname.bin"
|
|
||||||
write(filename, data)
|
|
||||||
println("Saved image: $filename")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
NATS.subscribe(SUBJECT) do msg
|
|
||||||
message_handler(msg)
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 5: Handling Large Files with Link Transport
|
|
||||||
|
|
||||||
### Automatic Transport Selection
|
|
||||||
|
|
||||||
```julia
|
|
||||||
using NATS
|
|
||||||
using JSON
|
|
||||||
using UUIDs
|
|
||||||
using Dates
|
|
||||||
using PrettyPrinting
|
|
||||||
using DataFrames
|
|
||||||
using Arrow
|
|
||||||
using HTTP
|
|
||||||
using Base64
|
|
||||||
|
|
||||||
include("NATSBridge.jl")
|
|
||||||
using .NATSBridge
|
|
||||||
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
const FILESERVER_URL = "http://localhost:8080"
|
|
||||||
const SUBJECT = "/chat/room1"
|
|
||||||
|
|
||||||
function plik_upload_handler(fileserver_url::String, dataname::String, data::Vector{UInt8})::Dict{String, Any}
|
|
||||||
url_getUploadID = "$fileserver_url/upload"
|
|
||||||
headers = ["Content-Type" => "application/json"]
|
|
||||||
body = """{ "OneShot" : true }"""
|
|
||||||
httpResponse = HTTP.request("POST", url_getUploadID, headers, body; body_is_form=false)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
uploadid = responseJson["id"]
|
|
||||||
uploadtoken = responseJson["uploadToken"]
|
|
||||||
|
|
||||||
file_multipart = HTTP.Multipart(dataname, IOBuffer(data), "application/octet-stream")
|
|
||||||
url_upload = "$fileserver_url/file/$uploadid"
|
|
||||||
headers = ["X-UploadToken" => uploadtoken]
|
|
||||||
|
|
||||||
form = HTTP.Form(Dict("file" => file_multipart))
|
|
||||||
httpResponse = HTTP.post(url_upload, headers, form)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
|
|
||||||
fileid = responseJson["id"]
|
|
||||||
url = "$fileserver_url/file/$uploadid/$fileid/$dataname"
|
|
||||||
|
|
||||||
return Dict("status" => httpResponse.status, "uploadid" => uploadid, "fileid" => fileid, "url" => url)
|
|
||||||
end
|
|
||||||
|
|
||||||
function send_large_file()
|
|
||||||
# Create a large file (> 1MB triggers link transport)
|
|
||||||
large_data = rand(10_000_000) # ~80MB
|
|
||||||
|
|
||||||
env = NATSBridge.smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
[("large_file", large_data, "binary")],
|
|
||||||
nats_url = NATS_URL,
|
|
||||||
fileserver_url = FILESERVER_URL,
|
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
|
||||||
size_threshold = 1_000_000,
|
|
||||||
correlation_id = string(uuid4()),
|
|
||||||
msg_purpose = "chat",
|
|
||||||
sender_name = "user-1"
|
|
||||||
)
|
|
||||||
|
|
||||||
println("Uploaded large file to: $(env.payloads[1].data)")
|
|
||||||
println("Correlation ID: $(env.correlationId)")
|
|
||||||
end
|
|
||||||
|
|
||||||
send_large_file()
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 6: Audio and Video Support
|
|
||||||
|
|
||||||
### Sending Audio
|
|
||||||
|
|
||||||
```julia
|
|
||||||
using NATS
|
|
||||||
using JSON
|
|
||||||
using UUIDs
|
|
||||||
using Dates
|
|
||||||
using PrettyPrinting
|
|
||||||
using DataFrames
|
|
||||||
using Arrow
|
|
||||||
using HTTP
|
|
||||||
using Base64
|
|
||||||
|
|
||||||
include("NATSBridge.jl")
|
|
||||||
using .NATSBridge
|
|
||||||
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
const FILESERVER_URL = "http://localhost:8080"
|
|
||||||
const SUBJECT = "/chat/room1"
|
|
||||||
|
|
||||||
function plik_upload_handler(fileserver_url::String, dataname::String, data::Vector{UInt8})::Dict{String, Any}
|
|
||||||
url_getUploadID = "$fileserver_url/upload"
|
|
||||||
headers = ["Content-Type" => "application/json"]
|
|
||||||
body = """{ "OneShot" : true }"""
|
|
||||||
httpResponse = HTTP.request("POST", url_getUploadID, headers, body; body_is_form=false)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
uploadid = responseJson["id"]
|
|
||||||
uploadtoken = responseJson["uploadToken"]
|
|
||||||
|
|
||||||
file_multipart = HTTP.Multipart(dataname, IOBuffer(data), "application/octet-stream")
|
|
||||||
url_upload = "$fileserver_url/file/$uploadid"
|
|
||||||
headers = ["X-UploadToken" => uploadtoken]
|
|
||||||
|
|
||||||
form = HTTP.Form(Dict("file" => file_multipart))
|
|
||||||
httpResponse = HTTP.post(url_upload, headers, form)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
|
|
||||||
fileid = responseJson["id"]
|
|
||||||
url = "$fileserver_url/file/$uploadid/$fileid/$dataname"
|
|
||||||
|
|
||||||
return Dict("status" => httpResponse.status, "uploadid" => uploadid, "fileid" => fileid, "url" => url)
|
|
||||||
end
|
|
||||||
|
|
||||||
function send_audio()
|
|
||||||
# Read audio file (WAV, MP3, etc.)
|
|
||||||
audio_data = read("voice_message.mp3", Vector{UInt8})
|
|
||||||
|
|
||||||
env = NATSBridge.smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
[("voice_message", audio_data, "audio")],
|
|
||||||
nats_url = NATS_URL,
|
|
||||||
fileserver_url = FILESERVER_URL,
|
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
|
||||||
size_threshold = 1_000_000,
|
|
||||||
correlation_id = string(uuid4()),
|
|
||||||
msg_purpose = "chat",
|
|
||||||
sender_name = "user-1"
|
|
||||||
)
|
|
||||||
|
|
||||||
println("Sent audio message: $(env.correlationId)")
|
|
||||||
end
|
|
||||||
|
|
||||||
send_audio()
|
|
||||||
```
|
|
||||||
|
|
||||||
### Sending Video
|
|
||||||
|
|
||||||
```julia
|
|
||||||
using NATS
|
|
||||||
using JSON
|
|
||||||
using UUIDs
|
|
||||||
using Dates
|
|
||||||
using PrettyPrinting
|
|
||||||
using DataFrames
|
|
||||||
using Arrow
|
|
||||||
using HTTP
|
|
||||||
using Base64
|
|
||||||
|
|
||||||
include("NATSBridge.jl")
|
|
||||||
using .NATSBridge
|
|
||||||
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
const FILESERVER_URL = "http://localhost:8080"
|
|
||||||
const SUBJECT = "/chat/room1"
|
|
||||||
|
|
||||||
function plik_upload_handler(fileserver_url::String, dataname::String, data::Vector{UInt8})::Dict{String, Any}
|
|
||||||
url_getUploadID = "$fileserver_url/upload"
|
|
||||||
headers = ["Content-Type" => "application/json"]
|
|
||||||
body = """{ "OneShot" : true }"""
|
|
||||||
httpResponse = HTTP.request("POST", url_getUploadID, headers, body; body_is_form=false)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
uploadid = responseJson["id"]
|
|
||||||
uploadtoken = responseJson["uploadToken"]
|
|
||||||
|
|
||||||
file_multipart = HTTP.Multipart(dataname, IOBuffer(data), "application/octet-stream")
|
|
||||||
url_upload = "$fileserver_url/file/$uploadid"
|
|
||||||
headers = ["X-UploadToken" => uploadtoken]
|
|
||||||
|
|
||||||
form = HTTP.Form(Dict("file" => file_multipart))
|
|
||||||
httpResponse = HTTP.post(url_upload, headers, form)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
|
|
||||||
fileid = responseJson["id"]
|
|
||||||
url = "$fileserver_url/file/$uploadid/$fileid/$dataname"
|
|
||||||
|
|
||||||
return Dict("status" => httpResponse.status, "uploadid" => uploadid, "fileid" => fileid, "url" => url)
|
|
||||||
end
|
|
||||||
|
|
||||||
function send_video()
|
|
||||||
# Read video file (MP4, AVI, etc.)
|
|
||||||
video_data = read("video_message.mp4", Vector{UInt8})
|
|
||||||
|
|
||||||
env = NATSBridge.smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
[("video_message", video_data, "video")],
|
|
||||||
nats_url = NATS_URL,
|
|
||||||
fileserver_url = FILESERVER_URL,
|
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
|
||||||
size_threshold = 1_000_000,
|
|
||||||
correlation_id = string(uuid4()),
|
|
||||||
msg_purpose = "chat",
|
|
||||||
sender_name = "user-1"
|
|
||||||
)
|
|
||||||
|
|
||||||
println("Sent video message: $(env.correlationId)")
|
|
||||||
end
|
|
||||||
|
|
||||||
send_video()
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 7: Table/Data Exchange
|
|
||||||
|
|
||||||
### Sending Tabular Data
|
|
||||||
|
|
||||||
```julia
|
|
||||||
using NATS
|
|
||||||
using JSON
|
|
||||||
using UUIDs
|
|
||||||
using Dates
|
|
||||||
using PrettyPrinting
|
|
||||||
using DataFrames
|
|
||||||
using Arrow
|
|
||||||
using HTTP
|
|
||||||
using Base64
|
|
||||||
|
|
||||||
include("NATSBridge.jl")
|
|
||||||
using .NATSBridge
|
|
||||||
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
const FILESERVER_URL = "http://localhost:8080"
|
|
||||||
const SUBJECT = "/chat/room1"
|
|
||||||
|
|
||||||
function plik_upload_handler(fileserver_url::String, dataname::String, data::Vector{UInt8})::Dict{String, Any}
|
|
||||||
url_getUploadID = "$fileserver_url/upload"
|
|
||||||
headers = ["Content-Type" => "application/json"]
|
|
||||||
body = """{ "OneShot" : true }"""
|
|
||||||
httpResponse = HTTP.request("POST", url_getUploadID, headers, body; body_is_form=false)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
uploadid = responseJson["id"]
|
|
||||||
uploadtoken = responseJson["uploadToken"]
|
|
||||||
|
|
||||||
file_multipart = HTTP.Multipart(dataname, IOBuffer(data), "application/octet-stream")
|
|
||||||
url_upload = "$fileserver_url/file/$uploadid"
|
|
||||||
headers = ["X-UploadToken" => uploadtoken]
|
|
||||||
|
|
||||||
form = HTTP.Form(Dict("file" => file_multipart))
|
|
||||||
httpResponse = HTTP.post(url_upload, headers, form)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
|
|
||||||
fileid = responseJson["id"]
|
|
||||||
url = "$fileserver_url/file/$uploadid/$fileid/$dataname"
|
|
||||||
|
|
||||||
return Dict("status" => httpResponse.status, "uploadid" => uploadid, "fileid" => fileid, "url" => url)
|
|
||||||
end
|
|
||||||
|
|
||||||
function send_table()
|
|
||||||
# Create a DataFrame
|
|
||||||
df = DataFrame(
|
|
||||||
id = 1:5,
|
|
||||||
name = ["Alice", "Bob", "Charlie", "Diana", "Eve"],
|
|
||||||
score = [95, 88, 92, 98, 85],
|
|
||||||
grade = ['A', 'B', 'A', 'B', 'B']
|
|
||||||
)
|
|
||||||
|
|
||||||
env = NATSBridge.smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
[("student_scores", df, "table")],
|
|
||||||
nats_url = NATS_URL,
|
|
||||||
fileserver_url = FILESERVER_URL,
|
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
|
||||||
size_threshold = 1_000_000,
|
|
||||||
correlation_id = string(uuid4()),
|
|
||||||
msg_purpose = "chat",
|
|
||||||
sender_name = "user-1"
|
|
||||||
)
|
|
||||||
|
|
||||||
println("Sent table with $(nrow(df)) rows")
|
|
||||||
end
|
|
||||||
|
|
||||||
send_table()
|
|
||||||
```
|
|
||||||
|
|
||||||
### Receiving and Using Tables
|
|
||||||
|
|
||||||
```julia
|
|
||||||
using NATS
|
|
||||||
using JSON
|
|
||||||
using UUIDs
|
|
||||||
using Dates
|
|
||||||
using PrettyPrinting
|
|
||||||
using DataFrames
|
|
||||||
using Arrow
|
|
||||||
using HTTP
|
|
||||||
using Base64
|
|
||||||
|
|
||||||
include("NATSBridge.jl")
|
|
||||||
using .NATSBridge
|
|
||||||
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
const SUBJECT = "/chat/room1"
|
|
||||||
|
|
||||||
function message_handler(msg::NATS.Msg)
|
|
||||||
payloads = NATSBridge.smartreceive(
|
|
||||||
msg,
|
|
||||||
max_retries = 5,
|
|
||||||
base_delay = 100,
|
|
||||||
max_delay = 5000
|
|
||||||
)
|
|
||||||
|
|
||||||
for (dataname, data, data_type) in payloads
|
|
||||||
if data_type == "table"
|
|
||||||
data = DataFrame(data)
|
|
||||||
println("Received table:")
|
|
||||||
show(data)
|
|
||||||
println("\nAverage score: $(mean(data.score))")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
NATS.subscribe(SUBJECT) do msg
|
|
||||||
message_handler(msg)
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 8: Bidirectional Communication
|
|
||||||
|
|
||||||
### Request-Response Pattern
|
|
||||||
|
|
||||||
```julia
|
|
||||||
using NATS
|
|
||||||
using JSON
|
|
||||||
using UUIDs
|
|
||||||
using Dates
|
|
||||||
using PrettyPrinting
|
|
||||||
using DataFrames
|
|
||||||
using Arrow
|
|
||||||
using HTTP
|
|
||||||
using Base64
|
|
||||||
|
|
||||||
include("NATSBridge.jl")
|
|
||||||
using .NATSBridge
|
|
||||||
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
const SUBJECT = "/api/query"
|
|
||||||
const REPLY_SUBJECT = "/api/response"
|
|
||||||
|
|
||||||
# Request
|
|
||||||
function send_request()
|
|
||||||
query_data = Dict("query" => "SELECT * FROM users")
|
|
||||||
|
|
||||||
env = NATSBridge.smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
[("sql_query", query_data, "dictionary")],
|
|
||||||
nats_url = NATS_URL,
|
|
||||||
fileserver_url = "http://localhost:8080",
|
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
|
||||||
size_threshold = 1_000_000,
|
|
||||||
correlation_id = string(uuid4()),
|
|
||||||
msg_purpose = "request",
|
|
||||||
sender_name = "frontend",
|
|
||||||
receiver_name = "backend",
|
|
||||||
reply_to = REPLY_SUBJECT,
|
|
||||||
reply_to_msg_id = string(uuid4())
|
|
||||||
)
|
|
||||||
|
|
||||||
println("Request sent: $(env.correlationId)")
|
|
||||||
end
|
|
||||||
|
|
||||||
# Response handler
|
|
||||||
function response_handler(msg::NATS.Msg)
|
|
||||||
payloads = NATSBridge.smartreceive(
|
|
||||||
msg,
|
|
||||||
max_retries = 5,
|
|
||||||
base_delay = 100,
|
|
||||||
max_delay = 5000
|
|
||||||
)
|
|
||||||
|
|
||||||
for (dataname, data, data_type) in payloads
|
|
||||||
if data_type == "table"
|
|
||||||
data = DataFrame(data)
|
|
||||||
println("Query results:")
|
|
||||||
show(data)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
NATS.subscribe(REPLY_SUBJECT) do msg
|
|
||||||
response_handler(msg)
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 9: Complete Chat Application
|
|
||||||
|
|
||||||
### Full Chat System
|
|
||||||
|
|
||||||
```julia
|
|
||||||
module ChatApp
|
|
||||||
using NATS
|
|
||||||
using JSON
|
|
||||||
using UUIDs
|
|
||||||
using Dates
|
|
||||||
using PrettyPrinting
|
|
||||||
using DataFrames
|
|
||||||
using Arrow
|
|
||||||
using HTTP
|
|
||||||
using Base64
|
|
||||||
|
|
||||||
# Include the bridge module
|
|
||||||
include("../src/NATSBridge.jl")
|
|
||||||
using .NATSBridge
|
|
||||||
|
|
||||||
# Configuration
|
|
||||||
const NATS_URL = "nats://localhost:4222"
|
|
||||||
const FILESERVER_URL = "http://localhost:8080"
|
|
||||||
const SUBJECT = "/chat/room1"
|
|
||||||
|
|
||||||
# File upload handler for plik server
|
|
||||||
function plik_upload_handler(fileserver_url::String, dataname::String, data::Vector{UInt8})::Dict{String, Any}
|
|
||||||
url_getUploadID = "$fileserver_url/upload"
|
|
||||||
headers = ["Content-Type" => "application/json"]
|
|
||||||
body = """{ "OneShot" : true }"""
|
|
||||||
httpResponse = HTTP.request("POST", url_getUploadID, headers, body; body_is_form=false)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
uploadid = responseJson["id"]
|
|
||||||
uploadtoken = responseJson["uploadToken"]
|
|
||||||
|
|
||||||
file_multipart = HTTP.Multipart(dataname, IOBuffer(data), "application/octet-stream")
|
|
||||||
url_upload = "$fileserver_url/file/$uploadid"
|
|
||||||
headers = ["X-UploadToken" => uploadtoken]
|
|
||||||
|
|
||||||
form = HTTP.Form(Dict("file" => file_multipart))
|
|
||||||
httpResponse = HTTP.post(url_upload, headers, form)
|
|
||||||
responseJson = JSON.parse(String(httpResponse.body))
|
|
||||||
|
|
||||||
fileid = responseJson["id"]
|
|
||||||
url = "$fileserver_url/file/$uploadid/$fileid/$dataname"
|
|
||||||
|
|
||||||
return Dict("status" => httpResponse.status, "uploadid" => uploadid, "fileid" => fileid, "url" => url)
|
|
||||||
end
|
|
||||||
|
|
||||||
function send_chat_message(
|
|
||||||
text::String,
|
|
||||||
image_path::Union{String, Nothing}=nothing,
|
|
||||||
audio_path::Union{String, Nothing}=nothing
|
|
||||||
)
|
|
||||||
# Build payloads list
|
|
||||||
payloads = [("message_text", text, "text")]
|
|
||||||
|
|
||||||
if image_path !== nothing
|
|
||||||
image_data = read(image_path, Vector{UInt8})
|
|
||||||
push!(payloads, ("user_image", image_data, "image"))
|
|
||||||
end
|
|
||||||
|
|
||||||
if audio_path !== nothing
|
|
||||||
audio_data = read(audio_path, Vector{UInt8})
|
|
||||||
push!(payloads, ("user_audio", audio_data, "audio"))
|
|
||||||
end
|
|
||||||
|
|
||||||
env = NATSBridge.smartsend(
|
|
||||||
SUBJECT,
|
|
||||||
payloads,
|
|
||||||
nats_url = NATS_URL,
|
|
||||||
fileserver_url = FILESERVER_URL,
|
|
||||||
fileserverUploadHandler = plik_upload_handler,
|
|
||||||
size_threshold = 1_000_000,
|
|
||||||
correlation_id = string(uuid4()),
|
|
||||||
msg_purpose = "chat",
|
|
||||||
sender_name = "user-1"
|
|
||||||
)
|
|
||||||
|
|
||||||
println("Message sent with correlation ID: $(env.correlationId)")
|
|
||||||
end
|
|
||||||
|
|
||||||
function receive_chat_messages()
|
|
||||||
function message_handler(msg::NATS.Msg)
|
|
||||||
payloads = NATSBridge.smartreceive(
|
|
||||||
msg,
|
|
||||||
max_retries = 5,
|
|
||||||
base_delay = 100,
|
|
||||||
max_delay = 5000
|
|
||||||
)
|
|
||||||
|
|
||||||
println("\n--- New Message ---")
|
|
||||||
for (dataname, data, data_type) in payloads
|
|
||||||
if data_type == "text"
|
|
||||||
println("Text: $data")
|
|
||||||
elseif data_type == "image"
|
|
||||||
filename = "received_$dataname.bin"
|
|
||||||
write(filename, data)
|
|
||||||
println("Image saved: $filename")
|
|
||||||
elseif data_type == "audio"
|
|
||||||
filename = "received_$dataname.bin"
|
|
||||||
write(filename, data)
|
|
||||||
println("Audio saved: $filename")
|
|
||||||
elseif data_type == "table"
|
|
||||||
println("Table received:")
|
|
||||||
data = DataFrame(data)
|
|
||||||
show(data)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
NATS.subscribe(SUBJECT) do msg
|
|
||||||
message_handler(msg)
|
|
||||||
end
|
|
||||||
println("Subscribed to: $SUBJECT")
|
|
||||||
end
|
|
||||||
|
|
||||||
function run_interactive_chat()
|
|
||||||
println("\n=== Interactive Chat ===")
|
|
||||||
println("1. Send a message")
|
|
||||||
println("2. Join a chat room")
|
|
||||||
println("3. Exit")
|
|
||||||
|
|
||||||
while true
|
|
||||||
print("\nSelect option (1-3): ")
|
|
||||||
choice = readline()
|
|
||||||
|
|
||||||
if choice == "1"
|
|
||||||
print("Enter message text: ")
|
|
||||||
text = readline()
|
|
||||||
send_chat_message(text)
|
|
||||||
elseif choice == "2"
|
|
||||||
receive_chat_messages()
|
|
||||||
elseif choice == "3"
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end # module
|
|
||||||
|
|
||||||
# Run the chat app
|
|
||||||
using .ChatApp
|
|
||||||
ChatApp.run_interactive_chat()
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 10: Testing the Chat System
|
|
||||||
|
|
||||||
### Test Scenario 1: Text-Only Chat
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Terminal 1: Start the chat receiver
|
|
||||||
julia test_julia_to_julia_text_receiver.jl
|
|
||||||
|
|
||||||
# Terminal 2: Send a message
|
|
||||||
julia test_julia_to_julia_text_sender.jl
|
|
||||||
```
|
|
||||||
|
|
||||||
### Test Scenario 2: Image Chat
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Terminal 1: Receive messages
|
|
||||||
julia test_julia_to_julia_mix_payloads_receiver.jl
|
|
||||||
|
|
||||||
# Terminal 2: Send image
|
|
||||||
julia test_julia_to_julia_mix_payload_sender.jl
|
|
||||||
```
|
|
||||||
|
|
||||||
### Test Scenario 3: Large File Transfer
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Terminal 2: Send large file
|
|
||||||
julia test_julia_to_julia_mix_payload_sender.jl
|
|
||||||
```
|
|
||||||
|
|
||||||
## Conclusion
|
|
||||||
|
|
||||||
This walkthrough demonstrated how to build a chat system using NATSBridge.jl with support for:
|
|
||||||
|
|
||||||
- Text messages
|
|
||||||
- Images (direct transport for small, link transport for large)
|
|
||||||
- Audio files
|
|
||||||
- Video files
|
|
||||||
- Tabular data (DataFrames)
|
|
||||||
- Bidirectional communication
|
|
||||||
- Mixed-content messages
|
|
||||||
|
|
||||||
The key takeaways are:
|
|
||||||
|
|
||||||
1. **Always wrap payloads in a list** - Even for single payloads: `[("dataname", data, "type")]`
|
|
||||||
2. **Use appropriate transport** - NATSBridge automatically handles size-based routing
|
|
||||||
3. **Support mixed content** - Multiple payloads of different types in one message
|
|
||||||
4. **Handle errors** - Implement proper error handling for network failures
|
|
||||||
5. **Use correlation IDs** - Track messages across distributed systems
|
|
||||||
|
|
||||||
For more information, see:
|
|
||||||
- [`docs/architecture.md`](./docs/architecture.md) - Detailed architecture documentation
|
|
||||||
- [`docs/implementation.md`](./docs/implementation.md) - Implementation details
|
|
||||||
Reference in New Issue
Block a user