JavaScript API
To store and query data from a webpage include the Streametry JavaScript library which comes with the server and is located in web/js/
directory:
<script src="http://host/web/js/streametry.min.js"></script>
Then connect to the Streametry server using:
var streametry = new Streametry(appName, 'http://host:port');
For a working example see embed.html on the server or the following JS Fiddle.
Query
Queries have the same general stream-to form as in other APIs:
stream(collection) -> [filter, transform, aggregate] -> to( callback )
In the JavaScript API this is:
var query = streametry.stream( collection )
.filter( ).map( ).aggregateBy( )
.to( callback );
For example:
var query = streametry.stream("sensors")
.filter( function (sensor) { return sensor.temp > 20; } )
.to( function (sensors) {
// do something with sensors result object
console.log(sensors);
});
Historical Queries
Historical queries have the form:
var query = streametry.history( collection, field ) ... .to( callback )
Closing Queries
To stop receiving data simply call:
query.close();
Store
To store a value in a collection use set
:
streametry.set(collection, key, value [, callback ]);
For example:
streametry.set("sensors", "s1", {"temp": 20});
Lookup
To get a value from a collection use get
:
streametry.get(collection, key, callback );
For example:
streametry.get("sensors", "s1", function(sensor) {
console.log("temperature is " + sensor.temp);
});
Disconnect
To disconnect from the server call:
streametry.close();