Search code examples
javascriptjsonnode.jsactivemq-classicstomp

NODE.JS output feed information to a source


Morning. I have recently started to get into the wonders of NODE.JS and have a node that extracts information from an Active MQ datafeed via stomp, and renders it using prettyjson and outputs to the console.

var prettyjson = require('prettyjson'),
    StompClient = require('stomp-client').StompClient;

var destination = '/topic/(topic)',
    client = new StompClient('datafeeds.(datafeedlocation)', 61618, '(email.address)', '(password)', '1.0');

client.connect(function(sessionId) {
    console.log('Trying to connect...');
    client.subscribe(destination, function(body, headers) {
        console.log(prettyjson.render(JSON.parse(body)));
    });
});

and the feed churns out quite happily on the console.

Now I have read a lot of examples, and most of them always output to the console.. however I want to take this data and do something with it.

So how do I take the console output and store it, or just skip the console output and just store in the information.. any method for now is fine, in a json file, or to a database.. hell.. even a text file. Good clear guides are fine, not expecting anyone to write code for me.

Thanks in advance for any good pointers.


Solution

  • Bah, as per usual after posting for help.. i get it working.. here is the final code

    var prettyjson = require('prettyjson'),
        fs = require('fs'),
        StompClient = require('stomp-client').StompClient;
    
    var destination = '/topic/(topic)',
        client = new StompClient('datafeeds.(datafeed)', 61618, '(email.address)', '(password)', '1.0');
    
    client.connect(function(sessionId) {
        console.log('Trying to connect...');
        client.subscribe(destination, function(body, headers) {
            fs.writeFile("./output", prettyjson.render(JSON.parse(body)), function(err) {
        if(err) {
            console.log(err);
        } else {
            console.log("The file was saved!");
        }
    }); 
        });
    });