Message Pack
Message Pack is a smart way to serialize the data in a faster and smaller way than JSON. There is plenty of libraries in the common languages to encode/decode msgpack efficiently.
The format allows sending arbitrary objects (like in JSON).
A smaller body means less data transmitted and energy saving which makes the format very interesting for battery powered devices.
Using msgpack with thethings.iO
You can use msgpack with http/s by adding the header Content-type
Content-type : application/msgpack
Example
var http = require('http')
, msgPack = require('msgpack5')()
var thingToken = 'PUT HERE YOUR THING TOKEN'
var req = http.request({
host : 'api.devices.thethings.io',
path: '/v2/things/'+thingToken,
port : 80,
method : 'POST',
headers : {
'Content-type' : 'application/msgpack'
}
})
req.on('response',function(res){
res.on('data',function(data){
//Response console.logs
console.log('String: ', data.toString())
console.log('Decoded: ', msgPack.decode(data))
})
})
var json = {
"values":
[{
"key": "temperature",
"value" : 23,
"geo" : {
"lat" : 41.4121132,
"long" : 2.2199454
}
}]
}
// Payload Size comparisons logs
var json_string = JSON.stringify(json)
console.log('JSON stringified size: ', json_string.length)
console.log('JSON stringified to hex: ', new Buffer(json_string).toString('hex'))
var json_to_msgPack = msgPack.encode(json)
console.log('msgPack encoded length: ', json_to_msgPack.length)
console.log('msgPack to hex: ', json_to_msgPack.toString('hex'))
req.end(json_to_msgPack)
// Payload size comparisons
JSON stringified size: 87
JSON stringified to hex: 7b2276616c756573223a5b7b226b6579223a2274656d7065726174757265222c2276616c7565223a32332c2267656f223a7b226c6174223a34312e343132313133322c226c6f6e67223a322e323139393435347d7d5d7d
msgPack encoded length: 57
msgPack to hex: 81a676616c7565739183a36b6579ab74656d7065726174757265a576616c756517a367656f82a36c6174ca4225a601a46c6f6e67ca400e1396
//Response console.logs
String: ��status�success�message�created
Decoded: { status: 'success', message: 'created' }
Limitations
At the moment we don't offer full support to message pack.
The MQTT and Websocket can't be encoded in msgpack (we are working on that).
Due to our backend it is not possible to encode/decode binary 0xC4-0xC6.
That being said, if your project needs these features please contact us.
Updated over 1 year ago