Published
Edited
Jul 3, 2018
1 star
Insert cell
Insert cell
{
const serverLocation = "ws://127.0.0.1:8000/ws/output/"
const startWebSocketConnection = (websocketServerLocation) => {
console.log('Initializing Forja websocket connection')
const forjaSocket = new WebSocket(websocketServerLocation);
forjaSocket.onmessage = function(e) {
console.log(e.data)
const data = JSON.parse(e.data, (key, value) => JSON.parse(value))
document.querySelector('#data-display').innerHTML = JSON.stringify(data, null, "<br>&emsp;")
};
forjaSocket.onclose = function(){
console.error('Forja websocket closed unexpectedly')
// Try to reconnect in 5 seconds
setTimeout(function(){startWebSocketConnection(websocketServerLocation)}, 5000);
};

const buttons = document.getElementsByClassName('get-data')
Array.from(buttons).forEach((button) => {
button.onclick = null
button.onclick = function() {
forjaSocket.send(JSON.stringify({
'group': this.name
}))
}
})
}
startWebSocketConnection(serverLocation)
}
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more