diff --git a/src/NATSBridge.jl b/src/NATSBridge.jl index 26ea775..4b38fec 100644 --- a/src/NATSBridge.jl +++ b/src/NATSBridge.jl @@ -340,8 +340,8 @@ end """ smartsend - Send data either directly via NATS or via a fileserver URL, depending on payload size This function intelligently routes data delivery based on payload size relative to a threshold. -If the serialized payload is smaller than `size_threshold`, it encodes the data as Base64 and publishes directly over NATS. -Otherwise, it uploads the data to a fileserver (by default using `plik_oneshot_upload`) and publishes only the download URL over NATS. +If the serialized payload is smaller than `size_threshold`, it encodes the data as Base64 and constructs a "direct" msg_payload_v1. +Otherwise, it uploads the data to a fileserver (by default using `plik_oneshot_upload`) and constructs a "link" msg_payload_v1 with the download URL. The function accepts a list of (dataname, data, type) tuples as input and processes each payload individually. Each payload can have a different type, enabling mixed-content messages (e.g., chat with text, images, audio). @@ -352,13 +352,13 @@ Each payload can have a different type, enabling mixed-content messages (e.g., c 3. Compares the serialized size against `size_threshold` 4. For small payloads: encodes as Base64, constructs a "direct" msg_payload_v1 5. For large payloads: uploads to the fileserver, constructs a "link" msg_payload_v1 with the URL -6. Converts envelope to JSON string and optionally publishes to NATS +6. Converts envelope to JSON string and returns (NATS publishing is not performed by this function) # Arguments: - `subject::String` - NATS subject to publish the message to - - `data::AbstractArray{Tuple{String, Any, String}}` - List of (dataname, data, type) tuples to send + - `data::AbstractArray{Tuple{String, T1, String}, 1}` - List of (dataname, data, type) tuples to send - `dataname::String` - Name of the payload - - `data::Any` - The actual data to send + - `data::T1` - The actual data to send (any type supported by `_serialize_data`) - `payload_type::String` - Payload type: "text", "dictionary", "arrowtable", "jsontable", "image", "audio", "video", "binary" - No standalone `type` parameter - type is specified per payload @@ -367,17 +367,16 @@ Each payload can have a different type, enabling mixed-content messages (e.g., c - `fileserver_url = DEFAULT_FILESERVER_URL` - URL of the HTTP file server for large payloads - `fileserver_upload_handler::Function = plik_oneshot_upload` - Function to handle fileserver uploads (must return Dict with "status", "uploadid", "fileid", "url" keys) - `size_threshold::Int = DEFAULT_SIZE_THRESHOLD` - Threshold in bytes separating direct vs link transport - - `correlation_id::String = string(uuid4())` - Correlation ID for tracing (auto-generated UUID) - - `msg_purpose::String = "chat"` - Purpose of the message: "ACK", "NACK", "updateStatus", "shutdown", "chat", etc. - - `sender_name::String = "NATSBridge"` - Name of the sender - - `receiver_name::String = ""` - Name of the receiver (empty string means broadcast) + - `correlation_id::String = string(uuid4())` - Correlation ID for tracing (auto-generated UUID) + - `msg_purpose::String = "chat"` - Purpose of the message: "ACK", "NACK", "updateStatus", "shutdown", "chat", etc. + - `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 - - `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) - - `msg_id::String = string(uuid4())` - Message ID (auto-generated UUID if not provided) - - `sender_id::String = string(uuid4())` - Sender ID (auto-generated UUID if not provided) + - `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 + - `NATS_connection::Union{NATS.Connection, Nothing} = nothing` - Pre-existing NATS connection (reserved for future use) + - `msg_id::String = string(uuid4())` - Message ID (auto-generated UUID if not provided) + - `sender_id::String = string(uuid4())` - Sender ID (auto-generated UUID if not provided) # Return: - `::Tuple{msg_envelope_v1, String}` - A tuple containing: @@ -412,8 +411,9 @@ env, msg_json = smartsend("chat.subject", [ ("audio_clip", audio_data, "audio") ]) -# Publish the JSON string directly using NATS request-reply pattern -# reply = NATS.request(broker_url, subject, env_json_str; reply_to=reply_to_topic) +# Publish the JSON string directly using NATS (manual publish) +# conn = NATS.connect(broker_url) +# NATS.publish(conn, subject, env_json_str) ``` """ function smartsend( @@ -438,7 +438,6 @@ function smartsend( receiver_id::String = "", reply_to::String = "", reply_to_msg_id::String = "", - is_publish::Bool = true, # some time the user want to get env and env_json_str from this function without publishing the msg NATS_connection::Union{NATS.Connection, Nothing} = nothing, # a provided connection saves establishing connection overhead. msg_id::String = string(uuid4()), # Message ID sender_id::String = string(uuid4()) # Sender ID @@ -539,13 +538,15 @@ function smartsend( ) env_json_str = envelope_to_json(env) # Convert envelope to JSON - if is_publish == false - # skip publish a message - elseif is_publish == true && NATS_connection === nothing - publish_message(broker_url, subject, env_json_str, correlation_id) # Publish message to NATS - elseif is_publish == true && NATS_connection !== nothing - publish_message(NATS_connection, subject, env_json_str, correlation_id) # Publish message to NATS - end + # if is_publish == false + # # skip publish a message + # elseif is_publish == true && NATS_connection === nothing + # # Publish message to NATS using new connection + # publish_message(broker_url, subject, env_json_str, correlation_id) + # elseif is_publish == true && NATS_connection !== nothing + # # Publish message to NATS using existing connection + # publish_message(NATS_connection, subject, env_json_str, correlation_id) + # end return (env, env_json_str) end