Windows Azure Service Bus: Sending the message to queue and topic with JavaScript
IAs you probably know the easiest way to send the message to the queue or topic is to use .NET SDK. When using .NET SDK communication with Service Bus will be TCP on specific ports.
This protocol is called SBMP and stands fro native service message protocol. It is not some broadly use standard and for this reason is not very pushed form the marketing perspective. Another way to establish communication would be AMQP
At the writing of this you cannot switch (downgrade) SBMP pr AMQP to HTTP protocol. Many developers think that this is possible. Unfortunately there is confusion around this, because Service Bus Relay features can downgrade to HTTP automatically if required TCP ports are blocked.
Fortunately, you can use HTTP to communicate with the Service Bus. In this post I will provide some samples which show how to do that.
Prerequisites
Before you start with this example you will have to create one queue and one topic. But it is more interesting and more complicate to prepareShare Access Signatures. In other words you will have to create Access Rules and apply them to your entities (Queue and Topic). In my example the Java Script code will be shown which authenticate by using Shared Access Signature and not by using of Access Control Service.
The Share Access Signature token will look like:
http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/damir_5F00_dobric/image_5F00_thumb_5F00_2F67C034.png
You use following code to create the SAS Key with name ‘demouser1_can_Send“
private static void createSaskey() |
After you execute this code, be sure that you make a copy of the ‘key1”.
Sending of a message to the queue/topic
First of all you will need a function which will create the token form key value (key1) and key name (demouser_can_send).
Missing encoding function can be found here.
|
Just to send a POST request to some endpoint is not a big deal. Unfortunately in our case, we have to be aware of CORS issue.
When you start to execute the JavaScript code, the JS file containing the code it downloaded from some Web site like contoso.com/mycode.js.
However Service Bus is hosted on domain servicebus.windows.net, which is a different one. In this case you will run into CORS issue.
Following code shows how to send the message to service bus queue by handling of CORS issue.
|
Assuming that the code shown above is packaged in some library, following demonstrate how to send the message.
The instance of the helper library is SB.
var msg = { "message": txtMsg.value, "id": 1234}; SB.sendMessage(topicOrqueuePath, JSON.stringify(msg), "application/json", function (messagingResult) { $("#result").html(messagingResult.body); }); }); |
If you are interested on this topic, you can also read Microsoft Azure Service Bus: Receiving of messages from queue and topic with JavaScript or Microsoft Azure Service Bus: PeekAndLock Messages with JavaScript