update
This commit is contained in:
87
README.md
87
README.md
@@ -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, env_json_str = smartsend("/chat/room1", data, nats_url="nats://localhost:4222")
|
env, env_json_str = smartsend("/chat/room1", data, broker_url="nats://localhost:4222")
|
||||||
print("Message sent!")
|
print("Message sent!")
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -185,7 +185,7 @@ const { smartsend } = require('./src/NATSBridge');
|
|||||||
// Send a text message
|
// Send a text message
|
||||||
const { env, env_json_str } = await smartsend("/chat/room1", [
|
const { env, env_json_str } = await smartsend("/chat/room1", [
|
||||||
{ dataname: "message", data: "Hello World", type: "text" }
|
{ dataname: "message", data: "Hello World", type: "text" }
|
||||||
], { natsUrl: "nats://localhost:4222" });
|
], { broker_url: "nats://localhost:4222" });
|
||||||
|
|
||||||
console.log("Message sent!");
|
console.log("Message sent!");
|
||||||
```
|
```
|
||||||
@@ -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, env_json_str = NATSBridge.smartsend("/chat/room1", data; nats_url="nats://localhost:4222")
|
env, env_json_str = NATSBridge.smartsend("/chat/room1", data; broker_url="nats://localhost:4222")
|
||||||
println("Message sent!")
|
println("Message sent!")
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -313,7 +313,7 @@ from nats_bridge import smartsend
|
|||||||
env, env_json_str = smartsend(
|
env, env_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
|
broker_url="nats://localhost:4222", # NATS server URL
|
||||||
fileserver_url="http://localhost:8080", # File server URL
|
fileserver_url="http://localhost:8080", # File server URL
|
||||||
fileserver_upload_handler=plik_oneshot_upload, # Upload handler function
|
fileserver_upload_handler=plik_oneshot_upload, # Upload handler function
|
||||||
size_threshold=1_000_000, # Threshold in bytes (default: 1MB)
|
size_threshold=1_000_000, # Threshold in bytes (default: 1MB)
|
||||||
@@ -337,18 +337,18 @@ const { env, env_json_str } = await smartsend(
|
|||||||
subject, // NATS subject
|
subject, // NATS subject
|
||||||
data, // Array of {dataname, data, type}
|
data, // Array of {dataname, data, type}
|
||||||
{
|
{
|
||||||
natsUrl: "nats://localhost:4222",
|
broker_url: "nats://localhost:4222",
|
||||||
fileserverUrl: "http://localhost:8080",
|
fileserver_url: "http://localhost:8080",
|
||||||
fileserverUploadHandler: customUploadHandler,
|
fileserver_upload_handler: customUploadHandler,
|
||||||
sizeThreshold: 1_000_000,
|
size_threshold: 1_000_000,
|
||||||
correlationId: "custom-id",
|
correlation_id: "custom-id",
|
||||||
msgPurpose: "chat",
|
msg_purpose: "chat",
|
||||||
senderName: "NATSBridge",
|
sender_name: "NATSBridge",
|
||||||
receiverName: "",
|
receiver_name: "",
|
||||||
receiverId: "",
|
receiver_id: "",
|
||||||
replyTo: "",
|
reply_to: "",
|
||||||
replyToMsgId: "",
|
reply_to_msg_id: "",
|
||||||
isPublish: true // Whether to automatically publish to NATS
|
is_publish: true // Whether to automatically publish to NATS
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
@@ -361,9 +361,9 @@ using NATSBridge
|
|||||||
env, env_json_str = NATSBridge.smartsend(
|
env, env_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",
|
broker_url::String = "nats://localhost:4222",
|
||||||
fileserver_url = "http://localhost:8080",
|
fileserver_url = "http://localhost:8080",
|
||||||
fileserverUploadHandler::Function = plik_oneshot_upload,
|
fileserver_upload_handler::Function = plik_oneshot_upload,
|
||||||
size_threshold::Int = 1_000_000,
|
size_threshold::Int = 1_000_000,
|
||||||
correlation_id::Union{String, Nothing} = nothing,
|
correlation_id::Union{String, Nothing} = nothing,
|
||||||
msg_purpose::String = "chat",
|
msg_purpose::String = "chat",
|
||||||
@@ -372,7 +372,8 @@ env, env_json_str = NATSBridge.smartsend(
|
|||||||
receiver_id::String = "",
|
receiver_id::String = "",
|
||||||
reply_to::String = "",
|
reply_to::String = "",
|
||||||
reply_to_msg_id::String = "",
|
reply_to_msg_id::String = "",
|
||||||
is_publish::Bool = true # Whether to automatically publish to NATS
|
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)
|
# Returns: (msgEnvelope_v1, JSON string)
|
||||||
# - env: msgEnvelope_v1 object with all envelope metadata and payloads
|
# - env: msgEnvelope_v1 object with all envelope metadata and payloads
|
||||||
@@ -423,9 +424,9 @@ const env = 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
|
||||||
env, env_json_str = NATSBridge.smartreceive(
|
env = NATSBridge.smartreceive(
|
||||||
msg::NATS.Msg;
|
msg::NATS.Msg;
|
||||||
fileserverDownloadHandler::Function = _fetch_with_backoff,
|
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
|
||||||
@@ -433,6 +434,35 @@ env, env_json_str = NATSBridge.smartreceive(
|
|||||||
# Returns: Dict with envelope metadata and payloads array
|
# 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
|
## Payload Types
|
||||||
@@ -488,7 +518,7 @@ smartsend("/topic", data, fileserver_url="http://localhost:8080")
|
|||||||
```javascript
|
```javascript
|
||||||
await smartsend("/topic", [
|
await smartsend("/topic", [
|
||||||
{ dataname: "file", data: largeData, type: "binary" }
|
{ dataname: "file", data: largeData, type: "binary" }
|
||||||
], { fileserverUrl: "http://localhost:8080" });
|
], { fileserver_url: "http://localhost:8080" });
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Julia
|
#### Julia
|
||||||
@@ -529,7 +559,7 @@ const { env, env_json_str } = await smartsend("/chat/room1", [
|
|||||||
{ 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" }
|
||||||
], {
|
], {
|
||||||
fileserverUrl: "http://localhost:8080"
|
fileserver_url: "http://localhost:8080"
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -653,9 +683,10 @@ from nats_bridge import smartsend
|
|||||||
env, env_json_str = smartsend(
|
env, env_json_str = smartsend(
|
||||||
"/device/command",
|
"/device/command",
|
||||||
[("command", {"action": "read_sensor"}, "dictionary")],
|
[("command", {"action": "read_sensor"}, "dictionary")],
|
||||||
|
broker_url="nats://localhost:4222",
|
||||||
reply_to="/device/response"
|
reply_to="/device/response"
|
||||||
)
|
)
|
||||||
# env: msgEnvelope_v1 object
|
# env: MessageEnvelope object
|
||||||
# env_json_str: JSON string for publishing to NATS
|
# env_json_str: JSON string for publishing to NATS
|
||||||
|
|
||||||
# The env_json_str can also be published directly using NATS request-reply pattern
|
# The env_json_str can also be published directly using NATS request-reply pattern
|
||||||
@@ -697,7 +728,8 @@ const { smartsend } = require('./src/NATSBridge');
|
|||||||
const { env, env_json_str } = await smartsend("/device/command", [
|
const { env, env_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"
|
broker_url: "nats://localhost:4222",
|
||||||
|
reply_to: "/device/response"
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -739,6 +771,7 @@ using NATSBridge
|
|||||||
env, env_json_str = NATSBridge.smartsend(
|
env, env_json_str = NATSBridge.smartsend(
|
||||||
"/device/command",
|
"/device/command",
|
||||||
[("command", Dict("action" => "read_sensor"), "dictionary")];
|
[("command", Dict("action" => "read_sensor"), "dictionary")];
|
||||||
|
broker_url="nats://localhost:4222",
|
||||||
reply_to="/device/response"
|
reply_to="/device/response"
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
@@ -755,7 +788,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
|
||||||
env, env_json_str = NATSBridge.smartreceive(msg, fileserverDownloadHandler)
|
env = NATSBridge.smartreceive(msg, fileserver_download_handler=_fetch_with_backoff)
|
||||||
for (dataname, data, type) in env["payloads"]
|
for (dataname, data, type) in env["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)
|
||||||
@@ -790,7 +823,7 @@ async def main():
|
|||||||
|
|
||||||
# Send sensor data
|
# Send sensor data
|
||||||
data = [("temperature", "25.5", "text"), ("humidity", 65, "dictionary")]
|
data = [("temperature", "25.5", "text"), ("humidity", 65, "dictionary")]
|
||||||
smartsend("/device/sensors", data, nats_url="nats://localhost:4222")
|
smartsend("/device/sensors", data, broker_url="nats://localhost:4222")
|
||||||
|
|
||||||
# Receive commands - msg comes from the callback
|
# Receive commands - msg comes from the callback
|
||||||
async def message_handler(msg):
|
async def message_handler(msg):
|
||||||
|
|||||||
9
etc.jl
9
etc.jl
@@ -0,0 +1,9 @@
|
|||||||
|
Task: Update README.md to reflect recent changes in NATSbridge package.
|
||||||
|
|
||||||
|
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.
|
||||||
Reference in New Issue
Block a user