-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(examples): add mqtt example (#523)
Signed-off-by: Xavier Serrano <zombispormedio007@gmail.com>
- Loading branch information
1 parent
64e527c
commit b374d9a
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |