Getting Started

JavaScript Universal Facade API

Simple pubsub example

In this tutorial, clients subscribe to a channel and receive any messages published to that channel.

Follow the steps below to learn the client SDK, or see the final result in action.

Create the client

Add the client libraries to your application from the hosted CDN:

Copy
        
<script src="//cdn.kaazing.com/releases/enterprise.javascript.client/4.1.0/WebSocket.js"></script>
<script src="//cdn.kaazing.com/releases/enterprise.javascript.client/4.1.0/AmqpClient.js"></script>
<script src="//cdn.kaazing.com/universal-facade/JavascriptUniversalClientNPM.js"></script>
<script src="//cdn.kaazing.com/universal-facade/AmqpUniversalClient.js"></script>
        
      

(You can also get the client SDKs in other ways from the download page.)

Create the client. Specify 'amqp' as the protocol for now:

Copy
        
var client = UniversalClientDef('amqp');
        
      

Establish a connection

To make it easy to get started, you can connect to Kaazing's hosted sandbox for testing purposes. The details are shown here:

Copy
        
var connectionInfo = {
  url: 'wss://sandbox.kaazing.net/amqp091', // URL to Kaazing's public sandbox for WebSocket testing
  username: 'guest',
  password: 'guest'
};
        
      

In the future, if you install Kaazing WebSocket Gateway locally, then you can change the connection details to correspond with your installation.

Now you are ready to connect. The connect() function has additional callback parameters for when the connection is established or when an error occurred.

Copy
        
client.connect(
  connectionInfo,
  onError,           // Callback function to process errors
  onConnect          // Callback function once connected
);
        
      

Subscribe

In this example, we will subscribe once the connection is established.

Copy
        
var subscription = {};

var onSubscribe = function(sub) {
  subscription = sub;
  log('Subscribed');
  ...
};

var onConnect = function(conn) {
  log('Connected');
  conn.subscribe(
    'getting-started',    // Topic to send messages
    'getting-started',    // Topic to receive messages
    onMessage,            // Callback function to process received messages
    false,                // noLocal flag - 'false' allows you to receive your own messages
    onSubscribe           // Callback function once subscribed
  );
};
        
      

Line 4 remembers the subscription object. That will be used later to publish messages on this subscription.

If the noLocal flag on line 15 is true, then you will not receive your own messages that you publish. In this example, it is set to false so that we see our own messages.

Receive messages

The subscribe() function above took a callback function to receive messages: onMessage(). This function will be invoked anytime a message is received on the getting-started channel.

Copy
        
var onMessage = function(msg) {
  console.log('Received: ' + msg.message);
};
        
      

Publish

Copy
        
subscription.sendMessage({ message: 'Hello, world!' });
        
      

Putting it all together

Click the button below to run the whole example. Open multiple browser tabs and see that each one receives all messages being published.

Run this demo