This commit is contained in:
2026-02-23 08:11:03 +07:00
parent f9e08ba628
commit cec70e6036

View File

@@ -107,17 +107,7 @@ NATSBridge enables seamless communication across Julia, JavaScript, and Python/M
```julia ```julia
using Pkg using Pkg
Pkg.add("NATS") Pkg.add("NATS")
Pkg.add("Arrow") Pkg.add("https://git.yiem.cc/ton/NATSBridge")
Pkg.add("JSON3")
Pkg.add("HTTP")
Pkg.add("UUIDs")
Pkg.add("Dates")
```
Add to your `Project.toml`:
```toml
[NATSBridge]
deps = ["NATS", "Arrow", "JSON3", "HTTP", "UUIDs", "Dates"]
``` ```
### JavaScript ### JavaScript
@@ -207,7 +197,7 @@ using NATSBridge
# Send a text message # Send a text message
data = [("message", "Hello World", "text")] data = [("message", "Hello World", "text")]
env = smartsend("/chat/room1", data, nats_url="nats://localhost:4222") env = NATSBridge.smartsend("/chat/room1", data, nats_url="nats://localhost:4222")
println("Message sent!") println("Message sent!")
``` ```
@@ -242,7 +232,7 @@ for (const payload of envelope.payloads) {
using NATSBridge using NATSBridge
# Receive and process message # Receive and process message
envelope = smartreceive(msg, fileserverDownloadHandler) envelope = 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
@@ -307,7 +297,7 @@ const env = await smartsend(
```julia ```julia
using NATSBridge using NATSBridge
env = smartsend( env = 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",
@@ -365,7 +355,7 @@ const envelope = await smartreceive(
```julia ```julia
using NATSBridge using NATSBridge
envelope = smartreceive( envelope = 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,
@@ -485,7 +475,7 @@ data = [
("large_document", large_file_data, "binary") ("large_document", large_file_data, "binary")
] ]
smartsend("/chat/room1", data, fileserver_url="http://localhost:8080") NATSBridge.smartsend("/chat/room1", data, fileserver_url="http://localhost:8080")
``` ```
### Example 2: Dictionary Exchange ### Example 2: Dictionary Exchange
@@ -532,7 +522,7 @@ config = Dict(
) )
data = [("config", config, "dictionary")] data = [("config", config, "dictionary")]
smartsend("/device/config", data) NATSBridge.smartsend("/device/config", data)
``` ```
### Example 3: Table Data (Arrow IPC) ### Example 3: Table Data (Arrow IPC)
@@ -581,7 +571,7 @@ df = DataFrame(
) )
data = [("students", df, "table")] data = [("students", df, "table")]
smartsend("/data/analysis", data) NATSBridge.smartsend("/data/analysis", data)
``` ```
### Example 4: Request-Response Pattern ### Example 4: Request-Response Pattern
@@ -640,7 +630,7 @@ for (const payload of envelope.payloads) {
```julia ```julia
using NATSBridge using NATSBridge
env = smartsend( env = 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"
@@ -651,7 +641,7 @@ env = smartsend(
```julia ```julia
using NATSBridge using NATSBridge
envelope = smartreceive(msg, fileserverDownloadHandler) envelope = 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)
@@ -699,7 +689,7 @@ for (const payload of envelope.payloads) {
```julia ```julia
using NATSBridge using NATSBridge
envelope = smartreceive(msg, fileserverDownloadHandler) envelope = 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")