update
This commit is contained in:
@@ -109,13 +109,13 @@ from nats_bridge import smartsend
|
||||
|
||||
# Send a text message (is_publish=True by default)
|
||||
data = [("message", "Hello World", "text")]
|
||||
env, msg_json_str = smartsend("/chat/room1", data, nats_url="nats://localhost:4222")
|
||||
env, env_json_str = smartsend("/chat/room1", data, nats_url="nats://localhost:4222")
|
||||
print("Message sent!")
|
||||
|
||||
# Or use is_publish=False to get envelope and JSON without publishing
|
||||
env, msg_json_str = smartsend("/chat/room1", data, nats_url="nats://localhost:4222", is_publish=False)
|
||||
env, env_json_str = smartsend("/chat/room1", data, nats_url="nats://localhost:4222", is_publish=False)
|
||||
# env: MessageEnvelope object
|
||||
# msg_json_str: JSON string for publishing to NATS
|
||||
# env_json_str: JSON string for publishing to NATS
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
@@ -131,11 +131,11 @@ await smartsend("/chat/room1", [
|
||||
console.log("Message sent!");
|
||||
|
||||
// Or use isPublish=false to get envelope and JSON without publishing
|
||||
const { env, msg_json_str } = await smartsend("/chat/room1", [
|
||||
const { env, env_json_str } = await smartsend("/chat/room1", [
|
||||
{ dataname: "message", data: "Hello World", type: "text" }
|
||||
], { natsUrl: "nats://localhost:4222", isPublish: false });
|
||||
// env: MessageEnvelope object
|
||||
// msg_json_str: JSON string for publishing to NATS
|
||||
// env_json_str: JSON string for publishing to NATS
|
||||
```
|
||||
|
||||
#### Julia
|
||||
@@ -145,9 +145,9 @@ using NATSBridge
|
||||
|
||||
# Send a text message
|
||||
data = [("message", "Hello World", "text")]
|
||||
env, msg_json_str = smartsend("/chat/room1", data, nats_url="nats://localhost:4222")
|
||||
env, env_json_str = smartsend("/chat/room1", data, nats_url="nats://localhost:4222")
|
||||
# env: msgEnvelope_v1 object with all metadata and payloads
|
||||
# msg_json_str: JSON string representation of the envelope for publishing
|
||||
# env_json_str: JSON string representation of the envelope for publishing
|
||||
println("Message sent!")
|
||||
```
|
||||
|
||||
@@ -208,7 +208,7 @@ config = {
|
||||
|
||||
# Send as dictionary type
|
||||
data = [("config", config, "dictionary")]
|
||||
env, msg_json_str = smartsend("/device/config", data, nats_url="nats://localhost:4222")
|
||||
env, env_json_str = smartsend("/device/config", data, nats_url="nats://localhost:4222")
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
@@ -222,7 +222,7 @@ const config = {
|
||||
update_interval: 60
|
||||
};
|
||||
|
||||
const { env, msg_json_str } = await smartsend("/device/config", [
|
||||
const { env, env_json_str } = await smartsend("/device/config", [
|
||||
{ dataname: "config", data: config, type: "dictionary" }
|
||||
]);
|
||||
```
|
||||
@@ -239,7 +239,7 @@ config = Dict(
|
||||
)
|
||||
|
||||
data = [("config", config, "dictionary")]
|
||||
env, msg_json_str = smartsend("/device/config", data)
|
||||
env, env_json_str = smartsend("/device/config", data)
|
||||
```
|
||||
|
||||
### Example 2: Sending Binary Data (Image)
|
||||
@@ -255,7 +255,7 @@ with open("image.png", "rb") as f:
|
||||
|
||||
# Send as binary type
|
||||
data = [("user_image", image_data, "binary")]
|
||||
env, msg_json_str = smartsend("/chat/image", data, nats_url="nats://localhost:4222")
|
||||
env, env_json_str = smartsend("/chat/image", data, nats_url="nats://localhost:4222")
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
@@ -267,7 +267,7 @@ const { smartsend } = require('./src/NATSBridge');
|
||||
const fs = require('fs');
|
||||
const image_data = fs.readFileSync('image.png');
|
||||
|
||||
const { env, msg_json_str } = await smartsend("/chat/image", [
|
||||
const { env, env_json_str } = await smartsend("/chat/image", [
|
||||
{ dataname: "user_image", data: image_data, type: "binary" }
|
||||
]);
|
||||
```
|
||||
@@ -281,7 +281,7 @@ using NATSBridge
|
||||
image_data = read("image.png")
|
||||
|
||||
data = [("user_image", image_data, "binary")]
|
||||
env, msg_json_str = smartsend("/chat/image", data)
|
||||
env, env_json_str = smartsend("/chat/image", data)
|
||||
```
|
||||
|
||||
### Example 3: Request-Response Pattern
|
||||
@@ -293,7 +293,7 @@ from nats_bridge import smartsend
|
||||
|
||||
# Send command with reply-to
|
||||
data = [("command", {"action": "read_sensor"}, "dictionary")]
|
||||
env, msg_json_str = smartsend(
|
||||
env, env_json_str = smartsend(
|
||||
"/device/command",
|
||||
data,
|
||||
nats_url="nats://localhost:4222",
|
||||
@@ -301,7 +301,7 @@ env, msg_json_str = smartsend(
|
||||
reply_to_msg_id="cmd-001"
|
||||
)
|
||||
# env: msgEnvelope_v1 object
|
||||
# msg_json_str: JSON string for publishing to NATS
|
||||
# env_json_str: JSON string for publishing to NATS
|
||||
```
|
||||
|
||||
#### JavaScript (Responder)
|
||||
@@ -358,7 +358,7 @@ import os
|
||||
large_data = os.urandom(2_000_000) # 2MB of random data
|
||||
|
||||
# Send with file server URL
|
||||
env, msg_json_str = smartsend(
|
||||
env, env_json_str = smartsend(
|
||||
"/data/large",
|
||||
[("large_file", large_data, "binary")],
|
||||
nats_url="nats://localhost:4222",
|
||||
@@ -380,7 +380,7 @@ const largeData = new ArrayBuffer(2_000_000);
|
||||
const view = new Uint8Array(largeData);
|
||||
view.fill(42); // Fill with some data
|
||||
|
||||
const { env, msg_json_str } = await smartsend("/data/large", [
|
||||
const { env, env_json_str } = await smartsend("/data/large", [
|
||||
{ dataname: "large_file", data: largeData, type: "binary" }
|
||||
], {
|
||||
fileserverUrl: "http://localhost:8080",
|
||||
@@ -396,7 +396,7 @@ using NATSBridge
|
||||
# Create large data (> 1MB)
|
||||
large_data = rand(UInt8, 2_000_000)
|
||||
|
||||
env, msg_json_str = smartsend(
|
||||
env, env_json_str = smartsend(
|
||||
"/data/large",
|
||||
[("large_file", large_data, "binary")],
|
||||
fileserver_url="http://localhost:8080"
|
||||
@@ -425,7 +425,7 @@ data = [
|
||||
("user_avatar", image_data, "image")
|
||||
]
|
||||
|
||||
env, msg_json_str = smartsend("/chat/mixed", data, nats_url="nats://localhost:4222")
|
||||
env, env_json_str = smartsend("/chat/mixed", data, nats_url="nats://localhost:4222")
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
@@ -435,7 +435,7 @@ const { smartsend } = require('./src/NATSBridge');
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
const { env, msg_json_str } = await smartsend("/chat/mixed", [
|
||||
const { env, env_json_str } = await smartsend("/chat/mixed", [
|
||||
{
|
||||
dataname: "message_text",
|
||||
data: "Hello with image!",
|
||||
@@ -461,7 +461,7 @@ data = [
|
||||
("user_avatar", image_data, "image")
|
||||
]
|
||||
|
||||
env, msg_json_str = smartsend("/chat/mixed", data)
|
||||
env, env_json_str = smartsend("/chat/mixed", data)
|
||||
```
|
||||
|
||||
### Example 6: Table Data (Arrow IPC)
|
||||
@@ -483,7 +483,7 @@ df = pd.DataFrame({
|
||||
|
||||
# Send as table type
|
||||
data = [("students", df, "table")]
|
||||
env, msg_json_str = smartsend("/data/students", data, nats_url="nats://localhost:4222")
|
||||
env, env_json_str = smartsend("/data/students", data, nats_url="nats://localhost:4222")
|
||||
```
|
||||
|
||||
#### Julia
|
||||
@@ -500,7 +500,7 @@ df = DataFrame(
|
||||
)
|
||||
|
||||
data = [("students", df, "table")]
|
||||
env, msg_json_str = smartsend("/data/students", data)
|
||||
env, env_json_str = smartsend("/data/students", data)
|
||||
```
|
||||
|
||||
---
|
||||
@@ -519,7 +519,7 @@ using NATSBridge
|
||||
# Send dictionary from Julia to JavaScript
|
||||
config = Dict("step_size" => 0.01, "iterations" => 1000)
|
||||
data = [("config", config, "dictionary")]
|
||||
env, msg_json_str = smartsend("/analysis/config", data, nats_url="nats://localhost:4222")
|
||||
env, env_json_str = smartsend("/analysis/config", data, nats_url="nats://localhost:4222")
|
||||
```
|
||||
|
||||
#### JavaScript Receiver
|
||||
@@ -544,7 +544,7 @@ for (const payload of envelope.payloads) {
|
||||
```javascript
|
||||
const { smartsend } = require('./src/NATSBridge');
|
||||
|
||||
const { env, msg_json_str } = await smartsend("/data/transfer", [
|
||||
const { env, env_json_str } = await smartsend("/data/transfer", [
|
||||
{ dataname: "message", data: "Hello from JS!", type: "text" }
|
||||
]);
|
||||
```
|
||||
@@ -568,7 +568,7 @@ for dataname, data, type in envelope["payloads"]:
|
||||
from nats_bridge import smartsend
|
||||
|
||||
data = [("message", "Hello from Python!", "text")]
|
||||
env, msg_json_str = smartsend("/chat/python", data)
|
||||
env, env_json_str = smartsend("/chat/python", data)
|
||||
```
|
||||
|
||||
#### Julia Receiver
|
||||
|
||||
@@ -132,7 +132,7 @@ class ChatUI {
|
||||
});
|
||||
}
|
||||
|
||||
const { env, msg_json_str } = await smartsend(
|
||||
const { env, env_json_str } = await smartsend(
|
||||
`/chat/${this.currentRoom}`,
|
||||
data,
|
||||
{
|
||||
@@ -304,7 +304,7 @@ class FileUploadService {
|
||||
type: 'binary'
|
||||
}];
|
||||
|
||||
const { env, msg_json_str } = await smartsend(
|
||||
const { env, env_json_str } = await smartsend(
|
||||
`/files/${recipient}`,
|
||||
data,
|
||||
{
|
||||
@@ -534,7 +534,7 @@ class SensorSender:
|
||||
data = [("reading", reading.to_dict(), "dictionary")]
|
||||
|
||||
# With is_publish=False, returns (envelope, json_str) without publishing
|
||||
env, msg_json_str = smartsend(
|
||||
env, env_json_str = smartsend(
|
||||
f"/sensors/{sensor_id}/prepare",
|
||||
data,
|
||||
nats_url=self.nats_url,
|
||||
@@ -543,9 +543,9 @@ class SensorSender:
|
||||
)
|
||||
|
||||
# Now you can publish manually using NATS request-reply pattern
|
||||
# nc.request(subject, msg_json_str, reply_to=reply_to_topic)
|
||||
# nc.request(subject, env_json_str, reply_to=reply_to_topic)
|
||||
|
||||
return env, msg_json_str
|
||||
return env, env_json_str
|
||||
|
||||
def send_batch(self, readings: List[SensorReading]):
|
||||
batch = SensorBatch()
|
||||
@@ -833,7 +833,7 @@ class DashboardUI {
|
||||
|
||||
async refreshData() {
|
||||
// Request fresh data
|
||||
const { env, msg_json_str } = await smartsend("/dashboard/request", [
|
||||
const { env, env_json_str } = await smartsend("/dashboard/request", [
|
||||
{ dataname: "request", data: { type: "refresh" }, type: "dictionary" }
|
||||
], {
|
||||
fileserverUrl: window.config.fileserver_url
|
||||
|
||||
Reference in New Issue
Block a user