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:

<script src="//"></script>
<script src="//"></script>
<script src="//"></script>
<script src="//"></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:

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:

var connectionInfo = {
  url: 'wss://', // 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.

  onError,           // Callback function to process errors
  onConnect          // Callback function once connected


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

var subscription = {};

var onSubscribe = function(sub) {
  subscription = sub;

var onConnect = function(conn) {
    '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.

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


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