This commit is contained in:
2026-02-19 19:25:34 +07:00
parent 7dc7ab67e4
commit 3f45052193
13 changed files with 2177 additions and 29 deletions

View File

@@ -71,13 +71,14 @@ The Julia implementation provides:
- **[`SmartSend()`](../src/julia_bridge.jl)**: Handles transport selection based on payload size
- **[`SmartReceive()`](../src/julia_bridge.jl)**: Handles both direct and link transport
### JavaScript Module: [`src/js_bridge.js`](../src/js_bridge.js)
### JavaScript Module: [`src/NATSBridge.js`](../src/NATSBridge.js)
The JavaScript implementation provides:
- **`MessageEnvelope` class**: For the unified JSON envelope
- **[`SmartSend()`](../src/js_bridge.js)**: Handles transport selection based on payload size
- **[`SmartReceive()`](../src/js_bridge.js)**: Handles both direct and link transport
- **`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
@@ -167,16 +168,16 @@ payloads = smartreceive(msg, "http://localhost:8080")
#### JavaScript (Sender)
```javascript
const { SmartSend } = require('./js_bridge');
const { smartsend } = require('./src/NATSBridge');
// Single payload wrapped in a list
const config = [{
dataname: "config",
data: { step_size: 0.01, iterations: 1000 },
type: "json"
type: "dictionary"
}];
await SmartSend("control", config, "json", {
await smartsend("control", config, {
correlationId: "unique-id"
});
@@ -185,16 +186,16 @@ const configs = [
{
dataname: "config1",
data: { step_size: 0.01 },
type: "json"
type: "dictionary"
},
{
dataname: "config2",
data: { iterations: 1000 },
type: "json"
type: "dictionary"
}
];
await SmartSend("control", configs, "json");
await smartsend("control", configs);
```
#### Julia (Receiver)
@@ -217,6 +218,25 @@ subscribe(nats, "control") do msg
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)
#### Julia (Sender)
@@ -237,28 +257,29 @@ await SmartSend("analysis_results", [("table_data", df, "table")]);
#### JavaScript (Receiver)
```javascript
const { SmartReceive } = require('./js_bridge');
const { smartreceive } = require('./src/NATSBridge');
const result = await SmartReceive(msg);
const result = await smartreceive(msg);
// Use table data for visualization with Perspective.js or D3
const table = result.data;
// Note: Tables are sent as arrays of objects in JavaScript
const table = result;
```
### Scenario 3: Live Binary Processing
#### JavaScript (Sender)
```javascript
const { SmartSend } = require('./js_bridge');
const { smartsend } = require('./src/NATSBridge');
// Binary data wrapped in a list
const binaryData = [{
dataname: "audio_chunk",
data: binaryBuffer,
data: binaryBuffer, // ArrayBuffer or Uint8Array
type: "binary"
}];
await SmartSend("binary_input", binaryData, "binary", {
await smartsend("binary_input", binaryData, {
metadata: {
sample_rate: 44100,
channels: 1
@@ -282,6 +303,25 @@ function process_binary(data)
end
```
### JavaScript (Receiver)
```javascript
const { smartreceive } = require('./src/NATSBridge');
// Receive binary data
function process_binary(msg) {
const result = await smartreceive(msg);
// Process the binary data
for (const { dataname, data, type } of result) {
if (type === "binary") {
// data is an ArrayBuffer or Uint8Array
console.log(`Received binary data: ${dataname}, size: ${data.length}`);
// Perform FFT or AI transcription here
}
}
}
```
### Scenario 4: Catch-Up (JetStream)
#### Julia (Producer)
@@ -299,6 +339,7 @@ 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();
@@ -312,9 +353,9 @@ const consumer = await js.pullSubscribe("health", {
// Process historical and real-time messages
for await (const msg of consumer) {
const result = await SmartReceive(msg);
// result.data contains the list of payloads
// result.envelope contains the message envelope
const result = await smartreceive(msg);
// result contains the list of payloads
// Each payload has: dataname, data, type
msg.ack();
}
```
@@ -351,22 +392,21 @@ smartsend(
**JavaScript (Receiver):**
```javascript
const { SmartReceive } = require('./js_bridge');
const { smartreceive, smartsend } = require('./src/NATSBridge');
// Receive NATS message with direct transport
const result = await SmartReceive(msg);
const result = await smartreceive(msg);
// Decode Base64 payload
// Parse Arrow IPC with zero-copy
// Load into selection UI component (e.g., dropdown, table)
const table = result[2]; // Get the DataFrame from the tuple
// 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", [
("selected_option", selection, "dictionary")
await smartsend("dashboard.response", [
{ dataname: "selected_option", data: selection, type: "dictionary" }
]);
```
@@ -416,7 +456,7 @@ smartsend(
**JavaScript (Sender/Receiver):**
```javascript
const { SmartSend, SmartReceive } = require('./js_bridge');
const { smartsend, smartreceive } = require('./src/NATSBridge');
// Build chat message with mixed content:
// - User input text: direct transport
@@ -441,7 +481,7 @@ const message = [
},
{
dataname: "image",
data: selectedImageBuffer, // Small image
data: selectedImageBuffer, // Small image (ArrayBuffer or Uint8Array)
type: "image"
},
{
@@ -451,7 +491,7 @@ const message = [
}
];
await SmartSend("chat.room123", message);
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.