Skip to content

How to use Azure Service Bus Java Script SDK?

Damir Dobric edited this page Jan 26, 2015 · 5 revisions

This repository contains basically two things:

  1. Java Script SDK
  2. Sample application

Java Script SDK

Java Script SDK consists of two files:

Version 1.2

  • servicebusjssdk-1.2
  • servicebusjssdk-1.2.min

Previous version 1.1 was built with:

  • servicebussdk-1.0.js (renamed in v1.2)
  • servicebusjssdk-1.0.min.js

Please use the first file to see full documentation of all classes and related functions.

Sample Application

Sample application consists of three pure HTML pages :Index.html, QueueSample.html, TopicSample.html and EventHubSample.html.

When the sample application is started the Index.html is loaded as welcome page. From there you can navigate to QueueSample, TopicSample and EventHubSample pages. These pages are very similar and describes all you can do with this SDK. You will notice, the only difference is in using of different entity artefacts.

      $("#btnSend").click(function () {
            var msgBody = { "message": txtMsg.value, "id": 1234 };
            var props = [{ 'Name': 'Property1', 'Value': 'Hello' }, { 'Name': 'Property2', 'Value': 1234 }]

            var msg = new BrokeredMessage(msgBody, props);

            topicClient.sendMessage(msg, function (messagingResult) {
                $("#result").html(messagingResult.body);
            });
        });


        $("#btnReceive").click(function () {
            clear();
            subscriptionClient.receiveMessage(function (messagingResult) {
                $("#result").html(messagingResult.brokeredMessage.body);
                if (messagingResult.brokeredMessage.properties != null &&
                    messagingResult.brokeredMessage.properties.brokerProperties != null) {
                    $("#msgId").html(messagingResult.brokeredMessage.properties.brokerProperties.MessageId);
                    $("#lockUri").html(messagingResult.brokeredMessage.properties.Location);
                }
                else
                    $("#msgId").html("No messages in queue.");
            });
        });

        $("#btnPeekLock").click(function () {
            clear();
            subscriptionClient.peekLockMessage(function (messagingResult) {
                $("#result").html(messagingResult.brokeredMessage.body);
                if (messagingResult.brokeredMessage.properties != null &&
                    messagingResult.brokeredMessage.properties.brokerProperties != null) {
                    $("#msgId").html(messagingResult.brokeredMessage.properties.brokerProperties.MessageId);
                    $("#txtMsgLockUri").val(messagingResult.brokeredMessage.properties.Location);
                }
            });
        });

        $("#btnComplete").click(function () {
         
            subscriptionClient.completeMessage(txtMsgLockUri.value, function (messagingResult) {
                $("#result").html(messagingResult.brokeredMessage.body);
                if (messagingResult.brokeredMessage.properties != null &&
                    messagingResult.brokeredMessage.properties.brokerProperties != null) {
                    $("#msgId").html(messagingResult.brokeredMessage.properties.brokerProperties.MessageId);
                    $("#lockUri").html(messagingResult.brokeredMessage.properties.Location);
                }
            });
            clear();
        });


        $("#btnAbandon").click(function () {
           
            subscriptionClient.abandonMessage(txtMsgLockUri.value, function (messagingResult) {
                $("#result").html(messagingResult.brokeredMessage.body);
                if (messagingResult.brokeredMessage.properties != null &&
                    messagingResult.brokeredMessage.properties.brokerProperties != null) {
                    $("#msgId").html(messagingResult.brokeredMessage.properties.brokerProperties.MessageId);
                    $("#lockUri").html(messagingResult.brokeredMessage.properties.Location);
                }
            });
            clear();
        });

       $("#btnEventHubSend").click(function () {

            var eventBody = { "temperature": "24", "area": "Frankfurt am Main" };

            var msg = new EventData(eventBody);

            clear();

            ehClient.sendMessage(msg, function (messagingResult) {
                $("#result").html(messagingResult.result);
                $("#eventData").html(JSON.stringify(eventBody));
            });
        });