Skip to content

Commit

Permalink
snowpack example
Browse files Browse the repository at this point in the history
  • Loading branch information
jdiamond committed May 8, 2021
1 parent d13c5ac commit b4084ff
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/snowpack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/package-lock.json
16 changes: 16 additions & 0 deletions examples/snowpack/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fieldset {
margin: 1em 0;
border: 1px solid lightgray;
padding: 1em;
}

table {
width: 100%;
border-collapse: collapse;
}

th,
td {
border: 1px solid lightgray;
padding: 1em;
}
50 changes: 50 additions & 0 deletions examples/snowpack/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Snowpack Example</title>
<link rel="stylesheet" href="index.css">
<script src="index.js" type="module"></script>
</head>
<body>
<h1>Snowpack Example</h1>

<fieldset>
<label for="urlInput">Host</label>
<input id="urlInput" type="text" value="ws://localhost:9001" />
<button id="connectButton">Connect</button>
<button id="disconnectButton">Disconnect</button>
<span id="connectOutput"></span>
</fieldset>

<fieldset>
<label for="topicFilterInput">Topic Filter</label>
<input id="topicFilterInput" type="text" value="foo/#" />
<button id="subscribeButton">Subscribe</button>
<button id="unsubscribeButton">Unsubscribe</button>
<span id="subscribeOutput"></span>
</fieldset>

<fieldset>
<label for="topicInput">Topic</label>
<input id="topicInput" type="text" value="foo/bar" />
<label for="payloadInput">Payload</label>
<input id="payloadInput" type="text" value="baz" />
<label for="qosInput">QoS</label>
<input id="qosInput" type="number" value="1" />
<button id="publishButton">Publish</button>
<span id="publishOutput"></span>
</fieldset>

<table>
<thead>
<tr>
<th>Topic</th>
<th>Payload</th>
</tr>
</thead>
<tbody id="messagesOutput"></tbody>
</table>
</body>
</html>
83 changes: 83 additions & 0 deletions examples/snowpack/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Client } from '@jdiamond/mqtt-browser';

let client = null;

const textDecoder = new TextDecoder();

const urlInput = document.getElementById('urlInput');
const topicFilterInput = document.getElementById('topicFilterInput');
const topicInput = document.getElementById('topicInput');
const payloadInput = document.getElementById('payloadInput');
const qosInput = document.getElementById('qosInput');

const connectOutput = document.getElementById('connectOutput');
const subscribeOutput = document.getElementById('subscribeOutput');
const publishOutput = document.getElementById('publishOutput');
const messagesOutput = document.getElementById('messagesOutput');

const connectButton = document.getElementById('connectButton');
const disconnectButton = document.getElementById('disconnectButton');
const subscribeButton = document.getElementById('subscribeButton');
const unsubscribeButton = document.getElementById('unsubscribeButton');
const publishButton = document.getElementById('publishButton');

async function connect() {
const url = urlInput.value;

client = new Client({ url });

client.on('message', receiveMessage);

connectOutput.textContent = 'Connecting';

const response = await client.connect();

connectOutput.textContent = JSON.stringify(response);
}

async function disconnect() {
connectOutput.textContent = 'Disconnecting';

await client.disconnect();

connectOutput.textContent = 'Not Connected';
}

async function subscribe() {
const response = await client.subscribe(topicFilterInput.value);

subscribeOutput.textContent = JSON.stringify(response);
}

async function unsubscribe() {
const response = await client.unsubscribe(topicFilterInput.value);

subscribeOutput.textContent = JSON.stringify(response);
}

async function publish() {
const response = await client.publish(topicInput.value, payloadInput.value, {
qos: qosInput.valueAsNumber,
});

console.log(response);

publishOutput.textContent = JSON.stringify(response);
}

function receiveMessage(topic, payload) {
console.log(topic, payload);

const decodedPayload = textDecoder.decode(payload);

const tr = messagesOutput.insertRow();

tr.insertCell().textContent = topic;
tr.insertCell().textContent = decodedPayload;
}

connectButton.addEventListener('click', connect);
disconnectButton.addEventListener('click', disconnect);
subscribeButton.addEventListener('click', subscribe);
unsubscribeButton.addEventListener('click', unsubscribe);
publishButton.addEventListener('click', publish);
20 changes: 20 additions & 0 deletions examples/snowpack/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "mqtt-example-snowpack",
"private": true,
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "snowpack dev",
"build": "snowpack build"
},
"keywords": [],
"author": "Jason Diamond",
"license": "ISC",
"dependencies": {
"@jdiamond/mqtt-browser": "^0.1.2"
},
"devDependencies": {
"snowpack": "^3.3.3"
}
}

0 comments on commit b4084ff

Please sign in to comment.