You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -6,48 +6,31 @@ Build MQTT applications with an Elysia-like API: compose broker adapters, ordere
6
6
7
7
mqttkit does not reimplement the MQTT protocol. A broker such as Aedes owns CONNECT, SUBSCRIBE, PUBLISH, QoS, retain, sessions, persistence, and MQTT-over-WebSocket. mqttkit adds an application framework layer on top.
8
8
9
+
Full documentation: **<https://mqttkit.keyp.dev>** ([简体中文](https://mqttkit.keyp.dev/zh/))
10
+
9
11
## Features
10
12
11
13
- Ordered `use()` middleware for auth, logging, audit, validation, and interception.
12
-
-`router().topic()` declares MQTT topic routes, publish policy, and subscribe policy.
13
-
-`ctx.params` extracts topic params such as `devices/:uid/events`.
14
-
-`ctx.body` carries the validated payload — any [Standard Schema](https://standardschema.dev/) validator (zod, valibot, arktype, …) works, with full type inference. Use `@mqttkit/typebox` / `@mqttkit/zod` for raw TypeBox or for attaching JSON Schema.
15
-
-`ctx.services` injects Kafka, Redis, database, audit, or domain services.
16
-
-`app.request()` does MQTT 5 RPC over `responseTopic` + `correlationData`; `ctx.reply()` answers on the device side.
17
-
-`app.onError()` (and per-route `onError`) capture validation, handler, policy, middleware and publish failures with a structured `phase`.
18
-
-`app.on()` observes broker lifecycle events such as client, publish, subscribe, ack, and errors.
19
-
-`app.publish()` lets services, workers, and consumers push messages to MQTT clients through the broker.
20
-
-`@mqttkit/core/testing` ships an in-memory `TestBroker` so apps can be unit-tested without spawning aedes.
21
-
-`@mqttkit/aedes` provides TCP MQTT and MQTT-over-WebSocket support through Aedes (with MQTT 5 properties forwarded for RPC).
22
-
-`@mqttkit/asyncapi` generates AsyncAPI 3.0 documentation from routes and serves a browsable docs page.
23
-
- Built for Bun and TypeScript.
14
+
-`router().topic()` declares MQTT topic routes with publish / subscribe policies.
15
+
- Topic params (`devices/:uid/events`), payload validation via any [Standard Schema](https://standardschema.dev/) validator, and injected services on `ctx`.
16
+
- MQTT 5 RPC with `app.request()` and `ctx.reply()`.
MQTT has topics, not HTTP methods. mqttkit uses `topic(pattern, config)` to declare a route, publish policy, subscribe policy, and optional inbound message handler.
100
-
101
-
```ts
102
-
router()
103
-
.topic('devices/:uid/events', {
104
-
publish: ({ params, principal }) =>params.uid===principal?.uid,
ACK is produced by the broker according to MQTT packet lifecycle. mqttkit does not define a custom JSON ack.
181
-
182
-
## Service Push
183
-
184
-
Any external service, timer, worker, queue consumer, or Kafka consumer can call `app.publish()` to send messages into the broker and let the broker deliver them to subscribed MQTT clients.
185
-
186
-
```text
187
-
external service / worker / consumer
188
-
-> app.publish(topic, payload, options)
189
-
-> @mqttkit/aedes adapter
190
-
-> Aedes broker
191
-
-> subscribed MQTT clients
192
-
```
193
-
194
-
Declare a server-owned topic that clients may subscribe to but may not publish to:
Kafka, databases, and other services are injected with `decorate()`. MQTT -> Kafka happens inside `onMessage`; Kafka -> MQTT happens when a consumer calls `app.publish()`.
62
+
`topic({ schema })` accepts any [Standard Schema](https://standardschema.dev/) validator (zod, valibot, arktype, …). The validated payload is exposed on `ctx.body` with full type inference.
Use [`@mqttkit/typebox`](https://mqttkit.keyp.dev/schema) for raw TypeBox or [`@mqttkit/zod`](https://mqttkit.keyp.dev/schema) to attach a JSON Schema so `@mqttkit/asyncapi` can emit the full payload.
250
76
251
-
`@mqttkit/aedes` supports:
77
+
See the [Getting Started guide](https://mqttkit.keyp.dev/getting-started) for routers, middleware, events, RPC, Kafka bridging, and more.
252
78
253
-
- Creating an Aedes instance for mqttkit.
254
-
- Using an externally created Aedes instance.
255
-
- Starting a TCP MQTT server.
256
-
- Starting a standard MQTT-over-WebSocket server.
257
-
- Passing Aedes `persistence` through.
258
-
- Forwarding Aedes lifecycle events to `app.on()`.
259
-
- Returning `principal` from `authenticate` and injecting it into policies and handler context.
260
-
- Delegating publish / subscribe authorization to mqttkit route policies.
261
-
262
-
```ts
263
-
import { aedes } from'@mqttkit/aedes'
264
-
265
-
newMqttApp()
266
-
.use(
267
-
aedes({
268
-
tcp: { port: 1883 },
269
-
ws: { port: 8888, path: '/mqtt' },
270
-
authenticate({ username }) {
271
-
if (!username) returnfalse
272
-
return { uid: username }
273
-
},
274
-
}),
275
-
)
276
-
```
79
+
## Packages
277
80
278
-
Use Aedes persistence adapters for offline queues, retain, QoS sessions, and durable storage. mqttkit only owns the application layer.
-`@mqttkit/aedes`: Aedes adapter for TCP MQTT and MQTT-over-WebSocket (forwards MQTT 5 properties for RPC).
83
+
-`@mqttkit/asyncapi`: AsyncAPI 3.0 generator and HTTP plugin for browsable docs.
84
+
-`@mqttkit/typebox`: TypeBox schema provider — register once, then pass raw `Type.X(...)` schemas directly.
85
+
-`@mqttkit/zod`: zod helper that attaches a JSON Schema representation so AsyncAPI documents the full payload.
279
86
280
87
## Examples
281
88
282
-
-`examples/aedes-basic`: TCP broker, authentication, middleware, topic routes, and server-side publish.
283
-
-`examples/aedes-ws`: standard MQTT-over-WebSocket; mqtt.js connects to `ws://localhost:8888/mqtt`.
284
-
-`examples/events`: broker lifecycle events.
285
-
-`examples/service-push`: external service calls `app.publish()`, then Aedes delivers to subscribed MQTT clients.
286
-
-`examples/kafka-bridge`: MQTT messages go to Kafka, and Kafka consumer messages are forwarded to MQTT clients.
287
-
-`examples/schema-validation`: payload schemas with zod, TypeBox, and coexistence between the two.
288
-
-`examples/rpc`: MQTT 5 RPC round-trip with `app.request()` and `ctx.reply()`.
289
-
-`examples/asyncapi-docs`: AsyncAPI documentation served from `@mqttkit/asyncapi` (standalone HTTP). `dev:zod` variant shows zod + `jsonify` in the doc.
290
-
-`examples/asyncapi-elysia`: share a single Elysia port for both AsyncAPI docs and MQTT-over-WebSocket (aedes ws attached to the same `http.Server`).
291
-
292
-
Run an example:
89
+
Runnable examples under [`examples/`](./examples) cover the basic TCP / WebSocket broker, lifecycle events, service push, Kafka bridge, schema validation, MQTT 5 RPC, and AsyncAPI docs (standalone HTTP or shared Elysia port).
0 commit comments