简体   繁体   English

如何从Node.js客户端向WSO2消息代理发送消息

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

有谁知道如何从用Node.js编写的客户端向WSO2消息代理发送消息?

As WSO2 Message Broker supports AMQP protocol you should be able to do this with any NodeJS AMQP 0-9-1 Client Library. 由于WSO2 Message Broker支持AMQP协议,因此您应该能够使用任何NodeJS AMQP 0-9-1客户端库执行此操作。 Some of the examples are, 一些例子是

  1. amqp.node : https://github.com/squaremo/amqp.node amqp.node: https : //github.com/squaremo/amqp.node
  2. node-amqp : https://github.com/postwait/node-amqp 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. 使用amqp.node库编写的以下示例代码可以用作NodeJS客户端,以发布或接收来自WSO2 Message Broker的消息。 You have to use the format amqp://{username}:{password}@{hostname}:{port} to establish a connection with Message Broker. 您必须使用格式amqp://{username}:{password}@{hostname}:{port}建立与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. “ amqp.node”库提供了丰富的API ,该API也可用于其他Queue操作MB。

// 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. WSO2 Message Broker支持高级消息队列协议(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. 我自己还没有尝试过,但是您应该能够使用Node.js客户端与WSO2 MB连接。

See amqp.node project at GitHub. 请参阅GitHub上的amqp.node项目。

You can connect with WSO2 MB using the AMQP connection URL. 您可以使用AMQP连接URL与WSO2 MB连接。 See the " Sending and Receiving Messages Using Queues " doc to understand how AMQP connection URL can be specified to connect with WSO2 MB. 请参阅“ 使用队列发送和接收消息 ”文档,以了解如何指定AMQP连接URL以与WSO2 MB连接。

I hope this helps! 我希望这有帮助!

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM