← Back to Official Package Registry
Tier 6: Local-First, IoT, Web & Media • MQTT 5.0, Modbus TCP & mDNSv1.0.0 • 73 KB • Ed25519 Verified

iot_mqtt@1.0.0

MQTT 3.1.1/5.0 binary codec & embeddable broker (+/# wildcards, retained, Wills), industrial Modbus TCP registers, and mDNS/DNS-SD discovery.

use [iot_mqtt/mdns]use [iot_mqtt]use [iot_mqtt/modbus]
veko pkg add iot_mqtt@1.0.0
Run in Browser PlaygroundDownload 1.0.0.vkpRaw demo.vk

iot_mqtt

Pure-Veko OASIS MQTT 3.1.1 & MQTT 5.0 wire codec and embeddable broker, industrial Modbus TCP MBAP/PDU simulator, and RFC 6762 / RFC 6763 mDNS & DNS-SD .local service discovery (iot_mqtt, iot_mqtt/modbus, iot_mqtt/mdns).

Overview

iot_mqtt provides zero-dependency binary telemetry and industrial edge protocols in pure Veko:

  • iot_mqtt:
  • MQTT 3.1.1 (level = 4) and MQTT 5.0 (level = 5) binary framing: 1..4 byte Variable Byte Integer remaining-length codec, length-prefixed UTF-8 strings, and MQTT 5.0 Properties (Message-Expiry-Interval, Content-Type, Response-Topic, Correlation-Data, User-Property).
  • Full packet encoders and decoders for CONNECT, CONNACK, PUBLISH (QoS 0/1/2, retain, dup), PUBACK, SUBSCRIBE, SUBACK, UNSUBSCRIBE, PINGREQ, PINGRESP, and DISCONNECT.
  • OASIS-compliant topic wildcard matcher (topic-matches? supporting +, #, and §4.7.2 $SYS leading-$ isolation) plus an embeddable deterministic MqttBroker state machine (subscriptions, per-topic retained messages, QoS 1 in-flight ACK tracking, and Last Will delivery).
  • iot_mqtt/modbus:
  • Industrial Modbus TCP 7-byte MBAP header and PDU codec supporting 0x01 Read Coils, 0x03 Read Holding Registers, 0x04 Read Input Registers, 0x05 Write Single Coil, 0x06 Write Single Register, 0x10 Write Multiple Registers, and 0x80 | fc exception responses, backed by an in-memory RegisterBank PLC server handler.
  • iot_mqtt/mdns:
  • RFC 6762 / RFC 6763 mDNS & DNS-SD .local multicast query and service announcement packet builder (PTR + SRV + TXT + A with RFC 1035 0xC0 label compression) and pointer-decompressing parser.

Installation

powershell
veko pkg add iot_mqtt@1.0.0

Import the modules in .vk source files:

rebol
use [
    mqtt   = iot_mqtt
    modbus = iot_mqtt/modbus
    mdns   = iot_mqtt/mdns
]

Architecture & Wire Layouts

MQTT 5.0 Fixed & Variable Header Layout

FieldWidthNotes
`(pkt_type << 4) \flags`1 byte
Remaining Length1..4 bytesVariable Byte Integer (7 data bits + 0x80 continuation bit)
Variable HeaderVariableTopic / Packet Identifier + MQTT 5.0 Properties (encode-props)
PayloadRemaining bytesBinary application payload or subscription filter list

Modbus TCP MBAP Header (7 Bytes Big-Endian)

Byte OffsetFieldTypeNotes
0..2Transaction IDuint16 BEEchoed by server to correlate pipelined requests
2..4Protocol IDuint16 BEAlways 0x0000 for Modbus TCP
4..6Lengthuint16 BEByte count of Unit ID (1B) + PDU
6Unit IDuint8Slave / bridge unit identifier

Complete API Reference

iot_mqtt

  • Structs: MqttProps, TopicSub, MqttPacket, RetainedMsg, BrokerSession, MqttBroker
  • Wire Primitives & Packet Codecs:
  • encode-varint [val i64] bytes / decode-varint [raw bytes, off i64] map
  • encode-utf8 [s str] bytes / decode-utf8 [raw bytes, off i64] map
  • encode-props [props MqttProps] bytes / decode-props [raw bytes, off i64] map
  • encode-connect, encode-connack, encode-publish, encode-puback, encode-subscribe, encode-suback, encode-unsubscribe, encode-pingreq, encode-pingresp, encode-disconnect
  • decode-packet [wire bytes /level proto_level i64 = 5] MqttPacket
  • decode-packet-at [wire bytes, off i64 /level proto_level i64 = 5] map
  • Routing & Broker:
  • topic-matches? [filter str, topic str] bool
  • new-broker [] MqttBroker
  • broker-connect [mut b MqttBroker, conn MqttPacket] MqttPacket
  • broker-subscribe [mut b MqttBroker, client_id str, packet_id i64, subs block[TopicSub]] MqttPacket
  • broker-unsubscribe [mut b MqttBroker, client_id str, topics block[str]] i64
  • broker-publish [mut b MqttBroker, pub_pkt MqttPacket] i64
  • broker-puback [mut b MqttBroker, client_id str, packet_id i64] bool
  • broker-inflight-count [b MqttBroker, client_id str] i64
  • broker-disconnect [mut b MqttBroker, client_id str /unexpected ungraceful bool = false /reason reason_code i64 = 0] i64
  • broker-drain-client [mut b MqttBroker, client_id str] block[MqttPacket]

iot_mqtt/modbus

  • Constants: FC-READ-COILS, FC-READ-HOLDING-REGS, FC-READ-INPUT-REGS, FC-WRITE-SINGLE-COIL, FC-WRITE-SINGLE-REG, FC-WRITE-MULTI-REGS, EX-ILLEGAL-FUNCTION, EX-ILLEGAL-DATA-ADDR, EX-ILLEGAL-DATA-VAL
  • Codecs & PLC Simulator:
  • encode-read-coils, encode-read-holding-registers, encode-read-input-registers, encode-write-single-coil, encode-write-single-register, encode-write-multiple-registers, encode-exception-response
  • decode-request [wire bytes] ModbusRequest / decode-response [wire bytes /expected-coils coil_cnt i64 = -1] ModbusResponse
  • new-register-bank [/coils nc i64 = 256 /holding nh i64 = 256 /input ni i64 = 256] RegisterBank
  • bank-set-input, bank-set-holding, bank-get-holding, handle-modbus-tcp [mut bank RegisterBank, req_wire bytes] bytes

iot_mqtt/mdns

  • Constants: MDNS-IPV4, MDNS-PORT, TYPE-A, TYPE-PTR, TYPE-TXT, TYPE-AAAA, TYPE-SRV, CLASS-IN, CLASS-FLUSH
  • Functions:
  • build-ptr-query [service_type str /unicast-response qu bool = false] bytes
  • build-service-announcement [instance_name str, service_type str, host_target str, ipv4 str, port i64, txt_kv map[str] /ttl ttl_sec i64 = 120] bytes
  • decode-dns-name [wire bytes, start_off i64] map
  • parse-mdns-packet [wire bytes] MdnsPacket

Idiomatic Veko Example

rebol
use [mqtt = iot_mqtt, modbus = iot_mqtt/modbus]

// Encode an MQTT 5.0 PUBLISH with User Properties
pkt_wire = mqtt/encode-publish/qos/retain/packet-id/props 'factory/line-1/rpm' (bytes/from-str '1480') 1 true 9 (mqtt/MqttProps {
    content_type = 'text/plain'
    user_props   = { 'unit' = 'rpm' }
})
pkt = mqtt/decode-packet pkt_wire
println pkt.topic

// Execute Modbus TCP Holding Register write + read against RegisterBank
mut plc = modbus/new-register-bank
_ = modbus/handle-modbus-tcp mut plc (modbus/encode-write-multiple-registers 1 1 0 [1480 725])
resp = modbus/decode-response (modbus/handle-modbus-tcp mut plc (modbus/encode-read-holding-registers 2 1 0 2))
println resp.registers // [1480 725]
packages/iot_mqtt/examples/demo.vkSelf-contained runnable example & assertion suite
Raw .vk
// packages/iot_mqtt/examples/demo.vk
// Self-contained verification suite & runnable example for `iot_mqtt`, `iot_mqtt/modbus`, and `iot_mqtt/mdns`.

use [
	mqtt   = iot_mqtt
	modbus = iot_mqtt/modbus
	mdns   = iot_mqtt/mdns
]

assert-true = fn [cond bool, label str] enum [
	if not cond [
		println (str/join ['[FAIL] ' label] '')
		os/exit 1
	]
	println (str/join ['[PASS] ' label] '')
	.ok
]

// --- 1. MQTT 5.0 Variable-Byte Framing, Properties & Control Packet Codec ---
v_b = mqtt/encode-varint 2097152
v_d = mqtt/decode-varint v_b 0
_ = assert-true ((len v_b) == 4 and v_d.value == 2097152) 'MQTT 4-byte Variable Byte Integer boundary roundtrip'

props_v5 = mqtt/MqttProps {
	message_expiry_sec = 300
	content_type       = 'application/json'
	response_topic     = 'cmd/plc-7/ack'
	correlation_data   = (bytes/from-str 'corr-9001')
	user_props         = { 'site' = 'plant-eu', 'fw' = '2.4.1' }
}

conn_wire = mqtt/encode-connect/user/pass/will-topic/will-payload/will-qos/will-retain/props 'sensor-plc-7' 'device' (bytes/from-str 's3cr3t') 'status/plc-7' (bytes/from-str 'offline') 1 true props_v5
dec_conn = mqtt/decode-packet conn_wire

pub_wire = mqtt/encode-publish/qos/retain/packet-id/props 'telemetry/plant-eu/plc-7/temp' (bytes/from-str '{"c":72.4}') 1 true 42 props_v5
dec_pub = mqtt/decode-packet pub_wire

sub_wire = mqtt/encode-subscribe 101 [
	mqtt/TopicSub { filter = 'telemetry/+/plc-7/#', qos = 1 }
	mqtt/TopicSub { filter = '$SYS/broker/uptime',  qos = 0 }
]
dec_sub = mqtt/decode-packet sub_wire
ack_wire = mqtt/encode-puback/reason 42 0x00
dec_ack = mqtt/decode-packet ack_wire
ping_ok = (mqtt/decode-packet (mqtt/encode-pingreq)).pkt_type == 12 and (mqtt/decode-packet (mqtt/encode-pingresp)).pkt_type == 13
disc_ok = (mqtt/decode-packet (mqtt/encode-disconnect/reason 0x04)).reason_code == 4

pkt_ok_1 = dec_conn.pkt_type == 1 and dec_conn.proto_level == 5 and dec_conn.client_id == 'sensor-plc-7' and dec_conn.has_will and dec_conn.will_topic == 'status/plc-7'
pkt_ok_2 = dec_pub.pkt_type == 3 and dec_pub.qos == 1 and dec_pub.retain and dec_pub.packet_id == 42 and (bytes/to-str dec_pub.payload) == '{"c":72.4}'
pkt_ok_3 = dec_pub.props.message_expiry_sec == 300 and dec_pub.props.content_type == 'application/json' and (map/get dec_pub.props.user_props 'site') == 'plant-eu'
pkt_ok_4 = (len dec_sub.subscriptions) == 2 and dec_ack.packet_id == 42 and ping_ok and disc_ok
_ = assert-true (pkt_ok_1 and pkt_ok_2 and pkt_ok_3 and pkt_ok_4) 'MQTT 5.0 CONNECT, PUBLISH (with User Properties), SUBSCRIBE, PUBACK, PING, and DISCONNECT codec'

// --- 2. OASIS MQTT Topic Wildcard Matcher (`+`, `#`, and `$SYS` Rule) ---
tm_1 = (mqtt/topic-matches? 'sensors/+/temp' 'sensors/kiln-1/temp') and (not (mqtt/topic-matches? 'sensors/+/temp' 'sensors/kiln-1/zone-a/temp'))
tm_2 = (mqtt/topic-matches? 'sensors/kiln-1/#' 'sensors/kiln-1') and (mqtt/topic-matches? 'sensors/kiln-1/#' 'sensors/kiln-1/zone-a/temp')
tm_3 = (not (mqtt/topic-matches? '#' '$SYS/broker/uptime')) and (not (mqtt/topic-matches? '+/broker/uptime' '$SYS/broker/uptime'))
tm_4 = mqtt/topic-matches? '$SYS/+/uptime' '$SYS/broker/uptime'
_ = assert-true (tm_1 and tm_2 and tm_3 and tm_4) 'MQTT wildcard matcher (+ single-level, # multi-level, and OASIS $SYS leading-$ isolation)'

// --- 3. Embeddable `MqttBroker`: Retained Messages, QoS 1 In-Flight ACKs & Last Will ---
mut broker = mqtt/new-broker
cack_pub = mqtt/broker-connect mut broker dec_conn
_ = mqtt/broker-publish mut broker dec_pub // retained QoS 1 message published BEFORE subscriber joins!

dash_conn = mqtt/decode-packet (mqtt/encode-connect 'scada-dashboard')
_ = mqtt/broker-connect mut broker dash_conn
suback = mqtt/broker-subscribe mut broker 'scada-dashboard' 77 [
	mqtt/TopicSub { filter = 'telemetry/plant-eu/#', qos = 1 }
	mqtt/TopicSub { filter = 'status/+',             qos = 1 }
]

// Subscriber immediately receives the retained telemetry message with retain=true and QoS 1 inflight tracking
initial_msgs = mqtt/broker-drain-client mut broker 'scada-dashboard'
inflight_before = mqtt/broker-inflight-count broker 'scada-dashboard'
ack_cleared = mqtt/broker-puback mut broker 'scada-dashboard' initial_msgs[0].packet_id
inflight_after = mqtt/broker-inflight-count broker 'scada-dashboard'

// Simulate unexpected TCP drop of `sensor-plc-7` -> broker publishes Last Will ('offline') to `status/plc-7`!
will_fanout = mqtt/broker-disconnect/unexpected mut broker 'sensor-plc-7' true
will_msgs = mqtt/broker-drain-client mut broker 'scada-dashboard'

brk_ok_1 = cack_pub.reason_code == 0 and suback.reason_codes == [1 1] and (len initial_msgs) == 1 and initial_msgs[0].retain
brk_ok_2 = inflight_before == 1 and ack_cleared and inflight_after == 0
brk_ok_3 = will_fanout == 1 and (len will_msgs) == 1 and will_msgs[0].topic == 'status/plc-7' and (bytes/to-str will_msgs[0].payload) == 'offline'
_ = assert-true (brk_ok_1 and brk_ok_2 and brk_ok_3) 'MqttBroker retained message replay, QoS 1 in-flight PUBACK tracking, and Last Will delivery'

// --- 4. Industrial Modbus TCP MBAP + PDU & `RegisterBank` Simulator ---
mut bank = modbus/new-register-bank/coils/holding/input 64 128 64
_ = modbus/bank-set-input mut bank 4 2450 // 245.0 V sensor input

w_multi = modbus/encode-write-multiple-registers 10 1 20 [1500 3200 4095]
r_multi = modbus/decode-response (modbus/handle-modbus-tcp mut bank w_multi)

w_coil = modbus/encode-write-single-coil 11 1 5 true
_ = modbus/handle-modbus-tcp mut bank w_coil

r_hold = modbus/decode-response (modbus/handle-modbus-tcp mut bank (modbus/encode-read-holding-registers 12 1 20 3))
r_inp  = modbus/decode-response (modbus/handle-modbus-tcp mut bank (modbus/encode-read-input-registers 13 1 4 1))
r_coil = modbus/decode-response/expected-coils (modbus/handle-modbus-tcp mut bank (modbus/encode-read-coils 14 1 4 3)) 3
r_exc  = modbus/decode-response (modbus/handle-modbus-tcp mut bank (modbus/encode-read-holding-registers 15 1 200 10))

mb_ok_1 = r_multi.quantity == 3 and r_hold.registers == [1500 3200 4095] and r_inp.registers == [2450]
mb_ok_2 = r_coil.coils == [false true false] and r_exc.is_exception and r_exc.exception_code == (modbus/EX-ILLEGAL-DATA-ADDR)
_ = assert-true (mb_ok_1 and mb_ok_2) 'Modbus TCP (0x01/0x03/0x04/0x05/0x10 + 0x80|fc exception) against RegisterBank'

// --- 5. RFC 6762 / RFC 6763 mDNS & DNS-SD Announcement + Pointer-Compressed Parsing ---
q_pkt = mdns/parse-mdns-packet (mdns/build-ptr-query '_veko._tcp.local')
ann_wire = mdns/build-service-announcement 'edge-gw-01' '_veko._tcp.local' 'edge-gw-01.local' '192.168.10.42' 8883 {
	'txtvers' = '1'
	'proto'   = 'mqtt5'
}
ann = mdns/parse-mdns-packet ann_wire

rec_ptr = ann.answers[0]
rec_srv = ann.answers[1]
rec_txt = ann.answers[2]
rec_a   = ann.answers[3]

mdns_ok_1 = (len q_pkt.questions) == 1 and q_pkt.questions[0].name == '_veko._tcp.local' and q_pkt.questions[0].qtype == (mdns/TYPE-PTR)
mdns_ok_2 = ann.is_response and (len ann.answers) == 4 and rec_ptr.ptr_target == 'edge-gw-01._veko._tcp.local'
mdns_ok_3 = rec_srv.srv_port == 8883 and rec_srv.srv_target == 'edge-gw-01.local' and rec_srv.cache_flush
mdns_ok_4 = (map/get rec_txt.txt_map 'proto') == 'mqtt5' and rec_a.ipv4 == '192.168.10.42'
_ = assert-true (mdns_ok_1 and mdns_ok_2 and mdns_ok_3 and mdns_ok_4) 'mDNS / DNS-SD _veko._tcp.local PTR/SRV/TXT/A announcement and RFC 1035 pointer decompression'

println 'iot_mqtt: all verification checks passed'
Cryptographic Provenance

Ed25519 Signature & Merkle Integrity

Registry URL: https://veko-lang.org/pkg/iot_mqtt/1.0.0.vkp
Archive SHA-256: 5ebe400293532c843b80979d08c16c2517cd4737b2ccf9b219156bcdd63e70fc
Merkle Root: e671c3cd326d1fc62e232e790059d1b646b76e572ab398b109a1775c0d163d1b
Signer Trust Key: ed25519:27406074d8681da1d4e6420f5d887cac15c313334c249ac047ba9d4e2df72b70
Embedded VKP1 Manifest

manifest.json

{"name":"iot_mqtt","version":"1.0.0","tier":6.0,"tier_label":"Tier 6: Local-First, IoT, Web & Media","category":"MQTT 5.0, Modbus TCP & mDNS","description":"MQTT 3.1.1/5.0 binary codec & embeddable broker (+/# wildcards, retained, Wills), industrial Modbus TCP registers, and mDNS/DNS-SD discovery.","modules":["iot_mqtt/mdns","iot_mqtt","iot_mqtt/modbus"],"homepage":"https://veko-lang.org/packages/iot_mqtt"}
Bundle Entry PathSizeLeaf SHA-256 Digest
README.md6 KB80d5bd011d65557a7c4aea9ada578a17c55fde93aa9bc0dbb5d95c2c81e23d4a
examples/demo.vk7 KBe31f53ac3f864ce579e49b60e09c0e9bc9ceb20fa6ba81bfe545c3a3cee57acc
mdns.vk11 KB610d9f7b9f2904234e016542d9a3b33078ba736d6f2caa5e4d1a545f688c3fe8
mod.vk33 KBf92327d2a01f5e63627a668193fa779f4d97de34cd6846a3e22ab957fa0a7fbe
modbus.vk14 KB276eb6ef3358153a15c1fbad7d565963fded41a602b3104cf5df78a8ae04b091