Node Client for Phant.
To connect to data.sparkfun.com
var Phant = require('phant-client').Phant
var phant = new Phant()
To connect to your own Phant server:
var phant = new Phant({ "iri": "http://localhost:8080" })
IMPORTANT NOTE
If you are using your own Phant server, you must be
running phant-manager-http
.
Make sure you follow the instructions in the README.md
on this page:
https://github.com/sparkfun/phant-manager-http
Functions
create()
and connect()
will callback with a streamd
object
which can be used to do Phant stuff.
You must create()
or connect()
to a Phant
server first: you can't just use a serialized streamd
object.
phant.create({
title: "My Awesome Stream",
description: "The description",
fields: "a,b,c"
}, function(error, streamd) {
if (error) {
console.log("+ error", error)
} else {
console.log(streamd)
}
}
Note that if the stream as created on a different Phant server,
it will work exactly as expected. The streamd
records the location of the server it connected to.
phant.connect(streamd, function(error, streamd) {
/* do stuff */
})
If you have a 'streams' IRI, you can connect directly to that
var iri = 'https://data.sparkfun.com/streams/dZ4EVmE8yGCRGx5XRX1W'
phant.connect(iri, function(error, streamd) {
/* do stuff */
})
phant.add(streamd, {
a: 22
})
Phant requires that all fields be present.
iotdb-phant-client
cleverly defaults
these to the empty string.
Note that this is entirely independent of the next
/reset
functions below.
phant.latest(streamd, function(error, rd) {
if (error) {
console.log("# error", error)
} else if (rd === null) {
console.log("+ eof")
} else {
console.log("+ got", rd)
}
})
The first class to next()
will get the first
record and subsequent calls will step through the data.
When no more data is available, null
will be
returned to indicate EOF.
If you want to start looping through the records again
from the beginning, call phant.reset(streamd)
.
If you need to loop through records multiple times
simultaneously, use phant.scrub(streamd)
to
return a copy of the stream object.
The sightly complicated nature of this example is because the code is callback driven.
var fetcher = function() {
phant.next(streamd, function(error, rd) {
if (error) {
console.log("# error", error)
} else if (rd === null) {
console.log("+ eof")
} else {
console.log("+ got", rd)
fetcher()
}
})
}
fetcher()