Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node.js payload #14

Closed
confuser opened this issue Apr 19, 2023 · 4 comments · Fixed by #16
Closed

Node.js payload #14

confuser opened this issue Apr 19, 2023 · 4 comments · Fixed by #16

Comments

@confuser
Copy link

@piotr-rojek tried the new version and no longer getting the timeout issue described in #9 🥳

However a new issue cropped up. We're sending messages with a JSON payload with nodejs and receiving them with nodejs via the Azure SDK.

Example code:

const message: ServiceBusMessage = {
        body: { questionId: "fbf1ccfe-c544-4125-9e42-2e09b0d26a64", answerId: "51abac65-e201-402b-8b5a-09186a0fbdea", value: "", userId: "290c1ead-c95b-4559-969e-851be85689a6", version: "1.0", createdOn: "2023-04-19T13:20:27.718Z" }
        partitionKey:'questionnaire-V1'
      }

sender.sendMessages(message)
const serviceBusTrigger: AzureFunction = async function (
  context: Context,
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  req: any
): Promise<void> {
   console.log(req)
}

However, using the emulator the body is in a different format, Sp�@@@Sr�+�x-opt-partition-key�questionnaire-V1SsESu��{"questionId":"fbf1ccfe-c544-4125-9e42-2e09b0d26a64","answerId":"51abac65-e201-402b-8b5a-09186a0fbdea","value":"","userId":"290c1ead-c95b-4559-969e-851be85689a6","version":"1.0","createdOn":"2023-04-19T13:20:27.718Z"} as an example.

Seems to be the exact problem described in https://www.bfcamara.com/post/84113031238/send-a-message-to-an-azure-service-bus-queue-with

@rafak
Copy link

rafak commented Apr 20, 2023

There is in fact some weirdness with the content (using node azure-sdk)

example:

  • the message being published is
(...)

const msgBody =  [{
        "SuspiciousNumber":"87777788870009",
        "TestingEptyName":"",
        "StatusId":5,
        "IsProcessing":true,
        "StartingDate":null,
        "ExpiryDate":null,
        "LastModifiedDate":"2023-03-13T21:28:04.8387347Z"
      }]

const msg = {
        body: msgBody,
        subject: 'test pci processing',
        contentType: 'application/json',
      }

      await sender.sendMessages([msg])

  • what lands in rabbitmq is:
    image

payload:

AFNw0AAAAAcAAAADQEBAAFNy0QAAAAQAAAAAAFNz0AAAADAAAAAHQEBAoRN0ZXN0IHBjaSBwcm9jZXNzaW5nQECjEGFwcGxpY2F0aW9uL2pzb24AU3WgtVt7
IlN1c3BpY2lvdXNOdW1iZXIiOiI4Nzc3Nzc4ODg3MDAwOSIsIlRlc3RpbmdFcHR5TmFtZSI6IiIsIlN0YXR1c0lkIjo1LCJJc1Byb2Nlc3NpbmciOnRydWUs
IlN0YXJ0aW5nRGF0ZSI6bnVsbCwiRXhwaXJ5RGF0ZSI6bnVsbCwiTGFzdE1vZGlmaWVkRGF0ZSI6IjIwMjMtMDMtMTNUMjE6Mjg6MDQuODM4NzM0N1oifV0=

decoded payload:

echo "AFNw0AAAAAcAAAADQEBAAFNy0QAAAAQAAAAAAFNz0AAAADAAAAAHQEBAoRN0ZXN0IHBjaSBwcm9jZXNzaW5nQECjEGFwcGxpY2F0aW9uL2pzb24AU3WgtVt7
> IlN1c3BpY2lvdXNOdW1iZXIiOiI4Nzc3Nzc4ODg3MDAwOSIsIlRlc3RpbmdFcHR5TmFtZSI6IiIsIlN0YXR1c0lkIjo1LCJJc1Byb2Nlc3NpbmciOnRydWUs
> IlN0YXJ0aW5nRGF0ZSI6bnVsbCwiRXhwaXJ5RGF0ZSI6bnVsbCwiTGFzdE1vZGlmaWVkRGF0ZSI6IjIwMjMtMDMtMTNUMjE6Mjg6MDQuODM4NzM0N1oifV0=" | base64 --decode
Sp▒@@@Sr▒Ss▒0@@@▒test pci processing@@▒application/jsonSu▒▒[{"SuspiciousNumber":"87777788870009","TestingEptyName":"","StatusId":5,"IsProcessing":true,"StartingDate":null,"ExpiryDate":null,"LastModifiedDate":"2023-03-13T21:28:04.8387347Z"}]

i am guessing it's a problem with this method:

public byte[] MapToRabbit(IBasicProperties prop, Message rMessage)

but i might be wrong.
the thing is: rabbit already has the extra content at the beginning of the payload
and it looks like some "encoded" version of the subject and contentType and body keys
if i were to take a guess:

  • Sp▒@@@Sr▒Ss▒0@@@▒ - subject
  • @@▒ - contentType
  • Su▒▒ - body

but that is pure speculation. maybe you could try to run the sbemu-node docker projects and POST to the /publish endpoint and perhaps set a trap and inspect the raw payload coming in from azure-sdk - perhaps it's in a different format?

cheers
r.

@piotr-rojek
Copy link
Owner

Hey guys,

The problem seems to be related to the message batching not being supported by the emulator atm. When batching is enabled, multiple messages are serialized and wrapped by an outer message. Emulator recognizes only the outer message but does not understand its inner body containing the actual message, and forwards as-is to rabbitmq.

A quick workaround is to send a single message, not an array, by changing

await sender.sendMessages([msg])

to

await sender.sendMessages(msg)

Note that the example given by @confuser seems to already pass a single message, but since it is typed, might be following a "batched" code path on nodejs sdk side. On the other hand the code snippet given by @rafak works fine after removing [].

Going forward, I will try to find a way to handle this in the emulator. I just need to figure out how to access a MessageFormat field, which indicates that we are dealing with a batch. TBC

/Piotr

@rafak
Copy link

rafak commented Apr 21, 2023

ok i will try to work with single messages, thanks

@rafak
Copy link

rafak commented Apr 21, 2023

i can confirm that single messages work and all is good,
i found some info in ms docs, but you probably already got there:

i will put in the links in case they become somehow useful.

https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messages-payloads#payload-serialization

https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample01_SendReceive.md#send-and-receive-a-batch-of-messages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants