Published
Edited
Jul 12, 2020
4 forks
142 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
gateway = "██████████" // Replace this with your Gateway identifier.
Insert cell
Insert cell
Insert cell
counter = "example" // Edit this to change the counter.
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
account = "████████████" // Replace with your AWS account number.
Insert cell
region = "us-east-1" // Replace with your AWS region, as needed.
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
origin = location.origin.replace("observablehq", "████████") // Replace with your origin.
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
increment = {
let queue = [];
let socket;
let timeout;
let delay = 250;

// When forking, replace these as appropriate!
const gateway = "i3c3e2tpt9";
const region = "us-east-1";
const stage = "prod";

(function open() {
socket = new WebSocket(`wss://${gateway}.execute-api.${region}.amazonaws.com/${stage}?id=${encodeURIComponent(counter)}`);
socket.onopen = () => {
delay = 250;
queue.forEach(message => socket.send(message));
queue = null;
};
socket.onclose = () => {
delay *= 2;
timeout = setTimeout(open, delay + Math.random() * delay);
queue = [];
};
socket.onmessage = event => {
const data = JSON.parse(event.data);
if (isNaN(mutable count) || data.count > mutable count) {
mutable count = data.count;
}
};
})();

invalidation.then(() => {
socket.onclose = null;
socket.close();
clearTimeout(timeout);
});

return () => {
const message = JSON.stringify({type: "increment"});
if (queue) queue.push(message);
else socket.send(message);
++mutable count;
};
}
Insert cell
Insert cell
Insert cell
mutable count = counter, NaN
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
stage = "prod" // Replace if you choose a different stage name.
Insert cell
html`<style>

pre {
box-sizing: border-box;
border-left: solid 4px #e2e5e9;
margin-left: -14px;
padding-left: 10px;
}

body {
counter-reset: item;
}

.counter::before {
counter-increment: item;
content: counter(item) ". ";
}

</style>`
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more