This commit is contained in:
2026-02-23 21:43:09 +07:00
parent 0fa6eaf95b
commit 99bf57b154

View File

@@ -173,7 +173,7 @@ from nats_bridge import smartsend
# Send a text message # Send a text message
data = [("message", "Hello World", "text")] data = [("message", "Hello World", "text")]
env, msg_json_str = smartsend("/chat/room1", data, nats_url="nats://localhost:4222") envelope, msg_json_str = smartsend("/chat/room1", data, nats_url="nats://localhost:4222")
print("Message sent!") print("Message sent!")
``` ```
@@ -183,7 +183,7 @@ print("Message sent!")
const { smartsend } = require('./src/NATSBridge'); const { smartsend } = require('./src/NATSBridge');
// Send a text message // Send a text message
const { env, msg_json_str } = await smartsend("/chat/room1", [ const { envelope, msg_json_str } = await smartsend("/chat/room1", [
{ dataname: "message", data: "Hello World", type: "text" } { dataname: "message", data: "Hello World", type: "text" }
], { natsUrl: "nats://localhost:4222" }); ], { natsUrl: "nats://localhost:4222" });
@@ -197,7 +197,7 @@ using NATSBridge
# Send a text message # Send a text message
data = [("message", "Hello World", "text")] data = [("message", "Hello World", "text")]
env, msg_json_str = NATSBridge.smartsend("/chat/room1", data; nats_url="nats://localhost:4222") envelope, msg_json_str = NATSBridge.smartsend("/chat/room1", data; nats_url="nats://localhost:4222")
println("Message sent!") println("Message sent!")
``` ```
@@ -283,7 +283,7 @@ function test_receive()
log_trace("Received message on $(msg.subject)") log_trace("Received message on $(msg.subject)")
# Receive and process message # Receive and process message
envelope = NATSBridge.smartreceive(msg, fileserverDownloadHandler) envelope, msg_json_str = NATSBridge.smartreceive(msg, fileserverDownloadHandler)
for (dataname, data, type) in envelope["payloads"] for (dataname, data, type) in envelope["payloads"]
println("Received $dataname: $data") println("Received $dataname: $data")
end end
@@ -310,7 +310,7 @@ Sends data either directly via NATS or via a fileserver URL, depending on payloa
```python ```python
from nats_bridge import smartsend from nats_bridge import smartsend
env, msg_json_str = smartsend( envelope, msg_json_str = smartsend(
subject, # NATS subject to publish to subject, # NATS subject to publish to
data, # List of (dataname, data, type) tuples data, # List of (dataname, data, type) tuples
nats_url="nats://localhost:4222", # NATS server URL nats_url="nats://localhost:4222", # NATS server URL
@@ -333,7 +333,7 @@ env, msg_json_str = smartsend(
```javascript ```javascript
const { smartsend } = require('./src/NATSBridge'); const { smartsend } = require('./src/NATSBridge');
const { env, msg_json_str } = await smartsend( const { envelope, msg_json_str } = await smartsend(
subject, // NATS subject subject, // NATS subject
data, // Array of {dataname, data, type} data, // Array of {dataname, data, type}
{ {
@@ -358,7 +358,7 @@ const { env, msg_json_str } = await smartsend(
```julia ```julia
using NATSBridge using NATSBridge
env, msg_json_str = NATSBridge.smartsend( envelope, msg_json_str = NATSBridge.smartsend(
subject, # NATS subject subject, # NATS subject
data::AbstractArray{Tuple{String, Any, String}}; # List of (dataname, data, type) data::AbstractArray{Tuple{String, Any, String}}; # List of (dataname, data, type)
nats_url::String = "nats://localhost:4222", nats_url::String = "nats://localhost:4222",
@@ -375,7 +375,7 @@ env, msg_json_str = NATSBridge.smartsend(
is_publish::Bool = true # Whether to automatically publish to NATS is_publish::Bool = true # Whether to automatically publish to NATS
) )
# Returns: (msgEnvelope_v1, JSON string) # Returns: (msgEnvelope_v1, JSON string)
# - env: msgEnvelope_v1 object with all envelope metadata and payloads # - envelope: msgEnvelope_v1 object with all envelope metadata and payloads
# - msg_json_str: JSON string representation of the envelope for publishing # - msg_json_str: JSON string representation of the envelope for publishing
``` ```
@@ -423,7 +423,7 @@ const envelope = await smartreceive(
using NATSBridge using NATSBridge
# Note: msg is a NATS.Msg object passed from the subscription callback # Note: msg is a NATS.Msg object passed from the subscription callback
envelope = NATSBridge.smartreceive( envelope, msg_json_str = NATSBridge.smartreceive(
msg::NATS.Msg; msg::NATS.Msg;
fileserverDownloadHandler::Function = _fetch_with_backoff, fileserverDownloadHandler::Function = _fetch_with_backoff,
max_retries::Int = 5, max_retries::Int = 5,
@@ -517,14 +517,14 @@ data = [
("large_document", large_file_data, "binary") ("large_document", large_file_data, "binary")
] ]
env, msg_json_str = smartsend("/chat/room1", data, fileserver_url="http://localhost:8080") envelope, msg_json_str = smartsend("/chat/room1", data, fileserver_url="http://localhost:8080")
``` ```
#### JavaScript #### JavaScript
```javascript ```javascript
const { smartsend } = require('./src/NATSBridge'); const { smartsend } = require('./src/NATSBridge');
const { env, msg_json_str } = await smartsend("/chat/room1", [ const { envelope, msg_json_str } = await smartsend("/chat/room1", [
{ dataname: "message_text", data: "Hello!", type: "text" }, { dataname: "message_text", data: "Hello!", type: "text" },
{ dataname: "user_avatar", data: image_data, type: "image" }, { dataname: "user_avatar", data: image_data, type: "image" },
{ dataname: "large_document", data: large_file_data, type: "binary" } { dataname: "large_document", data: large_file_data, type: "binary" }
@@ -543,7 +543,7 @@ data = [
("large_document", large_file_data, "binary") ("large_document", large_file_data, "binary")
] ]
env, msg_json_str = NATSBridge.smartsend("/chat/room1", data; fileserver_url="http://localhost:8080") envelope, msg_json_str = NATSBridge.smartsend("/chat/room1", data; fileserver_url="http://localhost:8080")
``` ```
### Example 2: Dictionary Exchange ### Example 2: Dictionary Exchange
@@ -561,7 +561,7 @@ config = {
} }
data = [("config", config, "dictionary")] data = [("config", config, "dictionary")]
env, msg_json_str = smartsend("/device/config", data) envelope, msg_json_str = smartsend("/device/config", data)
``` ```
#### JavaScript #### JavaScript
@@ -574,7 +574,7 @@ const config = {
update_interval: 60 update_interval: 60
}; };
const { env, msg_json_str } = await smartsend("/device/config", [ const { envelope, msg_json_str } = await smartsend("/device/config", [
{ dataname: "config", data: config, type: "dictionary" } { dataname: "config", data: config, type: "dictionary" }
]); ]);
``` ```
@@ -590,7 +590,7 @@ config = Dict(
) )
data = [("config", config, "dictionary")] data = [("config", config, "dictionary")]
env, msg_json_str = NATSBridge.smartsend("/device/config", data) envelope, msg_json_str = NATSBridge.smartsend("/device/config", data)
``` ```
### Example 3: Table Data (Arrow IPC) ### Example 3: Table Data (Arrow IPC)
@@ -609,7 +609,7 @@ df = pd.DataFrame({
}) })
data = [("students", df, "table")] data = [("students", df, "table")]
env, msg_json_str = smartsend("/data/analysis", data) envelope, msg_json_str = smartsend("/data/analysis", data)
``` ```
#### JavaScript #### JavaScript
@@ -622,7 +622,7 @@ const tableData = [
{ id: 3, name: "Charlie", score: 92 } { id: 3, name: "Charlie", score: 92 }
]; ];
const { env, msg_json_str } = await smartsend("/data/analysis", [ const { envelope, msg_json_str } = await smartsend("/data/analysis", [
{ dataname: "students", data: tableData, type: "table" } { dataname: "students", data: tableData, type: "table" }
]); ]);
``` ```
@@ -639,7 +639,7 @@ df = DataFrame(
) )
data = [("students", df, "table")] data = [("students", df, "table")]
env, msg_json_str = NATSBridge.smartsend("/data/analysis", data) envelope, msg_json_str = NATSBridge.smartsend("/data/analysis", data)
``` ```
### Example 4: Request-Response Pattern with Envelope JSON ### Example 4: Request-Response Pattern with Envelope JSON
@@ -650,12 +650,12 @@ Bi-directional communication with reply-to support. The `smartsend` function now
```python ```python
from nats_bridge import smartsend from nats_bridge import smartsend
env, msg_json_str = smartsend( envelope, msg_json_str = smartsend(
"/device/command", "/device/command",
[("command", {"action": "read_sensor"}, "dictionary")], [("command", {"action": "read_sensor"}, "dictionary")],
reply_to="/device/response" reply_to="/device/response"
) )
# env: msgEnvelope_v1 object # envelope: msgEnvelope_v1 object
# msg_json_str: JSON string for publishing to NATS # msg_json_str: JSON string for publishing to NATS
# The msg_json_str can also be published directly using NATS request-reply pattern # The msg_json_str can also be published directly using NATS request-reply pattern
@@ -694,7 +694,7 @@ asyncio.run(main())
```javascript ```javascript
const { smartsend } = require('./src/NATSBridge'); const { smartsend } = require('./src/NATSBridge');
const { env, msg_json_str } = await smartsend("/device/command", [ const { envelope, msg_json_str } = await smartsend("/device/command", [
{ dataname: "command", data: { action: "read_sensor" }, type: "dictionary" } { dataname: "command", data: { action: "read_sensor" }, type: "dictionary" }
], { ], {
replyTo: "/device/response" replyTo: "/device/response"
@@ -736,7 +736,7 @@ main();
```julia ```julia
using NATSBridge using NATSBridge
env, msg_json_str = NATSBridge.smartsend( envelope, msg_json_str = NATSBridge.smartsend(
"/device/command", "/device/command",
[("command", Dict("action" => "read_sensor"), "dictionary")]; [("command", Dict("action" => "read_sensor"), "dictionary")];
reply_to="/device/response" reply_to="/device/response"
@@ -755,7 +755,7 @@ const NATS_URL = "nats://localhost:4222"
function test_responder() function test_responder()
conn = NATS.connect(NATS_URL) conn = NATS.connect(NATS_URL)
NATS.subscribe(conn, SUBJECT) do msg NATS.subscribe(conn, SUBJECT) do msg
envelope = NATSBridge.smartreceive(msg, fileserverDownloadHandler) envelope, msg_json_str = NATSBridge.smartreceive(msg, fileserverDownloadHandler)
for (dataname, data, type) in envelope["payloads"] for (dataname, data, type) in envelope["payloads"]
if dataname == "command" && data["action"] == "read_sensor" if dataname == "command" && data["action"] == "read_sensor"
response = Dict("sensor_id" => "sensor-001", "value" => 42.5) response = Dict("sensor_id" => "sensor-001", "value" => 42.5)
@@ -847,7 +847,7 @@ const NATS_URL = "nats://localhost:4222"
function test_receiver() function test_receiver()
conn = NATS.connect(NATS_URL) conn = NATS.connect(NATS_URL)
NATS.subscribe(conn, SUBJECT) do msg NATS.subscribe(conn, SUBJECT) do msg
envelope = NATSBridge.smartreceive(msg, fileserverDownloadHandler) envelope, msg_json_str = NATSBridge.smartreceive(msg, fileserverDownloadHandler)
for (dataname, data, type) in envelope["payloads"] for (dataname, data, type) in envelope["payloads"]
if dataname == "temperature" if dataname == "temperature"
println("Temperature: $data") println("Temperature: $data")