Backend Development 5 min read

How I Ported Netty’s MQTT Codec to t-io for Full MQTT 5 Support

This article details the process of adapting the Netty MQTT codec to the t‑io framework, covering the migration from MQTT 3.1.1 to MQTT 5, code adjustments, ByteBuf-to-ByteBuffer conversion, custom WriteBuffer handling, and the resulting client demonstrations.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
How I Ported Netty’s MQTT Codec to t-io for Full MQTT 5 Support

1. Introduction

MQTT (Message Queuing Telemetry Transport) is a lightweight publish/subscribe messaging protocol released by IBM. The widely used versions are MQTT 3.1.1 (2014) and MQTT 5.0 (2018). Details of MQTT 5 features can be found at https://www.emqx.io/cn/mqtt/mqtt5.

2. mica-mqtt

To deepen understanding of MQTT, a project called mica-mqtt was built around September using t‑io. The MQTT encoding/decoding is based on Netty’s codec‑mqtt module, which at that time did not support MQTT 5. Netty’s codec‑mqtt is mature and used by many Java MQTT brokers. This article explains how the codec was ported to t‑io.

3. Migrating to MQTT 5

Two weeks ago Netty’s codec‑mqtt added MQTT 5 support, and I upgraded the code accordingly. The following steps show how it was done.

3.1 Copy source code

ObjectUtil.checkNotNull

replaced with

Objects.requireNonNull

.

CharsetUtil.UTF_8

changed to

StandardCharsets.UTF_8

.

Reimplemented the

toString

method.

3.2 ByteBuf to ByteBuffer conversion

Netty parses protocols with

ByteBuf

, while t‑io uses

ByteBuffer

. The main task is converting between them. The method differences are:

writeByte

put
writeBytes

put
writeShort

putShort
writeInt

putInt
getByte

get
getBytes

get
getShort

getShort
getInt

getInt

3.3 Handling mqtt5 encodeProperties

Because

ByteBuffer

has a fixed length, I created a custom

WriteBuffer

to hold data during encoding, which is finally converted to a

byte[]

. The underlying implementation uses t‑io’s

FastByteBuffer

authored by biezhi.

4. Results (MQTT 5)

4.1 Using MQTTX client

4.2 Using mica-mqtt client

5. TODO

mica‑mqtt is currently experimental; contributions are welcome.

Bind topics.

Implement basic MQTT broker functionality.

Reference: ZBus can be used for messaging or internal clustering.

6. About t‑io

t‑io is a simple, high‑performance, low‑latency network middle‑platform that addresses pain points not solved by other frameworks, helping enterprises save labor costs.

Technical whitepaper: https://www.tiocloud.com/tio.pdf

t‑io IM (TanLiao) has a 14 k user group; join us!

Product brochure: https://www.tiocloud.com/tioim.pdf

7. Related Links

mica‑mqtt: https://gitee.com/596392912/mica-mqtt

t‑io: https://gitee.com/tywo45/t-io

BackendJavaNettyprotocolMQTTt-io
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.