Last active
February 14, 2016 19:28
-
-
Save marcosscriven/05e1c14f92dba41f280a to your computer and use it in GitHub Desktop.
Example of subscribing to Navetas Loop websockets, either in the browser console, or in a Node script. In the latter case should see output like this:
This file contains hidden or 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
// Serial and secret can be scraped from summary page | |
var server = 'https://www.your-loop.com'; | |
$.getScript('https://www.your-loop.com/socket.io/socket.io.js').done(function() { | |
socket = io.connect(server); | |
console.log('Connected to socket'); | |
socket.on('connect', function() { | |
socket.emit("subscribe_electric_realtime", { | |
serial: '00000000xxxxx', | |
clientIp: '127.0.0.1', | |
secret: 'xxxxxxxxxx' | |
}); | |
}); | |
socket.on('electric_realtime', function(data) { console.log(data);}); | |
}); | |
This file contains hidden or 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
// Subscribe to electricity and gas streams on Navetas Loop websockets | |
// Secrets and serial numbers can be found by logging into your-loop.com | |
// and evaluating the 'Drupal.settings.navetas_realtime' variable in browser console. | |
console.log('Starting'); | |
var elec_serial = 'xxxx'; | |
var elec_secret = 'xxxx'; | |
var gas_serial = 'xxxx'; | |
var gas_secret = 'xxxx'; | |
// Need version 0.9.16, newer versions incompatible | |
var io = require('socket.io-client'); | |
var socket = io.connect('https://www.your-loop.com', {reconnect: true}); | |
// Connect | |
socket.on('connect', function(){ | |
console.log('Connecting'); | |
// Subscribe to electricity readings in watts | |
socket.emit("subscribe_electric_realtime", { | |
serial: elec_serial, | |
clientIp: '127.0.0.1', | |
secret: elec_secret | |
}); | |
// Subscribe to gas readings | |
socket.emit("subscribe_gas_interval", { | |
serial: gas_serial, | |
clientIp: '127.0.0.1', | |
secret: gas_secret | |
}); | |
}); | |
// Output electricity readings (~1 per 10 seconds) | |
socket.on('electric_realtime', function(data) { console.log("Electricity:%j", data);}); | |
// Output gas readings (much slower ~15 mins) | |
socket.on('gas_interval', function(data) { console.log("Gas:%j", data);}); | |
// Disconnect | |
socket.on('disconnect', function(){ console.console.log("Disconnected.");}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment