update
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user