From df0bbc7327ddf2e9ca0c20321d70bb1a4659a131 Mon Sep 17 00:00:00 2001 From: narawat Date: Wed, 25 Feb 2026 09:58:10 +0700 Subject: [PATCH] update --- src/NATSBridge.jl | 53 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/src/NATSBridge.jl b/src/NATSBridge.jl index eda6441..c2acaa0 100644 --- a/src/NATSBridge.jl +++ b/src/NATSBridge.jl @@ -380,9 +380,10 @@ Each payload can have a different type, enabling mixed-content messages (e.g., c - `sender_name::String = "NATSBridge"` - Name of the sender - `receiver_name::String = ""` - Name of the receiver (empty string means broadcast) - `receiver_id::String = ""` - UUID of the receiver (empty string means broadcast) - - `reply_to::String = ""` - Topic to reply to (empty string if no reply expected) - - `reply_to_msg_id::String = ""` - Message ID this message is replying to - - `is_publish::Bool = true` - Whether to automatically publish the message to NATS + - `reply_to::String = ""` - Topic to reply to (empty string if no reply expected) + - `reply_to_msg_id::String = ""` - Message ID this message is replying to + - `is_publish::Bool = true` - Whether to automatically publish the message to NATS + - `NATS_connection::Union{NATS.Connection, Nothing} = nothing` - Pre-existing NATS connection (if provided, uses this connection instead of creating a new one; saves connection establishment overhead) # Return: - A tuple `(env, env_json_str)` where: @@ -653,7 +654,7 @@ end """ publish_message - Publish message to NATS -This internal function publishes a message to a NATS subject with proper +This function publishes a message to a NATS subject with proper connection management and logging. # Arguments: @@ -666,21 +667,51 @@ connection management and logging. - `nothing` - This function performs publishing but returns nothing # Example - ```jldoctest - using NATS +```jldoctest +using NATS - # Prepare JSON message - message = "{\"correlation_id\":\"abc123\",\"payload\":\"test\"}" +# Prepare JSON message +message = "{\"correlation_id\":\"abc123\",\"payload\":\"test\"}" - # Publish to NATS - publish_message("nats://localhost:4222", "my.subject", message, "abc123") - ``` +# Publish to NATS +publish_message("nats://localhost:4222", "my.subject", message, "abc123") +``` """ 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 +""" publish_message - Publish message to NATS using pre-existing connection +This function publishes a message to a NATS subject using a pre-existing NATS connection, +avoiding the overhead of connection establishment. + +# Arguments: + - `conn::NATS.Connection` - Pre-existing NATS connection + - `subject::String` - NATS subject to publish to (e.g., "/agent/wine/api/v1/prompt") + - `message::String` - JSON message to publish + - `correlation_id::String` - Correlation ID for tracing and logging + +# Return: + - `nothing` - This function performs publishing but returns nothing + +# Example +```jldoctest +using NATS + +# Prepare JSON message +message = "{\"correlation_id\":\"abc123\",\"payload\":\"test\"}" + +# Create connection once and reuse for multiple publishes +conn = NATS.connect("nats://localhost:4222") +publish_message(conn, "my.subject", message, "abc123") +# Connection is automatically drained after publish +``` + +# Use Case: +Use this version 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. +""" function publish_message(conn::NATS.Connection, subject::String, message::String, correlation_id::String) try NATS.publish(conn, subject, message) # Publish message to NATS