Skip to content

Commit

Permalink
chore(examples): add mqtt example (#523)
Browse files Browse the repository at this point in the history
Signed-off-by: Xavier Serrano <zombispormedio007@gmail.com>
  • Loading branch information
zombispormedio authored Feb 6, 2023
1 parent 64e527c commit b374d9a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/mqtt-ex/README.md
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
```
35 changes: 35 additions & 0 deletions examples/mqtt-ex/package.json
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"
}
}
35 changes: 35 additions & 0 deletions examples/mqtt-ex/src/index.ts
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();
});
16 changes: 16 additions & 0 deletions examples/mqtt-ex/tsconfig.json
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
}

0 comments on commit b374d9a

Please sign in to comment.