diff --git a/examples/mqtt-ex/README.md b/examples/mqtt-ex/README.md new file mode 100644 index 00000000..bb097c35 --- /dev/null +++ b/examples/mqtt-ex/README.md @@ -0,0 +1,24 @@ +# MQTT Example + +The MQTT message protocol are available since v5.3.0 + +## How To Start + +Install and compile: + +```bash +npm install +npm run compile +``` + +Start a MQTT broker using Docker: + +```bash +docker run -it -d -p 1883:1883 eclipse-mosquitto:2.0 mosquitto -c /mosquitto-no-auth.conf +``` + +Then, start + +```bash +npm start +``` diff --git a/examples/mqtt-ex/package.json b/examples/mqtt-ex/package.json new file mode 100644 index 00000000..261173eb --- /dev/null +++ b/examples/mqtt-ex/package.json @@ -0,0 +1,35 @@ +{ + "name": "mqtt-ex", + "version": "1.0.0", + "description": "Simple mqtt example using CloudEvents types", + "repository": "https://github.com/cloudevents/sdk-javascript.git", + "main": "build/src/index.js", + "types": "build/src/index.d.ts", + "files": [ + "build/src" + ], + "license": "Apache-2.0", + "keywords": [], + "scripts": { + "start": "node build/index.js", + "test": "echo \"Error: no test specified\" && exit 1", + "check": "gts check", + "clean": "gts clean", + "compile": "tsc -p .", + "watch": "tsc -p . --watch", + "fix": "gts fix", + "prepare": "npm run compile", + "pretest": "npm run compile", + "posttest": "npm run check" + }, + "devDependencies": { + "@types/node": "^14.14.10", + "@types/ws": "^8.5.4", + "gts": "^3.0.3", + "typescript": "~4.1.3" + }, + "dependencies": { + "cloudevents": "^6.0.3", + "mqtt": "^4.3.7" + } +} diff --git a/examples/mqtt-ex/src/index.ts b/examples/mqtt-ex/src/index.ts new file mode 100644 index 00000000..934bfdba --- /dev/null +++ b/examples/mqtt-ex/src/index.ts @@ -0,0 +1,35 @@ +/* eslint-disable */ +import { CloudEvent, MQTT } from "cloudevents"; +import * as mqtt from "mqtt"; + +const client = mqtt.connect("mqtt://localhost:1883"); + +client.on("connect", function () { + client.subscribe("presence", function (err) { + if (err) return; + const event = new CloudEvent({ + source: "presence", + type: "presence.event", + datacontenttype: "application/json", + data: { + hello: "world", + }, + }); + const { body, headers } = MQTT.binary(event); + + client.publish("presence", JSON.stringify(body), { + properties: { + userProperties: headers as mqtt.UserProperties, + }, + }); + }); +}); + +client.on("message", function (topic, message, packet) { + const event = MQTT.toEvent({ + body: JSON.parse(message.toString()), + headers: packet.properties?.userProperties || {}, + }); + console.log(event); + client.end(); +}); diff --git a/examples/mqtt-ex/tsconfig.json b/examples/mqtt-ex/tsconfig.json new file mode 100644 index 00000000..c4f3c0e3 --- /dev/null +++ b/examples/mqtt-ex/tsconfig.json @@ -0,0 +1,16 @@ +{ + "extends": "./node_modules/gts/tsconfig-google.json", + "compilerOptions": { + "rootDir": "./src", + "outDir": "./build/", + "lib": [ + "es6", + "dom" + ] + }, + "include": [ + "src/**/*.ts", + "test/**/*.ts" + ], + "allowJs": true +}