2.10. JavaScript inter application communication

JavaScript Inter Application Communication is designed to allow applications within a page to exchange data. This library is made for broadcasting messages on topic.

It is based on three functions:

A subscription to a topic will receive any subtopic messages. For example, an application subscribed to "/eXo/application" will receive messages sent on the "/eXo/application/map" topic. A message sent on "/eXo", however, would not be received.

Subscript ion Topics

Library

The Inter Application Communication library is found in JPP_HOME/gatein/gatein.ear/eXoResources.war/javascript/eXo/core/Topic.js

/**

 * publish is used to publish an event to the other subscribers to the given
channels
 * @param {Object} senderId is a string that identify the sender
 * @param {String} topic is the topic that the message will be published
 * @param {Object} message is the message that's going to be delivered to the
subscribers to the topic
 */
Topic.prototype.publish = function(/*Object*/ senderId, /*String*/ topicName,
/*Object*/ message ) { ... }
/**
 * isSubscribed is used to check if a function receive the events from a topic
 * @param {String} topic The topic.
 * @param {Function} func is the name of the function of obj to call when a message
is received on the topic
 */
Topic.prototype.isSubscribed = function(/*String*/ topic, /*Function*/ func) { ...
}
/**
 * subscribe is used to subscribe a callback to a topic
 * @param {String} topic is the topic that will be listened
 * @param {Function} func is the name of the function of obj to call when a message
is received on the topic
 *
 * func is a function that take a Object in parameter. the event received have this
format:
 * {senderId:senderId, message:message, topic: topic}
 *
 */
Topic.prototype.subscribe = function(/*String*/ topic, /*Function*/ func) { ... }
/**
 * unsubscribe is used to unsubscribe a callback to a topic
 * @param {String} topic is the topic
 * @param {Object} id is the id of the listener we want to unsubscribe
 */
Topic.prototype.unsubscribe = function(/*String*/ topic, /*Object*/ id) { ... }
Topic.prototype.initCometdBridge = function() { ... }

Syntax

The three messaging functions require particular objects and definitions in their syntax:

Example of JavaScript events usage


<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<portlet:defineObjects/>
<div>
  <p>
    Received messages:
    <div id="received_<portlet:namespace/>">     ssss

    </div>
  </p>

  <p>
    Send message:
    <input type="text" id="msg_<portlet:namespace/>"/> <a href="#"
onclick="send_<portlet:namespace/>();">send</a>
  </p>
</div>
<script type="text/javascript">

  Function.prototype.bind = function(object) {
    var method = this;
      return function() {
      method.apply(object, arguments);
    }
  }

  function send_<portlet:namespace/>() {
    var msg = document.getElementById("msg_<portlet:namespace/>").value;
    eXo.core.Topic.publish("<portlet:namespace/>", "/demo", msg);
  }

  function Listener_<portlet:namespace/>(){

  }

  Listener_<portlet:namespace/>.prototype.receiveMsg = function(event) {
    document.getElementById("received_<portlet:namespace/>").innerHTML =
      document.getElementById("received_<portlet:namespace/>").innerHTML + "<br
/>* " +
      event.senderId + ": " + event.message;
  }

  function init_<portlet:namespace/>() {
    var listener_<portlet:namespace/> = new Listener_<portlet:namespace/>();
    eXo.core.Topic.subscribe("/demo",
listener_<portlet:namespace/>.receiveMsg.bind(listener_<portlet:namespace/>));
  }

  init_<portlet:namespace/>();
</script>
Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus