简体   繁体   中英

Receiving BrokeredMessage from ServiceBus from a nodejs server

I am reading messages from an existing Azure ServiceBus from a new nodejs Server.

This is the way the messages are being sent to the ServiceBus from a .NET server:

var topicClient = TopicClient.CreateFromConnectionString(serviceBusConnectionString, routingKey);    
var brokeredMessage = new BrokeredMessage(message.ToJson());
topicClient.Send(brokeredMessage);

Where topicClient is Microsoft.ServiceBus.Messaging.TopicClient

I am using the following method to read the messages on a nodejs server using azure-sb package:

sbClient = ServiceBusClient.createFromConnectionString(connectionString)  
subscriptionClient = this.sbClient.createSubscriptionClient(topicName, subscriptionName);
receiver = this.subscriptionClient.createReceiver(ReceiveMode.receiveAndDelete);
messages = await this.receiver.receiveMessages(10,10);
console.log(messages.map(message => { message.body }));

message.body is a buffer and when I do message.body.toString('utf-8') I get something like:

@string3http://schemas.microsoft.com/2003/10/Serialization/��{VALID JSON}

I am interested of course in the valid JSON in between. In the .net servers we simply do brokeredMessage.GetBody() and we get the object, so is there an easy way on nodejs to do the same?

According to my test, if we use the standard library Microsoft.Azure.ServiceBus to send messages in.Net application, it will directly JSON parse the message in node application

For example

This is my C# code to send a message:

class Program
{
    static void Main(string[] args)
    {
        string connectionString = "Endpoint=sb://...";
        var client = new TopicClient(connectionString, "");
        var payload = JsonConvert.SerializeObject(new DemoMessage() { Title = $"hello!!! {DateTime.Now}" });
        var serviceBusMessage = new Message(Encoding.UTF8.GetBytes(payload));
        serviceBusMessage.SessionId = Guid.NewGuid().ToString("D");

        client.SendAsync(serviceBusMessage).Wait();

    }

    private class DemoMessage
    {
        public DemoMessage()
        {
        }

        public string Title { get; set; }
    }

This is my Node.js code to receive a message:

const { ServiceBusClient, ReceiveMode } = require("@azure/service-bus");

// Define connection string and related Service Bus entity names here
const connectionString =
  "Endpoint=sb://";
const topicName = "***";
const subscriptionName = "***";

async function main() {
  const sbClient = ServiceBusClient.createFromConnectionString(
    connectionString,
  );
  const subscriptionClient = sbClient.createSubscriptionClient(
    topicName,
    subscriptionName,
  );
  const receiver = subscriptionClient.createReceiver(ReceiveMode.receiveAndDelete);

  try {
    const messages = await receiver.receiveMessages(1);
    console.log("Received messages:");
    console.log(messages.map((message) => message.body));
    await subscriptionClient.close();
  } finally {
    await sbClient.close();
  }
}

main().catch((err) => {
  console.log("Error occurred: ", err);
});

在此处输入图像描述

Besides, if you still use the library WindowsAzure.ServiceBus , we need to use BrokeredMessage(Stream messageBodyStream, bool ownsStream) to initialize an object.

Because we use BrokeredMessage(platload) to initialize, it will use DataContractSerializer with a binary XmlDictionaryWriter to initialize an object. So, the payload is being serialized using DataContractSerializer with a binary XmlDictionaryWriter and this is the cause that explains why the body of the message has in its start the type indication @string3http://schemas.microsoft.com/2003/10/Serialization/ .

For example This is my C# code to send a message:

var client =TopicClient.CreateFromConnectionString(connectionString, "test");
            var payload = JsonConvert.SerializeObject(new DemoMessage() { Title = $"hello BrokeredMessage!!! {DateTime.Now}" });
            using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(payload))) {
                var serviceBusMessage = new BrokeredMessage(stream,true);
                await client.SendAsync(serviceBusMessage);

            }

I use the same code to receive在此处输入图像描述

For more details, please refer to here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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