Writing subscribe function

gomashan07
User offline. Last seen 9 weeks 5 days ago. Offline
Joined: 01/27/2011
Points: 251

Hi all,

               I  have written a publish function in a custom app and coupled that with a basic application and it worked fine. Now i want to  know to write a subscribe function. the documentation was not sufficient  for me and i  was not able to write a subscribe function.

 

 

 
        app.publish("Sample.stock", {
            "company": customer,
            "symbol": symbol
        });

this is my publish function. I  want a help to write the subscribe function. The name of the topic  in the publishing app is  sample.stock and the name of the topic  in the subscribing app is Sample.rec. The name of the prpoerty is symbol as  we see. Please help with  a subscribe function.

 

 

Thanks,

0
Your rating: None
smitchell
smitchell's picture
User offline. Last seen 10 hours 17 min ago. Offline
Joined: 08/29/2008
Points: 34

I'm sorry the documentation wasn't clear enough for you. It sounds like you have a tightly-coupled interaction between your custom App that publishes a topic and another custom App that subscribes to that topic.

The documentation on this type of interaction is at http://www.jackbe.com/prestodocs/v3.2/apps-dev/wiringTightlyCoupled.html. You should use the subscribe(topicName, handlerFunction) method from the Presto App API in your subscribing custom App. The signature for the handler function is: handlerFunction(topiName, message). Go to http://www.jackbe.com/prestodocs/v3.2/apps-dev/wiringTightlyCoupled.html#jbid1162H0E0VY4 to find the sections specifically on Subscribing Apps for tight-coupling.

If you've already seen this and it still isn't helping, we need a better understanding of:

* what you want the subscribing App to do when it receives a published message
* where it is failing, what error message you are receiving, what you are not seeing...

Sara, technical writer/jackbe

 

gomashan07
User offline. Last seen 9 weeks 5 days ago. Offline
Joined: 01/27/2011
Points: 251

    Thanks for the swift response . I  am doing a looseley coupled wiring involving two custom apps. Can  u please  write me a subscribe function and show me for the publish function shown  above. When i tried   i got some errors. I  am getting the syntax but , there is a topic name in he subscribe function and also in handler function.. Any where should i use the publishing app's topic name . I know i am confusing a lot , but please help me clear this confusion. I would be very  happy if u reply with a subscribe function with a handler function in side . i have listed out the properties and the topic name  of both apps in the first thread.

gomashan07
User offline. Last seen 9 weeks 5 days ago. Offline
Joined: 01/27/2011
Points: 251

  I have solved the problem using the receive function and using the prototype syntax.  I was trying to use the subscribe function because i  know the publishing topic and the properties . Can u help  me write the subscribe function for this .publishing topic  name  Sample.stock and property is symbol.

 Sample.dec7= function( app ) {
   this.app=app;
    var root = jQuery( app.getRootElement() );
    var projectTable = root.find(".projectTable1");
    var projectBody = projectTable.find(".tblBody1");
 
//    var rowMarkup = "<tr class='project'>"+
//        "<td class='company'>${company}</td>"+
//        "<td class='symbol'>${symbol}</td>"+
 //       "<td class='domain'>${domain}</td>"+
  //  "</tr>";
 
   // var rowTemplate = jQuery.template("rowTemplate", rowMarkup);
 
    //inner function to publish topics for row click
 
  
 // alert("outside");
 
    //invoke mashup
  var symbol= app.getPropertyValue("symbol");
  //alert(symbol);
   
 };
 Sample.dec7.prototype={
  
  
   receive: function(args) {
    alert("inside");

   var rowMarkup = "<tr class='project'>"+
        "<td class='company'>${company}</td>"+
        "<td class='symbol'>${symbol}</td>"+
        "<td class='domain'>${domain}</td>"+
    "</tr>";
 
    var rowTemplate = jQuery.template("rowTemplate", rowMarkup);
  
      var w = app.width,
            h = app.height;
     root.html('<iframe style="width:'+w+'px;height:'+h+'px;" ' +

                'src="http//:www.googgle.com"'></iframe>');
   
     symbol = args.symbol;
    
        if (symbol) {
          //invoke mashup
          var prestoUrl = "/presto/edge/api/rest/dec7d/runMashup?x-presto-resultFormat=json&input_3="+symbol;
          alert(symbol);
          app.getConnection().request({
              url: prestoUrl,
              type: "get",
              contentType: "application/x-www-form-urlencoded",
              data: ""
            },
            {  onSuccess: function(response) {
                if (response.Resultset.result) {
                    var projects = response.Resultset.result;
                                        
                    projectBody.empty();
                    jQuery.tmpl(rowTemplate, projects).appendTo(projectBody);
 
                    //bind handler to cell click event
               
 
                } else {
                   projectTable.hide();
                }
            },
              onFailure: function(e) {
                  app.handleException({
                      message: 'Failed to find travel information: ' + e.message
                  });
              }
              });
        } else {
  
 
              }
           
            
   },      

};

 

 

 

 

jeremy.pitten
jeremy.pitten's picture
User offline. Last seen 5 weeks 4 days ago. Offline
Joined: 09/22/2008
Points: 275

 You need to subscribe by invoking the subscribe method exposed by the app object which is passed in to your custom app via the constructor, so you could do the following:

Sample.dec7functionapp ){

    app.subscribe("Sample.Stock", function(topic, msg){

          if(msg.symbol && msg.company){

               // do something with your received data

          } else {

              console.warn('expecting symbol and company properties', msg);

         }

    }, this);

}