bundyfx bundyfx - 2 months ago 15
Node.js Question

nodejs rabbitjs simple pub sub rabbitmq

New to NodeJS and RabbitMQ. I am attempting to do a simple pub and sub and store the output from the sub when it receives a message from pub.

sub.js:

var context = require('rabbit.js').createContext();
var sub = context.socket('SUB');
context.on('ready', function() {
sub.pipe(process.stdout);
sub.connect('events', function() {

});
});


pub.js:

var context = require('rabbit.js').createContext();

var pub = context.socket('PUB')
pub.connect('events', function() {

pub.write(JSON.stringify({welcome: 'rabbit.js'}), 'utf8');

})


This works fine and I get the stdout message (from sub.pipe(process.stdout)) on the console running the sub.js, However my question is how can I work with this object? Is it possible to store that output into a variable and pass it into another function?

Answer

sockets are node streams. So instead of piping them to stdout, you can read from them:

sub.on('data', function(message) { /** Do something with message */ });
Comments