TIBCO eFTL™ JavaScript Quick Start
Contents
- Getting Started
- Building and running HTML samples
- Building and running NodeJS samples
- Description of sample applications
- Using the eFTL SDK
- Connecting to TIBCO Cloud Messaging
- Client Identifiers
- Publishing Messages
- Receiving Messages
- Related Links
Getting Started
This quick start guide provides basic instructions for writing TIBCO eFTL applications in JavaScript for TIBCO Cloud™ Messaging.
- Download the TIBCO eFTL Messaging SDK for JavaScript. This guide refers to your eFTL installation location as
<eftl>
. - Download a client configuration file using the roles REST API or user interface. In the sample application source files, replace the placeholders
TIBCO-CLOUD-MESSAGING-URL
andTIBCO-CLOUD-MESSAGING-KEY
with theeftl_url
andtcm_authentication_key
from client configuration file - Review the eFTL API documentation.
Building and running HTML samples
Follow the steps below to build and run the JavaScript samples:
- Copy the eFTL JavaScript file into the web sample directory:
cp <eftl>/lib/eftl.js <eftl>/web
- Open any of the HTML files found in
<eftl>/web/
in a web browser.
Building and running NodeJS samples
Follow the steps below to build and run the Node.js samples on Linux and macOS:
- Change directory into the node samples:
cd <eftl>/node
- Install websocket library:
npm install ws
- Copy the eFTL JavaScript file into node_modules directory:
cp <eftl>/lib/eftl.js <eftl>/node/node_modules
- Run sample:
node subscriber.js
Description of sample applications
Publisher
Publisher is a basic publisher program that demonstrates the use of publishing eFTL messages.
Subscriber
Subscriber is a 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
SharedSubscriber is a 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
LastValueSubscriber is a 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>
Connecting to TIBCO Cloud Messaging
The client configuration file contains all the information client applications need to securely connect to TIBCO Cloud Messaging. Generate the client configuration file using the roles REST API or user interface. Generate as many configuration files as needed for each Role.
Note: TIBCO Cloud Messaging samples require a client configuration file to run.
Client Identifiers
You must provide a unique identifier for your client if you want it to receive any messages missed while disconnected. Only one application can connect with a given client id at any given time. If a second application connects with a client id already in use by another application, the first application will disconnect.
Connection Example
// connect to TIBCO Cloud Messaging
eFTL.connect(<eftl_url>,
{
password: "<tcm_authentication_key>",
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.
Publishing 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);
}
});
Receiving 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 the message fields of 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.