Search Results

TIBCO eFTL JavaScript Quick Start

Quick Start Contents

Introduction

This quick start guide is intended to provide basic instructions for writing JavaScript applications for TIBCO eFTL. It focuses on a collection of sample applications included in the TIBCO eFTL Messaging SDK for JavaScript.

Getting Started

  • Download the TIBCO eFTL Messaging SDK for JavaScript. This guide will refer to your eFTL installation location as <eftl>.
  • Obtain a URL and authentication key from your TIBCO Cloud Messaging account.
  • In the sample application source files, replace the placeholders TIBCO-CLOUD-MESSAGING-URL and TIBCO-CLOUD-MESSAGING-KEY with your account URL and authentication key.

Building and running samples

Follow the steps below to build and run the JavaScript samples.

  1. Copy <eftl>/lib/eftl.js to the <eftl>/samples/web directory.
  2. Open any of the HTML files found in <eftl>/samples/web/ in a web browser.

Description of sample applications

Publisher

Basic publisher program that demonstrates the use of publishing eFTL messages.

Subscriber

Basic subscriber program that demonstrates the use of subscribing to eFTL messages.

Run the Subscriber and Publisher at the same time to demonstrate real-time messaging. Stop the Subscriber, run the Publisher, and restart the Subscriber to demonstrate persistence.

SharedSubscriber

Basic subscriber program that demonstrates the use of subscribing to eFTL messages using a shared durable.

Run multiple instances of the SharedSubscriber and one Publisher to demonstrate message load-balancing across a shared durable subscription.

LastValueSubscriber

Basic subscriber program that demonstrates the use of subscribing to eFTL messages using a last-value durable.

Start and stop the LastValueSubscriber. Run the Publisher several times. Re-start the LastValueSubscriber to receive only the last published message to demonstrate the use of a last-value durable subscription.

Using the eFTL SDK

For JavaScript programs, include the eFTL JavaScript messaging SDK file on your page.

<script src="eftl.js"></script>

Connect to TIBCO Cloud Messaging

To establish a connection to TIBCO Cloud Messaging, you need the URL and authentication key. Authentication keys can be obtained only from your TIBCO Cloud Messaging account page on the Authentication key tab. The URL tells the eFTL client library where to connect. The authentication key is the password used by the eFTL client library to authenticate itself.

Client Identifiers

You must also provide a unique identifier for your client so that it can receive any missed messages while it is disconnected. Note: Client identifiers must be unique; no two clients with the same identifier can be connected simultaneously.

// connect to TIBCO Cloud Messaging
eFTL.connect("wss://messaging.cloud.tibco.com/tcm/01BT0Q4Q3QMK65FBC80P1MDP5Q/channel",
{ 
   password: "6ee7639011dd67ac79272a0844c16671", 
   clientId: "abc123",
   onConnect: function(connection) { 
      console.log("connected"); 
   },
   onDisconnect: function(connection, code, reason) {
      console.log("disconnected:", reason); 
   } 
});

onConnect callback

The onConnect callback is invoked once the connection to TIBCO Cloud Messaging is established. The connection object passed to it can be used for subscribing to messages and publishing messages.

onDisconnect callback

If the connection cannot be established, or is lost, the onDisconnect callback is invoked.

Publish Messages

After you are connected to TIBCO Cloud Messaging, a client can publish messages. To publish messages, use the connection returned in the onConnect callback.

// create a message
// Fields and arrays of type string, numeric, date, and sub-messages are used in messages.
var message = new eFTLMessage();
message.set("event", "hello"); 
message.set("text", "Hello, World!"); 

// publish a message to TIBCO Cloud Messaging
connection.publish(message);

// To be notified if the publish was successful or if an error occurred, callbacks can be provided.
// publish a message to TIBCO Cloud Messaging
connection.publish(message, { 
   onComplete: function(message) {
      console.log("publish success"); 
   }, 
   onError: function(message, code, reason) { 
      console.log("publish error:", reason); 
   } 
});

Receive Messages

After you are connected to TIBCO Cloud Messaging, a client can create one or more subscriptions to receive messages of interest. You can subscribe to messages by matching on the message fields that are of your interest. Use the connection returned in the onConnect callback to register subscriptions.

// create a subscription in TIBCO Cloud Messaging 
// 
// this subscription matches all published messages that contain a 
// field
named "event" with a value of "hello" 
// 
// this subscription also sets the durable name to "hello" which allows 
// the client toreceive messages that were published while disconnected 
//
connection.subscribe({ 
   matcher: '{"event": "hello"}', 
   durable: "hello", 
   onMessage: function(message) { 
      console.log("message received:", message.get("text")); 
   }, 
   onError: function(subscription, code, reason) { 
      console.log("subscription error:", reason); 
   } 
});

Matcher field

The matcher field specifies the messages that are to be received by matching their content. In this case, the subscription receives all published messages containing a field named “event” whose value is “hello”.

Durable field

The durable field is the unique subscription name used by TIBCO Cloud Messaging to store messages for the client when the client is not connected.

onMessage callback

The onMessage callback is invoked when the content of a published message matches the subscription’s matcher.

onError callback

The onError callback is invoked when an error occurs while registering the subscription, typically because of an invalid matcher or invalid durable.