22 Replies Latest reply on Mar 25, 2015 10:59 PM by tahir_mirza

    Ajax call to node.js server with some user data from jive app

    pawans

      Hi Ryan/ Aron,

      Some more questions as I am exploring more possibilities of the jive-sdk.

       

      I deployed Ryan's admin essential app on our jive7 uat instance.

      It works perfectly.

      Now I am trying to send some data from this app to the node.js server  (like user_id selected by the user picker) and then depending on some response from the node.js server, perform a new action on jive app.

      Is this possible (ajax call to node.js server)? Or is jive apps only capable of performing actions client side using javascript apis?

      If yes, can you provide a suitable code example?

        • Re: Ajax call to node.js server with some user data from jive app
          tone

          Hi Pawan,

           

          Sorry for swooping in on this discussion, but I have some input to share.

           

          Until the official answer from Ryan Rutan or Aron Racho, you might want to consider the following:

           

          I'm not 100% clear what the domain organization is with regard to the server and Jive apps, but what I was told in Extending Events Functionality?, I do know is that Jive serves as a proxy, allowing the use of the Jive JavaScript API to issue asynchronous calls to Jive, even though the app itself is hosted on the external (node.js) server. You might already be familiar with that, though.

           

          Given that, if the app itself still belongs to the external server domain, I don't think you should run into any CORS restrictions if you run raw jQuery AJAX calls to your server. But in the event that you do, this might help (haven't tried it yet, but it's worth considering): javascript - How to allow CORS in Express/NodeJS? - Stack Overflow

          • Re: Ajax call to node.js server with some user data from jive app

            You cannot make direct ajax calls using things like the jQuery $.ajax function.

             

            Your friend here is the osapi.http API that is provided by the Jive addon/apps framework. That way you won't have to worry about CORS issues.

             

            Here is an example of an HTTP get via the osapi API:

            osapi.http.get({

              "href": "https://yourserver.domain.com/api/dosomethingwiththeuserId?userId=1234"

            }).execute(

              function(response) {

              }

            );

             

            response will be the json response from your server

            1 person found this helpful
              • Re: Ajax call to node.js server with some user data from jive app

                What Nils Heuer, I thought I had replied to this yesterday.  Despite Anthony Isaac uncertainty, his instincts are spot on...when using the OSAPI functionality, CORS should not be an issue to any domain.  Let us know if you have more questions Pawan, and apologies for the delay here.

                • Re: Ajax call to node.js server with some user data from jive app
                  pawans

                  Thanks a lot Ryan, Nils HeuerRyan Rutan and Anthony Isaac.

                  I was just writing a post when i saw  these two responses.

                   

                  I guess this is what I was looking for.

                  Let me try it and let you know how it goes on.

                   

                  Just some info on what exactly I am trying to do (sorry for the long post. this needs some patience).

                   

                  We have a customized streamonce service for Thoughtworks since Thoughtworks uses enterprise google mail/services heavily.

                  In addition to the custom streamonce, we did some extra magic from our side to integrate google groups with jive.

                   

                  We have a plugin which does following

                  • creates google group when a jive group is created
                  • disable google group when a jive group is deleted
                  • add user to a google group when that user starts following corresponding jive group in email enabled stream
                  • remove user from google group when that user stops following corresponding jive group from an email enabled stream

                  Streamonce does the magic of mapping jive and google groups and also two way communication between the two.

                  We have this setup working on prod since several months now on jive 6.0.4.

                  With this the google groups are now managed through jive.

                   

                  Our group admins wanted ability to force add users as members of jive as well as to the email groups.

                  And since we are upgrading to jive 7 in a month's time, we thought of using the new jive-sdk for this.

                  Ryan's admin essential add-on helped here.

                  His app allows group admins to force add users as members.

                  I added an extra logic where the app detects the user's email watches stream and associates it with the group.


                  Now the second part works only for system admins.

                  The reason is only system admins can get details of a user's streams using api.

                  The add as member part (by Ryan) is not affected since that part of logic does not require user to be super admin.

                  And javascript apis authenticate as the logged in user (which in this case is a group admin, not super admin)

                  So my logic failed for group admin.

                   

                  So now my next idea is to get the user_ids from the user picker in Ryan's app and send it to the node server which owns the app.

                  This node server, using an admin account will associate group to stream using jive apis and send success response.

                   

                  This is a crazy logic. But critical for our custom streamonce.

                  I will try what you  all have mentioned.

                  In mean time of you guys have better solution to associate a user's stream with the group, please do share.

                    • Re: Ajax call to node.js server with some user data from jive app
                      pawans

                      For people who want to know in short what I am trying to do (without reading my essay )--

                      I am trying to add extra logic to the force add users feature of Ryan's admin essentials app.

                      I want to associate the current group to a specific stream of every user selected in Ryan's plugin.

                      That too as a group admin, not super admin.

                        • Re: Ajax call to node.js server with some user data from jive app

                          Yep...you should be able to make an OSAPI request to your node service, similar to what Nils shared in a previous discussion:

                          osapi.http.post({

                            "authz" : "signed"

                            "href": "https://yourserver.domain.com/.../.../../.",

                            "body" : [ 1234, 5684, ... ]

                          }).execute(

                            function(response) {

                            }

                          );

                          this will sign the request, such that you can verify it came from the right source.  All you have to do is read the body of User IDs and perform the appropriate API calls on them.  Hope that helps =)

                            • Re: Ajax call to node.js server with some user data from jive app
                              pawans

                              Hi Ryan,

                              Thanks a lot.

                              I could make a simple ajax call on local instance with above requests with osapi.http.post.

                              One last question.

                              As I understand, when we upload extension.zip to jive site, jive gets the details to contact the node.js server where the jive-sdk code is running.

                              The html / js / css required for the app is loaded into jive app run time and not when the add-on extension.zip is uploaded to jive.

                               

                              Now at this stage the javascript/html is part of jive instance.

                              How can I make the node.js server client url (from meta.json or jiveclientconfiguration.json) available to this javascript?
                              I have hard coded it right now in the app.

                              I need to ensure the app gets the correct url from some config (the proxy url in prod and local url in test instance).

                               

                               

                              I have used node.js a lot, but this combination of loading files generated by node.js into other app (jive instance here) and then getting some configs is confusing me a bit.

                              • Re: Ajax call to node.js server with some user data from jive app
                                pawans

                                Hi Ryan,

                                Last time when you posted this answer, I had tried making a request using osapi.http and it worked.

                                I had not tried posting data yet, which I tried today.

                                Here's the code snippet :

                                var options = {

                                            'authz': "signed",

                                            'href': "http://localhost:8090/tasks",

                                            'body': '{"name":"mytask"}'

                                        };

                                        osapi.http.post(options).execute(function(resp) {

                                                    console.log(resp);

                                             }

                                            );

                                 

                                On nodejs server side I have this test route --

                                 

                                app.post('/tasks',function(req, res){

                                     console.log(req);  

                                    res.send("Success");

                                });

                                 

                                 

                                The request does come through, but the data in body tag never reaches node.

                                If I try ajax call, it works. I do get the data on server side.

                                 

                                        $.ajax({

                                            type: "POST",

                                            url: 'http://localhost:8090/tasks',

                                            data: {'name':'mytask'}

                                        });

                                 

                                I have tried many many ways to put that data in osapi.http.post.

                                The request is always successful, i can even send back some response. but the body/data of the request never reaches the server.

                                 

                                Any idea how osapi.http.post can send data?

                                I checked osapi documentation. But it didn't help.

                                  • Re: Ajax call to node.js server with some user data from jive app

                                    Make sure you set the correct content type and format on the request:

                                               osapi.http.post({

                                                        'href': 'https://server/api/postSomething",

                                                        'body': data,

                                                        'headers': { 'Content-Type': ['application/json'] },

                                                        'format': 'json',

                                                        'authz': 'signed'

                                                    }).execute(function (response) {

                                                       //do something with the response

                                     

                                     

                                                    });

                                    1 person found this helpful
                                    • Re: Ajax call to node.js server with some user data from jive app

                                      Set the content-type header to application/json and see if that helps.  At first glance that should work

                                        • Re: Ajax call to node.js server with some user data from jive app
                                          pawans

                                          Hi Ryan / Nils Heuer,

                                          Previously I had tried  header : {'content-type': 'application/json'}.

                                          This did not work. After I saw reply from Nils, I tried the exact case and added type in array. This worked.

                                          Not sure why the previous one should not work.

                                          But now the second one is working.

                                           

                                          In addition to sending data from jive app to server for some processing, I am also trying to use this for logging the apps action to server.

                                          Like in admin essentials, for success as well as errors, I am logging this to server side to track stuff happening from the app.

                                          Exploring the possibilities of jive-sdk, loving it

                                            • Re: Ajax call to node.js server with some user data from jive app
                                              pawans

                                              Hi Ryan Rutan/Nils Heuer,

                                              Sorry to trouble again.

                                              After been able to implement the above code successfully, i tried the osapi.jive.connects.post

                                               

                                              First I added this tag in my app.xml for my test service

                                                      <Require feature="jive-connects-v1">

                                                          <Param name="alias:somealias|title:My Service">jive:service://myservice</Param>

                                                      </Require>

                                               

                                              And then tried sending this request

                                                      osapi.jive.connects.post({

                                                          'alias' : 'somealias',

                                                          'href' : '/tasks',

                                                          'headers' : {'Content-Type': ['application/json'] },

                                                          'body' : data

                                                      }).execute(function(response) {

                                                              console.log(response);

                                                          }

                                                      );

                                               

                                              In this case, the body param is itself missing from request I receive on node server (in previous case i was getting empty body).

                                              I do get the request for sure and I am able to send back a random response which is received successfully in app side

                                              .

                                              I went through all the documentation.

                                              Does connects api have a different format to send data in post?

                                                • Re: Ajax call to node.js server with some user data from jive app
                                                  pawans

                                                  To add to this, lets say I have the node-sdk running at localhost:8090. And the jive app belongs to this service.

                                                  And I have one more random node server running at localhost:4000.

                                                  Both these servers have the same routes.

                                                   

                                                  If I register localhost:4000 as service for connects api, then I am at least able to call the server from my app.

                                                  If I register localhost:8090 as service, I get "Connection Refused" error.

                                                  Is there a limitation on connects api where I cannot register the node service which is running the app?

                                                    • Re: Ajax call to node.js server with some user data from jive app
                                                      pawans

                                                      Hi Nils Heuer / Ryan Rutan / anyone else,

                                                      Any hints on this jive connects API issue?

                                                      Has anyone used osapi.jive.connects.post with some data?

                                                        • Re: Ajax call to node.js server with some user data from jive app
                                                          pawans

                                                          Hi,

                                                          Got the solution on this.

                                                          I am a bit surprised with this difference.

                                                          When I use osapi.http.post, the headers need to be set as 'headers' : {'Content-Type': ['application/json'] } to send body in post request

                                                          When I use osapi.jive.connects.post, the headers need to be set as headers : { "Content-Type" : "application/json" }, to send body in post request

                                                           

                                                          The first one does not work for second option and vice-versa.

                                                          osapi.http.post requires content types in array and connects api required content type without array.

                                                          If these headers are not set as mentioned above, then the body won't be passed in the request.

                                                          A bit surprised with this difference. But finally got this working.

                                                            • Re: Ajax call to node.js server with some user data from jive app
                                                              pawans

                                                              So to summarize, if you want to make an ajax call to node.js server with some user data (in request body and not query params) from jive app, here's the code

                                                               

                                                              For osapi.http.post

                                                               

                                                              osapi.http.post({

                                                                                  'href': 'https://server/api/postSomething",

                                                                                  'body': data,

                                                                                  'headers': { 'Content-Type': ['application/json'] },

                                                                                  'format': 'json',

                                                                                  'authz': 'signed'

                                                                              }).execute(function (response) {

                                                                                 //do something with the response

                                                               

                                                               

                                                              for osapi.jive.connects.post

                                                               

                                                              osapi.jive.connects.api({

                                                                                 alias: "someAlias",

                                                                                  'body': data,

                                                                                  'headers': { 'Content-Type': 'application/json' },

                                                                                  'format': 'json',

                                                                              }).execute(function (response) {

                                                                                 //do something with the response

                                                               

                                                              difference is the way to set content type, which is a bit strange

                                        • Re: Ajax call to node.js server with some user data from jive app

                                          Hi Ryan,

                                          I wanted to hit the follwoing url using osapi.http.get from a custom view tile  but i am facing the error"Uncaught SecurityError: Blocked a frame with origin "https://app-abc.jiveon.com" from accessing a frame with origin "https://abc.jiveon.com". Protocols, domains, and ports must match. "

                                          in the following code:

                                          var urlJson=hostUrl+"/api/core/v3/places?filter=type(group)&fields=-resources,placeID,displayName&filter=search(" + groupName + ")";

                                            var getParams = {

                                                 'href' : urlJson,

                                                 'authz' : "signed",

                                                 'format' : 'json',

                                                 "noCache" : true

                                            };

                                          osapi.http.get(getParams).execute(function(res){

                                            console.log("groupObject");

                                            console.log(res);

                                            });