Skip to content

Commit b374d9a

Browse files
chore(examples): add mqtt example (cloudevents#523)
Signed-off-by: Xavier Serrano <zombispormedio007@gmail.com>
1 parent 64e527c commit b374d9a

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

examples/mqtt-ex/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# MQTT Example
2+
3+
The MQTT message protocol are available since v5.3.0
4+
5+
## How To Start
6+
7+
Install and compile:
8+
9+
```bash
10+
npm install
11+
npm run compile
12+
```
13+
14+
Start a MQTT broker using Docker:
15+
16+
```bash
17+
docker run -it -d -p 1883:1883 eclipse-mosquitto:2.0 mosquitto -c /mosquitto-no-auth.conf
18+
```
19+
20+
Then, start
21+
22+
```bash
23+
npm start
24+
```

examples/mqtt-ex/package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "mqtt-ex",
3+
"version": "1.0.0",
4+
"description": "Simple mqtt example using CloudEvents types",
5+
"repository": "https://github.com/cloudevents/sdk-javascript.git",
6+
"main": "build/src/index.js",
7+
"types": "build/src/index.d.ts",
8+
"files": [
9+
"build/src"
10+
],
11+
"license": "Apache-2.0",
12+
"keywords": [],
13+
"scripts": {
14+
"start": "node build/index.js",
15+
"test": "echo \"Error: no test specified\" && exit 1",
16+
"check": "gts check",
17+
"clean": "gts clean",
18+
"compile": "tsc -p .",
19+
"watch": "tsc -p . --watch",
20+
"fix": "gts fix",
21+
"prepare": "npm run compile",
22+
"pretest": "npm run compile",
23+
"posttest": "npm run check"
24+
},
25+
"devDependencies": {
26+
"@types/node": "^14.14.10",
27+
"@types/ws": "^8.5.4",
28+
"gts": "^3.0.3",
29+
"typescript": "~4.1.3"
30+
},
31+
"dependencies": {
32+
"cloudevents": "^6.0.3",
33+
"mqtt": "^4.3.7"
34+
}
35+
}

examples/mqtt-ex/src/index.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* eslint-disable */
2+
import { CloudEvent, MQTT } from "cloudevents";
3+
import * as mqtt from "mqtt";
4+
5+
const client = mqtt.connect("mqtt://localhost:1883");
6+
7+
client.on("connect", function () {
8+
client.subscribe("presence", function (err) {
9+
if (err) return;
10+
const event = new CloudEvent({
11+
source: "presence",
12+
type: "presence.event",
13+
datacontenttype: "application/json",
14+
data: {
15+
hello: "world",
16+
},
17+
});
18+
const { body, headers } = MQTT.binary(event);
19+
20+
client.publish("presence", JSON.stringify(body), {
21+
properties: {
22+
userProperties: headers as mqtt.UserProperties,
23+
},
24+
});
25+
});
26+
});
27+
28+
client.on("message", function (topic, message, packet) {
29+
const event = MQTT.toEvent({
30+
body: JSON.parse(message.toString()),
31+
headers: packet.properties?.userProperties || {},
32+
});
33+
console.log(event);
34+
client.end();
35+
});

examples/mqtt-ex/tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"extends": "./node_modules/gts/tsconfig-google.json",
3+
"compilerOptions": {
4+
"rootDir": "./src",
5+
"outDir": "./build/",
6+
"lib": [
7+
"es6",
8+
"dom"
9+
]
10+
},
11+
"include": [
12+
"src/**/*.ts",
13+
"test/**/*.ts"
14+
],
15+
"allowJs": true
16+
}

0 commit comments

Comments
 (0)