initial commit: working mqtt example
This commit is contained in:
28
.eslintrc
Normal file
28
.eslintrc
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 8,
|
||||
"sourceType": "module"
|
||||
},
|
||||
"extends": [
|
||||
"airbnb",
|
||||
"prettier"
|
||||
],
|
||||
"plugins": [
|
||||
"prettier",
|
||||
"import"
|
||||
],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"import/no-extraneous-dependencies": [
|
||||
"error", {
|
||||
"devDependencies": [
|
||||
"test/**",
|
||||
"packages/**/test/**",
|
||||
"packages/**/*.test.js"
|
||||
],
|
||||
"optionalDependencies": false,
|
||||
"peerDependencies": false
|
||||
}
|
||||
],
|
||||
}
|
||||
}
|
||||
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
node_modules
|
||||
npm-debug.log
|
||||
yarn-error.log
|
||||
.DS_Store
|
||||
.*.swp
|
||||
coverage
|
||||
.nyc_output
|
||||
coverage.lcov
|
||||
lib/
|
||||
db/
|
||||
1
.node-version
Normal file
1
.node-version
Normal file
@@ -0,0 +1 @@
|
||||
8
|
||||
8
.npmignore
Normal file
8
.npmignore
Normal file
@@ -0,0 +1,8 @@
|
||||
.DS_Store
|
||||
.*.swp
|
||||
yarn.lock
|
||||
coverage
|
||||
.nyc_output
|
||||
__tests__/
|
||||
*.test.js
|
||||
*.test.jsx
|
||||
20
.travis.yml
Normal file
20
.travis.yml
Normal file
@@ -0,0 +1,20 @@
|
||||
language: node_js
|
||||
|
||||
node_js:
|
||||
- "stable"
|
||||
- "8"
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- node_modules
|
||||
|
||||
notifications:
|
||||
email: false
|
||||
|
||||
branches:
|
||||
only:
|
||||
- master
|
||||
- /^v[0-9].*$/
|
||||
|
||||
after_success:
|
||||
- npm run report-coverage -- -e TRAVIS_NODE_VERSION
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2018 Joe Fleming (https://github.com/w33ble)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
22
README.md
Normal file
22
README.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# mosca-playground
|
||||
|
||||
Mosca MQTT Playground.
|
||||
|
||||
[](https://raw.githubusercontent.com/w33ble/mosca-playground/master/LICENSE)
|
||||
[](https://travis-ci.org/w33ble/mosca-playground)
|
||||
[](https://codecov.io/gh/w33ble/mosca-playground)
|
||||
[](https://nodejs.org/api/documentation.html#documentation_stability_index)
|
||||
|
||||
## Usage
|
||||
|
||||
`npm install && node .`
|
||||
|
||||
This will create an mqtt server, publish a message, and log some output about the process.
|
||||
|
||||
Modify information about the message in `src/index.mjs`.
|
||||
|
||||
To see what's in the persisted store, run `node dump.js`.
|
||||
|
||||
#### License
|
||||
|
||||
MIT © [w33ble](https://github.com/w33ble)
|
||||
12
dump.js
Normal file
12
dump.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const level = require('level');
|
||||
|
||||
const db = level('db');
|
||||
|
||||
db
|
||||
.createReadStream()
|
||||
.on('data', data => {
|
||||
console.log(data.key, '=', data.value);
|
||||
})
|
||||
.on('end', () => {
|
||||
console.log('--- DONE');
|
||||
});
|
||||
5
index.js
Normal file
5
index.js
Normal file
@@ -0,0 +1,5 @@
|
||||
/* eslint no-global-assign: 0 */
|
||||
require = require('@std/esm')(module);
|
||||
const mod = require('./src/index.mjs').default;
|
||||
|
||||
module.exports = mod.default;
|
||||
3
index.mjs
Normal file
3
index.mjs
Normal file
@@ -0,0 +1,3 @@
|
||||
import mod from './src/index.mjs';
|
||||
|
||||
export default mod;
|
||||
60
package.json
Normal file
60
package.json
Normal file
@@ -0,0 +1,60 @@
|
||||
{
|
||||
"name": "mosca-playground",
|
||||
"version": "0.0.0",
|
||||
"description": "Another fine project",
|
||||
"module": "index.mjs",
|
||||
"scripts": {
|
||||
"lint": "eslint *.js src/**/*.js",
|
||||
"report-coverage": "nyc report --reporter=lcov > coverage.lcov && codecov",
|
||||
"test": "npm run lint && nyc ava",
|
||||
"test:only": "ava",
|
||||
"test:dev": "ava --watch",
|
||||
"start": "node .",
|
||||
"dev": "nodemon ."
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/w33ble/mosca-playground.git"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Joe Fleming (https://github.com/w33ble)",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/w33ble/mosca-playground/issues"
|
||||
},
|
||||
"homepage": "https://github.com/w33ble/mosca-playground",
|
||||
"lint-staged": {
|
||||
"*.{js,json,css}": [
|
||||
"prettier --write"
|
||||
]
|
||||
},
|
||||
"prettier": {
|
||||
"printWidth": 100,
|
||||
"singleQuote": true,
|
||||
"trailingComma": "es5"
|
||||
},
|
||||
"@std/esm": {
|
||||
"cjs": true
|
||||
},
|
||||
"dependencies": {
|
||||
"@std/esm": "^0.18.0",
|
||||
"level": "^1.7.0",
|
||||
"mosca": "^2.7.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ava": "^0.23.0",
|
||||
"codecov": "^3.0.0",
|
||||
"eslint": "^4.9.0",
|
||||
"eslint-config-airbnb": "^16.1.0",
|
||||
"eslint-config-prettier": "^2.9.0",
|
||||
"eslint-plugin-import": "^2.7.0",
|
||||
"eslint-plugin-jsx-a11y": "^6.0.2",
|
||||
"eslint-plugin-prettier": "^2.3.1",
|
||||
"eslint-plugin-react": "^7.1.0",
|
||||
"husky": "^0.14.3",
|
||||
"lint-staged": "^6.0.0",
|
||||
"nodemon": "^1.14.7",
|
||||
"nyc": "^11.2.1",
|
||||
"prettier": "^1.9.0"
|
||||
}
|
||||
}
|
||||
41
src/index.mjs
Normal file
41
src/index.mjs
Normal file
@@ -0,0 +1,41 @@
|
||||
import mosca from 'mosca';
|
||||
|
||||
// source: https://github.com/mcollina/mosca/wiki/Mosca-basic-usage
|
||||
// source: https://github.com/mcollina/mosca/wiki/Persistence-support
|
||||
|
||||
const moscaSettings = {
|
||||
port: 1883,
|
||||
};
|
||||
|
||||
const server = new mosca.Server(moscaSettings);
|
||||
|
||||
// set up the persistence layer
|
||||
const db = new mosca.persistence.LevelUp({
|
||||
path: 'db',
|
||||
});
|
||||
|
||||
db.wire(server);
|
||||
|
||||
const setup = () => {
|
||||
console.log('Mosca server is up and running');
|
||||
|
||||
const message = {
|
||||
topic: '/hello/world',
|
||||
payload: 'abdce', // or a Buffer
|
||||
qos: 0, // 0, 1, or 2
|
||||
retain: true, // retain message for future subscribers
|
||||
};
|
||||
|
||||
server.publish(message, () => {
|
||||
console.log('message published!');
|
||||
});
|
||||
};
|
||||
|
||||
server.on('ready', setup);
|
||||
|
||||
// fired when a message is published
|
||||
server.on('published', packet => {
|
||||
console.log('published event', packet.retain ? '(persisted)' : '', packet.topic, packet.payload);
|
||||
});
|
||||
|
||||
export default server;
|
||||
Reference in New Issue
Block a user