JavaScript SDK npm package
The Fostrom Device SDK for JavaScript is built to work in Node.js and Bun, on Linux and macOS.
Example
import Fostrom from 'fostrom';
const fostrom = new Fostrom({
fleet_id: "<fleet-id>",
device_id: "<device-id>",
device_secret: "<device-secret>",
})
// Setup the onMail handler, to process incoming mail.
fostrom.onMail = async (mail) => {
const {id, name, payload, mailbox_size} = mail
console.debug(`Received Mail (${mailbox_size}): ${name} ${id}`)
await mail.ack()
}
async function main() {
await fostrom.connect()
// To send a message to Fostrom
await fostrom.sendMsg("<packet-schema-name>", { ...payload })
// To send a datapoint to Fostrom
await fostrom.sendDatapoint("<packet-schema-name>", { ...payload })
}
main()
Note that in Node.js, to ensure the import
statement above works, you’ll need to add "type": "module"
to your package.json
file.
Library Reference
Fostrom
Class
Fostrom {
// --- Functions ---
async connect()
async sendDatapoint(name, payload)
async sendMsg(name, payload)
// Usually, there's no need to use the following
// functions, as mail will be delivered to the
// handler you define automatically.
async mailboxStatus()
// returns {mailbox_size, next_mail_id, next_mail_name}
async nextMail()
// return FostromMail{}
// --- Replacable Callbacks ---
connected()
unauthorized(reason, reconnecting_in_ms)
reconnecting(reason, reconnecting_in_ms)
async onMail(mail = FostromMail{})
}
FostromMail
Class
FostromMail {
// --- Data ---
id: string
name: string
payload: object
mailbox_size: integer(u16)
// --- Functions ---
async ack()
async reject()
async requeue()
}