Node.js Examples

NOTE: Node.js is not available in mLinux 5.3.x and above.

Pushing data to M2X Cloud

    
    var http = require("http");

    var device_id = "<DEVICE-ID>";
    var m2xKey = "<M2X-KEY>";
    var stream = "<STREAM-NAME>";
    var data = { "value": 30 };

    var content = JSON.stringify(data);

    var options = {
        method : 'PUT',
        host : 'api-m2x.att.com',
        path : '/v2/devices/' + device_id + "/streams/" + stream + "/value",       
        headers : {
            'Content-Type' : 'application/json',
            'Content-Length' : content.length,
            'X-M2X-KEY' : m2xKey
        }
    };

    var req = http.request(options, function(res) {
        res.setEncoding('utf8');
        res.on('data', function(chunk) {
            console.log('BODY: ' + chunk);
        });
    });

    req.on('error', function(err) {
        console.error("post error: ", err.message);
    });

    // post the data
    req.write(content);
    req.end();