made with esnextbin
Last active
May 12, 2019 23:00
-
-
Save nha/d48a67bb94b8142220901623cdf4d2ec to your computer and use it in GitHub Desktop.
esnextbin sketch
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
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>GeoDB Demo</title> | |
<link href="https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css" rel="stylesheet"> | |
<style> | |
.topcorner { | |
position:absolute; | |
bottom:0; | |
right:0; | |
margin: 12px; | |
} | |
.status { | |
height: 12px; | |
width: 12px; | |
background-color: #bbb; | |
border-radius: 50%; | |
display: inline-block; | |
} | |
.false { | |
background-color: red; | |
} | |
.true { | |
background-color: green; | |
} | |
input { | |
width: 70%; | |
color: #b8c2cc; | |
padding: 5px; | |
margin: 12px; | |
border-color: #b8c2cc; | |
border-width:2px; | |
border-radius: 5px; | |
bottom:0; | |
position:absolute; | |
} | |
</style> | |
</head> | |
<body> | |
<h2>Messages</h2> | |
<ul id="messages"> | |
</ul> | |
<div class="topcorner"> | |
<div>connectionState: <span id="connectionState" class="status false"> </span></div> | |
<div>authStatus: <span id="authStatus" class="status false"> </span></div> | |
</div> | |
<input type="text" placeholder="message #general"> | |
</body> | |
</html> |
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
const websocket = require('websocket') | |
const geoInst = require('geodb').create | |
// NOTE: this is a SHARED USER_TOKEN and API_KEY | |
// messages here are periodically cleaned up | |
// | |
// Sign up at geodb.io to get your own FREE key | |
// | |
const USER_TOKEN = "5c0a79ec-1550-4732-938b-9100221c83b0" | |
const API_KEY = "c5619bab-391b-56d2-8b7e-75a5715664ce" | |
console.log('Hello GeoDB') | |
// TODO singleton API here to simplify | |
const geodb = geoInst.make({ | |
host: 'geodb.io', | |
type: 'ws', | |
protocol: 'https', | |
}) | |
const publish = function publish(message) { | |
console.log('Publishing!') | |
geodb.publish( | |
{ | |
payload: message, | |
channel: "#general-demo" | |
} | |
).then((data) => console.log('publish promise success', data)) | |
.catch((err) => console.log('publish promise err', err)); | |
} | |
const subscribe = function subscribe() { | |
return geodb.subscribe({channel: "#general"}, | |
(err, data) => { | |
console.log('data received in', err, data); | |
if (!err) { appendMsg(data); } | |
})} | |
geodb.on('ready', evt => { | |
console.log('Ready', evt) | |
// | |
// re-read the last 10 messages | |
// TODO document reader | |
//const reader = geodb.reader(); | |
}) | |
geodb.on('error', evt => { | |
console.log('error') | |
}) | |
geodb.on('disconnect', evt => { | |
console.log('Disconnected') | |
process.exit() | |
}) | |
geodb.connect({ | |
userToken: USER_TOKEN, | |
apiKey: API_KEY, | |
}) | |
console.log('Connecting...') | |
const status = function status() { | |
const connState = document.getElementById('connectionState'); | |
connState.classList.remove('true', 'false'); | |
connState.classList.add(geodb.connectionState().isOpen); | |
const authStatus = document.getElementById('authStatus'); | |
authStatus.classList.remove('true', 'false'); | |
authStatus.classList.add(geodb.authStatus().status === 'authenticated'); | |
} | |
setTimeout((function () { | |
status(); | |
}), 1000) |
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
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"websocket": "1.0.28", | |
"geodb": "0.1.5" | |
} | |
} |
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
'use strict'; | |
var websocket = require('websocket'); | |
var geoInst = require('geodb').create; | |
// NOTE: this is a SHARED USER_TOKEN and API_KEY | |
// messages here are periodically cleaned up | |
// | |
// Sign up at geodb.io to get your own FREE key | |
// | |
var USER_TOKEN = "5c0a79ec-1550-4732-938b-9100221c83b0"; | |
var API_KEY = "c5619bab-391b-56d2-8b7e-75a5715664ce"; | |
console.log('Hello GeoDB'); | |
// TODO singleton API here to simplify | |
var geodb = geoInst.make({ | |
host: 'geodb.io', | |
type: 'ws', | |
protocol: 'https' | |
}); | |
var publish = function publish(message) { | |
console.log('Publishing!'); | |
geodb.publish({ | |
payload: message, | |
channel: "#general-demo" | |
}).then(function (data) { | |
return console.log('publish promise success', data); | |
}).catch(function (err) { | |
return console.log('publish promise err', err); | |
}); | |
}; | |
var subscribe = function subscribe() { | |
return geodb.subscribe({ channel: "#general" }, function (err, data) { | |
console.log('data received in', err, data); | |
if (!err) { | |
appendMsg(data); | |
} | |
}); | |
}; | |
geodb.on('ready', function (evt) { | |
console.log('Ready', evt); | |
// | |
// re-read the last 10 messages | |
// TODO document reader | |
//const reader = geodb.reader(); | |
}); | |
geodb.on('error', function (evt) { | |
console.log('error'); | |
}); | |
geodb.on('disconnect', function (evt) { | |
console.log('Disconnected'); | |
process.exit(); | |
}); | |
geodb.connect({ | |
userToken: USER_TOKEN, | |
apiKey: API_KEY | |
}); | |
console.log('Connecting...'); | |
var status = function status() { | |
var connState = document.getElementById('connectionState'); | |
connState.classList.remove('true', 'false'); | |
connState.classList.add(geodb.connectionState().isOpen); | |
var authStatus = document.getElementById('authStatus'); | |
authStatus.classList.remove('true', 'false'); | |
authStatus.classList.add(geodb.authStatus().status === 'authenticated'); | |
}; | |
setTimeout(function () { | |
status(); | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment