How do I send a message to a WSO2 Message Broker from a Node.js client

Does anyone know how to send a message to a WSO2 Message Broker from a client written in Node.js?

As WSO2 Message Broker supports AMQP protocol you should be able to do this with any NodeJS AMQP 0-9-1 Client Library. Some of the examples are,

  1. amqp.node : https://github.com/squaremo/amqp.node
  2. node-amqp : https://github.com/postwait/node-amqp

The following sample code, written using amqp.node library can be used as a NodeJS client to publish or receive messages from WSO2 Message Broker. You have to use the format amqp://{username}:{password}@{hostname}:{port} to establish a connection with Message Broker. All messages will be sent as byte messages but can be received as text.

'amqp.node' library provide a rich API which can be used to other Queue operations MB too.

// Sample Publisher
var queuename = 'MyQueue';
var openConn = require('amqplib').connect('amqp://admin:admin@localhost:5672'); // amqp://{username}:{password}@{hostname}:{port} is default AMQP connection URL of WSO2 MB
openConn.then(function(conn) {
  var ok = conn.createChannel();
  ok = ok.then(function(channel) {
    channel.assertQueue(queuename);
    channel.sendToQueue(queuename, new Buffer('New Message'));
  });
  return ok;
}).then(null, console.warn); 

The consumer client code is as follows.

// Sample Consumer
var queuename = 'MyQueue';
var openConn = require('amqplib').connect('amqp://admin:admin@localhost:5672'); // amqp://{username}:{password}@{hostname}:{port} is default AMQP connection URL of WSO2 MB
openConn.then(function(conn) {
  var ok = conn.createChannel();
  ok = ok.then(function(channel) {
    channel.assertQueue(queuename);
    channel.consume(queuename, function(msg) {
      console.log(msg.content.toString());
      channel.ack(msg);
    });
  });
  return ok;
}).then(null, console.warn);

WSO2 Message Broker supports Advanced Message Queuing Protocol (AMQP) v0.91.

I haven't tried this myself, but you should be able to use a Node.js client to connect with WSO2 MB.

See amqp.node project at GitHub.

You can connect with WSO2 MB using the AMQP connection URL. See the "Sending and Receiving Messages Using Queues" doc to understand how AMQP connection URL can be specified to connect with WSO2 MB.

I hope this helps!

Thanks!