Messaging Functions

HTTP REST APIs

Within your device template, you could call a REST API and get data from an external source, or push data to an external service.

GET Requests

//requires the resource URL as the argument
rest.get({url: ''})  

rest.get({url:'https://httpbin.org/get'})

//you can also create a separate object for the argument and pass it to the API 
//headers are also supported
let options = {
	url: "https:https://httpbin.org/get",
	headers: {
		'content-type': 'application/json'
	}
}

rest.get(options)  

POST Requests

//requires the resource URL and a message object as the arguments
rest.post({url: '', json: {}})

rest.post({url:'https://httpbin.org/post', json: { hello: 'world'}})

//you can also create a separate object for the arguments and pass it to the API  
let obj = {
	username: "test_user",
	password: "dontusethis"
}

//headers are also supported
let options = {
	url: "https://httpbin.org/post",
	json: obj,
	headers: {
		'content-type': 'application/json'
	}
}

rest.post(options);

PUT Requests

//requires the resource URL and a message object as the arguments
rest.put({url: '', json: {}})

rest.put({url:'https://httpbin.org/put', json: { hello: 'world'}})

//you can also create a separate object for the arguments and pass it to the API  
let options = {
	url: "https://httpbin.org/put",
	json: { hello: 'world'},
	headers: {
		'content-type': 'application/json'
	}
}

rest.put(options);

PATCH Requests

//requires the resource URL and a message object as the arguments
rest.patch({url: '', json: {}})

rest.patch({url:'https://httpbin.org/patch', json: { hello: 'world'}})

//you can also create a separate object for the arguments and pass it to the API  
let options = {
	url: "https://httpbin.org/patch",
	json: { hello: 'world'},
}

rest.patch(options);

DELETE Requests

//requires the resource URL the argument
rest.delete({url: ''})

rest.delete({url:'https://httpbin.org/delete'})

//you can also create a separate object for the arguments and pass it to the API  
let options = {
	url: "https://httpbin.org/delete",
	headers: {
		'content-type': 'application/json'
	}
}

rest.delete(options);

MQTT APIs

Within the device template, you can use the MQTT APIs to interact with the MQTT connections.

Publish

To publish a message to an MQTT endpoint, you can use the following function.

//requires a payload object and a topic string as arguments
mqtt.publish(payload, topic)

let payload = {
    hello: 'world'
}

let topic = "testTopic"

mqtt.publish(payload, topic)

Resubscribe

To resubscribe to multiple topics use the following function.

//requires an array of topics as the argument
mqtt.resubscribe([topics])

mqtt.resubscribe([topic1, topic2, topic3])

Force Disconnect

To disconnect from all MQTT topics and close all connections, use the following function.

//no arguments are required for this function
mqtt.forceDisconect()

Force Connect

To connect to all previously connected MQTT connections, use the following function.

//no arguments are required for this function
mqtt.forceConect()

Last updated