This commit is contained in:
2026-02-25 09:58:10 +07:00
parent d94761c866
commit df0bbc7327

View File

@@ -383,6 +383,7 @@ Each payload can have a different type, enabling mixed-content messages (e.g., c
- `reply_to::String = ""` - Topic to reply to (empty string if no reply expected) - `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 - `reply_to_msg_id::String = ""` - Message ID this message is replying to
- `is_publish::Bool = true` - Whether to automatically publish the message to NATS - `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: # Return:
- A tuple `(env, env_json_str)` where: - A tuple `(env, env_json_str)` where:
@@ -653,7 +654,7 @@ end
""" publish_message - Publish message to NATS """ 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. connection management and logging.
# Arguments: # Arguments:
@@ -666,21 +667,51 @@ connection management and logging.
- `nothing` - This function performs publishing but returns nothing - `nothing` - This function performs publishing but returns nothing
# Example # Example
```jldoctest ```jldoctest
using NATS using NATS
# Prepare JSON message # Prepare JSON message
message = "{\"correlation_id\":\"abc123\",\"payload\":\"test\"}" message = "{\"correlation_id\":\"abc123\",\"payload\":\"test\"}"
# Publish to NATS # Publish to NATS
publish_message("nats://localhost:4222", "my.subject", message, "abc123") publish_message("nats://localhost:4222", "my.subject", message, "abc123")
``` ```
""" """
function publish_message(broker_url::String, subject::String, message::String, correlation_id::String) function publish_message(broker_url::String, subject::String, message::String, correlation_id::String)
conn = NATS.connect(broker_url) # Create NATS connection conn = NATS.connect(broker_url) # Create NATS connection
publish_message(conn, subject, message, correlation_id) publish_message(conn, subject, message, correlation_id)
end 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) function publish_message(conn::NATS.Connection, subject::String, message::String, correlation_id::String)
try try
NATS.publish(conn, subject, message) # Publish message to NATS NATS.publish(conn, subject, message) # Publish message to NATS