Opifex aims to provide a MQTT server and MQTT client in Typescript to be used with NodeJS, Deno or Bun It has no third party dependencies, it only relies on built in modules.
The following MQTT versions are supported:
| MQTT 3.1 (v3) | MQTT 3.1.1 (v4) | MQTT 5.0 (v5) | |
|---|---|---|---|
| Packet/encoder | ✅ | ✅ | ✅ |
| Client | ✅ | ✅ | ✅ 1 |
| Server | ❌ | ✅ | ❌ |
1 The client api fully supports MQTTv5, the CLI only supports a subset
Opifex offers plugable persistence, but one can provide its own persistence. (see Architecture)
The following types are currently provided:
| Plugable | Memory | Sqlite | |
|---|---|---|---|
| Client | ✅ | ✅ | ❌ |
| Server | ✅ | ✅ | ✅ |
The easiest way to use this project is to just use the demo server (demoServer) and/or the demo client (mqtt).
If you want to change the behaviour of the server and/or the client beyond what can be done with CLI options then the next step is to clone the demo server and/or the client scripts and modify them to your liking.
If you want to port the platform independent client and server libs to other
types of transports (e.g. Unix sockets or websocketstream) then it's recommended
to clone and modify the platform specific code in /node or /deno as well.
If you want to port the platform independent client and server libs to another
platform then the platform specific code in /node or /deno might serve as
inspiration.
Bun (as of version 1.2) and Deno are both capable of running the NodeJS version,
but for historic reasons /deno still exists.
A simple server example.
import { TcpServer}
import { TcpServer } from "@seriousme/opifex/tcpServer";
const server = new TcpServer({ port: 1883 }, {});
server.start();
console.log(
`MQTT server running on port: ${server.port}, address: ${server.address}`,
);More elaborate examples including client and server, TLS, Websockets and sqlite can be found in the examples folder.
-
The basis of Opifex is the MQTT packet module (mqttPacket/mod.ts) which contains all the logic to encode and decode packets.
-
On top of mqttPacket sits the MQTT connection module (mqttConn/mod.ts) module that reads packets from a Readable stream and writes them to Writable stream. It will take care of incomplete and/or malformed packets. mqttConn provides an async iterable that can be awaited for new packets.
-
On top of mqttConn live the MQTT server (server/mod.ts) and MQTT client (client/mod.ts) that take care of the MQTT protocol handling like requiring an authentication to be successful before another type of packet will be accepted. Both follow a similar model of implementation where for each packet that is received a handler is invoked which then triggers the next step. Server and client are totally independent of the technical implementation of the connection and only need a socketConn (socket) to be able to work.
-
As the server needs to be able to serve multiple clients at the same time, it maintains a context (server/context.ts) per client to keep track of its state and associated timers.
-
Persistence of data is handled by a pluggable persistence module (persistence) which currently offers memory persistence (persistence/memory) and a platform specific sqlite persistence (persistence/sqlite) but can be extended with other database backed persistence supported by third party modules.
-
The demo server listens to a platform specific socket and runs the
serve()method from the server module on the platform independent streams of every connection. -
The demo client opens a platform specific socket and passes the resulting platform independent streams to the client module.
| Export | Description |
|---|---|
| @seriousme/opifex/tcpClient | Exports a MQTT over TCP/TLS client |
| @seriousme/opifex/tcpServer | Exports a MQTT over TCP server |
| @seriousme/opifex/tlsServer | Exports a MQTT over TLS server |
| @seriousme/opifex/wsClient | Exports a MQTT over Websocket client (deno and browser only) |
| @seriousme/opifex/wsServer | Exports a MQTT over Websocket server (deno only) |
| @seriousme/opifex/server | Exports a transport agnostic MQTT server |
| @seriousme/opifex/client | Exports a transport agnostic MQTT client |
| @seriousme/opifex/persistence | Exports an Typescript interface for storage persistence |
| @seriousme/opifex/mqttConn | Exports MQTT connection handling |
| @seriousme/opifex/mqttPacket | Exports MQTT packet handling |
| @seriousme/opifex/utils | Exports various utilities |
The latest API documentation can be found at: https://jsr.io/@seriousme/opifex@latest/doc
Some MQTT servers have names like:
So to stay with the theme: Opifex
Licensed under MIT