This commit is contained in:
2026-05-23 13:07:37 +07:00
parent c919f9585d
commit f06fbce486

246
README.md
View File

@@ -178,7 +178,7 @@ env, env_json_str = smartpack(
"/chat/room1", "/chat/room1",
data, data,
broker_url="nats://localhost:4222", broker_url="nats://localhost:4222",
size_threshold=100000 # 100KB for MicroPython size_threshold=100000
) )
print("Message sent!") print("Message sent!")
``` ```
@@ -193,12 +193,10 @@ using msghandler, NATS
conn = NATS.connect("nats.yiem.cc") conn = NATS.connect("nats.yiem.cc")
NATS.subscribe(conn, "test.topic") do msg NATS.subscribe(conn, "test.topic") do msg
println("Received message on $(msg.subject)") println("Received message on $(msg.subject)")
payloads = String(msg.payload) envelope_json_str = String(msg.payload)
# Use NATSBridge.smartreceive to handle the data
# API: smartreceive(msg, download_handler; max_retries, base_delay, max_delay)
envelope = msghandler.smartunpack( envelope = msghandler.smartunpack(
payloads; envelope_json_str;
max_retries = 5, max_retries = 5,
base_delay = 100, base_delay = 100,
max_delay = 5000 max_delay = 5000
@@ -359,16 +357,16 @@ env, env_json_str = await msghandler.smartpack(
#### MicroPython #### MicroPython
```python ```python
from msghandler import msghandler from msghandler import smartpack
# Limited to direct transport (< 100KB threshold) # Limited to direct transport (< 100KB threshold)
env, env_json_str = msghandler.smartpack( env, env_json_str = smartpack(
subject, "/device/config",
data, # List of (dataname, data, type) tuples [("config", config, "dictionary")],
broker_url="nats://localhost:4222", broker_url="nats://localhost:4222",
size_threshold=100000 # Lower threshold for memory constraints size_threshold=100000
) )
# Returns: Tuple[Dict, str] print("Message sent!")
``` ```
### smartunpack ### smartunpack
@@ -442,16 +440,16 @@ env = await msghandler.smartunpack(
#### MicroPython #### MicroPython
```python ```python
from msghandler import msghandler from msghandler import smartunpack
env = msghandler.smartunpack( env = smartunpack(
msg, msg,
fileserver_download_handler=_sync_fileserver_download, fileserver_download_handler=_sync_fileserver_download,
max_retries=3, max_retries=3,
base_delay=100, base_delay=100,
max_delay=1000 max_delay=1000
) )
# Returns: Dict with "payloads" key print("Received payloads:", env["payloads"])
``` ```
--- ---
@@ -516,33 +514,63 @@ import msghandler from './src/msghandler_csr.js';
const data = [ const data = [
["message_text", "Hello!", "text"], ["message_text", "Hello!", "text"],
["user_avatar", imageData, "image"], ["user_avatar", image_data, "image"]
["large_document", largeFileData, "binary"]
]; ];
const [env, env_json_str] = await msghandler.smartpack( const [env, env_json_str] = await msghandler.smartpack(
"/chat/room1", "/chat/room1",
data, data,
{ broker_url: 'ws://localhost:4222', fileserver_url: 'http://localhost:8080' } { broker_url: 'ws://localhost:4222' }
); );
console.log("Message sent!");
``` ```
#### Python #### Python
```python ```python
from msghandler import msghandler import asyncio
from msghandler import smartpack
data = [ async def main():
image_data = open("user_avatar.png", "rb").read()
large_file_data = open("large_document.pdf", "rb").read()
data = [
("message_text", "Hello!", "text"), ("message_text", "Hello!", "text"),
("user_avatar", image_data, "image"), ("user_avatar", image_data, "image"),
("large_document", large_file_data, "binary") ("large_document", large_file_data, "binary")
] ]
env, env_json_str = await msghandler.smartpack( env, env_json_str = await smartpack(
"/chat/room1", "/chat/room1",
data, data,
fileserver_url="http://localhost:8080" fileserver_url="http://localhost:8080"
)
print("Message sent!")
if __name__ == "__main__":
asyncio.run(main())
```
#### MicroPython
```python
from msghandler import smartpack
# Note: MicroPython only supports direct transport (< 100KB threshold)
# Large files must be uploaded via file server first
data = [
("message_text", "Hello!", "text"),
("user_avatar", image_bytes, "image")
]
env, env_json_str = smartpack(
"/chat/room1",
data,
broker_url="nats://localhost:4222",
size_threshold=100000
) )
print("Message sent!")
``` ```
### Example 2: Dictionary Exchange ### Example 2: Dictionary Exchange
@@ -579,12 +607,34 @@ const [env, env_json_str] = await msghandler.smartpack(
"/device/config", "/device/config",
[["config", config, "dictionary"]] [["config", config, "dictionary"]]
); );
console.log("Message sent!");
``` ```
#### Python #### Python
```python ```python
from msghandler import msghandler import asyncio
from msghandler import smartpack
async def main():
config = {
"wifi_ssid": "MyNetwork",
"wifi_password": "password123",
"update_interval": 60
}
data = [("config", config, "dictionary")]
env, env_json_str = await smartpack("/device/config", data)
print("Message sent!")
if __name__ == "__main__":
asyncio.run(main())
```
#### MicroPython
```python
from msghandler import smartpack
config = { config = {
"wifi_ssid": "MyNetwork", "wifi_ssid": "MyNetwork",
@@ -593,7 +643,13 @@ config = {
} }
data = [("config", config, "dictionary")] data = [("config", config, "dictionary")]
env, env_json_str = await msghandler.smartpack("/device/config", data) env, env_json_str = smartpack(
"/device/config",
data,
broker_url="nats://localhost:4222",
size_threshold=100000
)
print("Message sent!")
``` ```
### Example 3: Table Data (Arrow IPC) ### Example 3: Table Data (Arrow IPC)
@@ -654,8 +710,6 @@ env, env_json_str = await msghandler.smartpack("/data/analysis", data)
```javascript ```javascript
import msghandler from './src/msghandler_csr.js'; import msghandler from './src/msghandler_csr.js';
// Browser uses jsontable (JSON array of objects) instead of arrowtable
// Apache Arrow is not compatible with browsers
const df = [ const df = [
{ id: 1, name: "Alice", score: 95 }, { id: 1, name: "Alice", score: 95 },
{ id: 2, name: "Bob", score: 88 }, { id: 2, name: "Bob", score: 88 },
@@ -664,9 +718,33 @@ const df = [
const [env, env_json_str] = await msghandler.smartpack( const [env, env_json_str] = await msghandler.smartpack(
"/data/analysis", "/data/analysis",
[["students", df, "jsontable"]], // Use jsontable for browser [["students", df, "jsontable"]],
{ broker_url: 'ws://localhost:4222' } { broker_url: 'ws://localhost:4222' }
); );
console.log("Message sent!");
```
#### MicroPython
```python
from msghandler import smartpack
# Note: MicroPython only supports direct transport (< 100KB threshold)
# MicroPython doesn't support Arrow IPC, only jsontable
df = [
{"id": 1, "name": "Alice", "score": 95},
{"id": 2, "name": "Bob", "score": 88},
{"id": 3, "name": "Charlie", "score": 92}
]
data = [("students", df, "jsontable")]
env, env_json_str = smartpack(
"/data/analysis",
data,
broker_url="nats://localhost:4222",
size_threshold=100000
)
print("Message sent!")
``` ```
### Example 4: Request-Response Pattern ### Example 4: Request-Response Pattern
@@ -676,32 +754,44 @@ Bi-directional communication with reply-to support.
#### Julia #### Julia
```julia ```julia
using msghandler using msghandler, NATS
# Requester # Requester
env, env_json_str = smartpack( env, env_json_str = msghandler.smartpack(
"/device/command", "/device/command",
[("command", Dict("action" => "read_sensor"), "dictionary")]; [("command", Dict("action" => "read_sensor"), "dictionary")];
broker_url="nats://localhost:4222", broker_url="nats://localhost:4222",
reply_to="/device/response" reply_to="/device/response"
) )
conn = NATS.connect("nats://localhost:4222")
NATS.publish(conn, "/device/command", env_json_str)
NATS.drain(conn)
# Receiver (in separate application) # Receiver (in separate application)
msg = NATS.subscription.next() conn = NATS.connect("nats://localhost:4222")
env = smartunpack(msg) NATS.subscribe(conn, "/device/command") do msg
# Process request and send response env = msghandler.smartunpack(msg)
response_env, response_json = smartpack( println("Received command: ", env["payloads"])
result = Dict("value" => 42)
response_env, response_json = msghandler.smartpack(
"/device/response", "/device/response",
[("result", Dict("value" => 42), "dictionary")], [("result", result, "dictionary")],
reply_to="/device/command", reply_to="/device/command",
reply_to_msg_id=env["msg_id"] reply_to_msg_id=env["msg_id"]
) )
NATS.publish(conn, "/device/response", response_json)
NATS.drain(conn)
end
``` ```
#### JavaScript (Node.js) #### JavaScript (Node.js)
```javascript ```javascript
import msghandler from './src/msghandler_ssr.js'; import msghandler from './src/msghandler_ssr.js';
import { connect } from 'nats';
// Requester // Requester
const [env, env_json_str] = await msghandler.smartpack( const [env, env_json_str] = await msghandler.smartpack(
@@ -709,11 +799,76 @@ const [env, env_json_str] = await msghandler.smartpack(
[["command", { action: "read_sensor" }, "dictionary"]], [["command", { action: "read_sensor" }, "dictionary"]],
{ broker_url: 'nats://localhost:4222', reply_to: '/device/response' } { broker_url: 'nats://localhost:4222', reply_to: '/device/response' }
); );
const nc = await connect({ port: 4222 });
nc.publish("/device/command", env_json_str);
await nc.flush();
// Receiver (in separate application)
const nc = await connect({ port: 4222 });
const sub = nc.subscribe("/device/command");
for await (const msg of sub) {
const env = await msghandler.smartunpack(msg);
console.log("Received command:", env.payloads);
const response_env, response_json = await msghandler.smartpack(
"/device/response",
[["result", { value: 42 }, "dictionary"]],
{ reply_to: '/device/command', reply_to_msg_id: env.msg_id }
);
nc.publish("/device/response", response_json);
await nc.flush();
}
```
#### Python
```python
import asyncio
from msghandler import smartpack
async def main():
# Requester
env, env_json_str = await smartpack(
"/device/command",
[("command", {"action": "read_sensor"}, "dictionary")],
broker_url="nats://localhost:4222",
reply_to="/device/response"
)
print("Request sent!")
# Receiver (in separate application)
# await nats_consumer.next()
# env = await smartunpack(msg)
# Process request and send response
# response_env, response_json = await smartpack(
# "/device/response",
# [("result", {"value": 42}, "dictionary")],
# reply_to="/device/command",
# reply_to_msg_id=env["msg_id"]
# )
if __name__ == "__main__":
asyncio.run(main())
```
#### JavaScript (Browser)
```javascript
import msghandler from './src/msghandler_csr.js';
// Requester
const [env, env_json_str] = await msghandler.smartpack(
"/device/command",
[["command", { action: "read_sensor" }, "dictionary"]],
{ broker_url: 'ws://localhost:4222', reply_to: '/device/response' }
);
console.log("Request sent!");
// Receiver (in separate application) // Receiver (in separate application)
// const msg = await natsConsumer.next(); // const msg = await natsConsumer.next();
// const env = await msghandler.smartunpack(msg); // const env = await msghandler.smartunpack(msg);
// Process request and send response // console.log("Received command:", env.payloads);
// const response_env, response_json = await msghandler.smartpack( // const response_env, response_json = await msghandler.smartpack(
// "/device/response", // "/device/response",
// [["result", { value: 42 }, "dictionary"]], // [["result", { value: 42 }, "dictionary"]],
@@ -721,29 +876,20 @@ const [env, env_json_str] = await msghandler.smartpack(
// ); // );
``` ```
#### Python #### MicroPython
```python ```python
from msghandler import msghandler from msghandler import smartpack
# Requester # Requester
env, env_json_str = await msghandler.smartpack( env, env_json_str = smartpack(
"/device/command", "/device/command",
[("command", {"action": "read_sensor"}, "dictionary")], [("command", {"action": "read_sensor"}, "dictionary")],
broker_url="nats://localhost:4222", broker_url="nats://localhost:4222",
reply_to="/device/response" reply_to="/device/response",
size_threshold=100000
) )
print("Request sent!")
# Receiver (in separate application)
# msg = await nats_consumer.next()
# env = await msghandler.smartunpack(msg)
# Process request and send response
# response_env, response_json = await msghandler.smartpack(
# "/device/response",
# [("result", {"value": 42}, "dictionary")],
# reply_to="/device/command",
# reply_to_msg_id=env["msg_id"]
# )
``` ```
--- ---