chore(examples): add mqtt example (#523)
Signed-off-by: Xavier Serrano <zombispormedio007@gmail.com>
This commit is contained in:
parent
64e527c120
commit
b374d9ac33
|
|
@ -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
|
||||
```
|
||||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
});
|
||||
|
|
@ -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
|
||||
}
|
||||
Loading…
Reference in New Issue