Categories
Community Learning XPages XPages

Tim explains JSON-RPC #codefortim

First I want to highlight OpenNTF’s recent post regarding Tim’s projects.  Let me simply say that I think the #codefortim idea is brilliant. Pure genius and so appropriate.  Tim shared.  Period. I just read that post like 5 minutes ago so I’m going to let it sink in before I talk any more about it.

Which brings us to the topic of today.  This email exchange I’m going to post was forwarded to me by my friend Dan.  He asked Tim a question and as usual got a great response back.  I’m so very thankful he sent this to me.  What make this really special is just how recent it is.  It was only a couple of days before Tim passed.  This is the last email I have to publish.  If anyone has any similar emails that they’d be willing to share I’d be very happy to publish them as well. Next up is some videos.

Dan asks:

On May 7, 2014 at 4:30:20 PM, Daniel wrote:

Hi Tim,

I was looking at an answer you gave Naveen on Stack Overflow on the use of the JSON-RPC control.

http://stackoverflow.com/questions/11356227/how-to-use-xejsonrpcservice

From how you describe the control, it sounds like an AJAX operation. Is my understanding correct?

I’m thinking of an application for it and I wonder if you could tell me if it’s the right tool.

I have an Xpage I’m building that accepts input from a proximity scanner. When an ID is scanned (badgeNo), I’d like to be able to query and SQL db to get the person’s EmpID number and use that to display his image on the screen. Would the JSON-RPC control be appropriate for that kind of operation?

Thanks,

Dan

Tim responds:

From:        Tim Tripcony

To:        Daniel

Date:        05/08/2014 02:15 AM

Subject:        Re: JSON-RPC control


In my opinion, that use case is almost definitely a good fit for JSON-RPC.

RPC stands for “remote procedure call”. Simply put, you’re telling the server to run a method — and how to run that method (in other words, passing method arguments) — and send back a result. So yes, this is AJAX, but it’s slightly different from partial refresh events in XPages.

Partial refresh events execute server-side code, of course, but upon completion they send back a specific visual representation of a portion of the page (namely, HTML) because that portion of the page might need to look different afterward as a result of the execution of that code. The archetypal example of this is an onChange event in a combo box that alters which select values are now valid for an additional combo box further down the page. As you’ve no doubt already seen in action, the platform automatically generates the JavaScript for you that causes the AJAX call to tell the server what code to run, but also handles taking the HTML that is returned and updating the affected portion of the page.

Similarly, submission of any data the user has entered is also automatic. While there are ways to override exactly how much data is sent for each specific event, the default is to just post the entire form to the server. This facilitates the “statefulness” of XPages — with each event, the server state of the page is essentially synchronized with what the client state now is, so both the browser and the server have some level of awareness of the user’s behavior, and both can account for that current state.

JSON-RPC, by comparison, might appear rather manual at first glance. Adding one of these components to your page and defining one or more methods is essentially defining an API that you can then call… but you still need to then add client-side code to actually call it, and also to respond to whatever data the server sends back to the browser. There are a few bits of good news in this regard, however:

– The platform still auto-generates some client-side JavaScript for you. To be precise, a global JavaScript object is created client-side and populated with methods that map to whatever server-side methods you defined for the RPC. These methods are even aware of what arguments you defined for each.

– The performance of this type of service is incredibly lean for two reasons. One reason is that it sends the absolute minimum amount of data in both directions. To be precise, it sends only identifiers for which service is being called and which method it should run, as well as the value of any arguments; in return, it sends back only whatever value you explicitly return from whichever method is called. So instead of posting the values of potentially dozens or even hundreds of fields and getting back a big blob of HTML, it might send only a single number (e.g. badge ID) to the server and get back a single string (e.g. employee ID)… although the response object can be as simple or complex as you want.

The other reason it’s faster is because the default behavior is to not save the component tree after an RPC method runs. It’s assuming that you’re doing something very similar to the use case you described, so there’s no need to re-serialize the entire page state, because you’re not sending a bunch of form data that should update a backing model bean, etc.; rather, you’re just sending it a badge ID and asking it to send back a corresponding employee ID, so there shouldn’t be a need for it to “remember” that it did once it has.

For both of these reasons, JSON-RPC tends to be lightning fast when compared to most partial refresh events, even under low-bandwidth / high latency conditions.

Here’s some pseudo-code to give you a feel for what this might look like if you decide to take the JSON-RPC approach. Somewhere in your XPage you’d have component markup that looks similar to the following:

<xe:jsonRpcService id=”scannerRpc” pathInfo=”scanner”>

<xe:this.methods>

<xe:remoteMethod name=”getEmployeeId” script=”return getEmployeeIDFromSQL(badgeId);”>

<xe:this.arguments>

<xe:remoteMethodArg type=”number” name=”badgeId” />

</xe:this.arguments>

</xe:remoteMethod>

</xe:this.methods>

</xe:jsonRpcService>

The assumption, of course, is that the getEmployeeIDFromSQL() function is already defined somewhere — presumably, in a SSJS library. So for what you’re describing, all that might really need to change in the above example is to alter the type of the badgeId argument (if, for instance, that should be a string, not a number) or to change the getEmployeeIDFromSQL reference to whatever code will be responsible for actually performing the SQL query, whether that be SSJS or perhaps a method of some Java bean.

The other half of this, then, is defining client-side code to call the remote method defined above. If that’s triggered by a “Search” button, for instance, you might end up with something like this:

<xp:button value=”Search” id=”button1″>

<xp:eventHandler event=”onclick” submit=”false”>

<xp:this.script><![CDATA[var badgeId = someCodeToGetIdFromScanner();

scanner.getEmployeeId(badgeId).addCallback(function(employeeId){

dojo.byId(“#{id:employeeId}”).attr(“value”, employeeId);

});]]></xp:this.script>

</xp:eventHandler>

</xp:button>

You might, of course, be setting the innerHTML of a div instead of the value of an input, but hopefully the above example illustrates the basic premise:

– Because the JSON-RPC service lists “scanner” as the value of its “pathInfo” attribute, the browser will automatically be aware of a global JavaScript object called “scanner”.

– Because the RPC service defines a remote method named “getEmployeeId” that accepts a single argument named “badgeId”, the global client-side object has a function called “getEmployeeId” that is also expecting a single argument.

– Calling that client-side function does not immediately send the AJAX request; rather, it returns an object that has a .addCallback method. Calling this method does immediately trigger the AJAX call… and passing a function to addCallback() defines what code will run when the AJAX call returns. Since we’re just expecting to get back a single string value, we specify that the function that we pass to addCallback() accepts a single argument, which we can then use inside the function to somehow update the page or otherwise inform the user that we now know what the scanned employee’s ID is.

No doubt there’s a myriad of valid approaches for your specific use case, but I’ve become rather fond of JSON-RPC for this type of functionality, both because of the relative ease of implementation and because of the comparative performance. Hopefully this overly verbose explanation gives you an indication of whether or not the JSON-RPC approach would, in fact, be a good fit for what you’re trying to do. 🙂


For anyone reading this information that’s interested in the JSON-RPC stuff.  I believe it’s come up on NotesIn9 a couple times as well. So there should be some examples available.

#codefortim

Categories
Community XPages

Tim Explains: SSJS Object Persistence

Below is an email thread between Serdar Basegmez, Tim Tripcony and myself. Serdar reminded me of it and re-sent it to me so I could share.  It’s a little dated but I’m reposting this because the quality of the information should be shared.

This is pretty much the marks the beginning of when I came to grips and realized that if I wanted to use XPages in a similar manner to how I developed for the Notes Client I needed to look past ServerSide JavaScript and focus on Java.  I liked building my applications around Custom Classes in LotusScript.  This email explains how that is no longer possible to do in XPages and that if I wanted to continue my client development “comfort zone” I needed to use Java for that.

Now this realization did not happen over night for me.  I believe this email came in right after my famous battle with Phil Riand. Where I said I’ll never use Java and SSJS should be made better and the “bug” should be fixed.  And he said it’s not really a “bug” and to use Java. we went back and forth a bit and I ended up losing royally and now I write with Java.  haha

 

So anyway here’s Serdar’s original question:

Categories
XPages

The Great XPages Mystery Solved.

Disclaimer: Everything in this post comes from Declan Lynch.  He doesn’t really blog anymore but I felt this important enough to share so I’m just trying to put out what info I know.

So we had a big problem at the day job this week.  Our main server was getting hammered with what looked like a memory leak.  This server houses our main business application that Declan wrote as well as the iPad BarCode Scanner stuff that I wrote.  So naturally the question was asked of me:

“What code updates have you pushed to production in the last week?”

A perfectly fair question.  Apparently he’s actually read some of my code.. who knew?

Anyway as luck would have it I’ve promoted nothing for a little while. I’ve been working on some new stuff that’s not ready. I could even use SourceTree to go back and find the date of my last promotion and it was far enough away that I was pretty much in the clear!  Talk about having a good alibi!  haha

Anyway – as best as I can tell Declan soon figured that is was not a memory leak but an unexplained CPU spike.  Something was happening that was killing the server.  And let me tell you, this is a BEEFY server. But still the CPU and HTTP Task was getting HAMMERED.

So what caused this almost Catastrophic problem?

The Safari Web Browser.  Specifically the “Top Sites” feature. From two users.

Here’s how Declan described it with the needed fix:

 

what I thought was a memory leak wasn;t a memory leak, it was Safari’s Top Sites feature on two different users machines. Safari has a feature called Top Sites, as you visit different web sites it adds the site to the Top Sites list.

So for these users it added our internal site because they use it a lot..  Safari is also trying to be helpful, by pre loading the site to show a preview. Except in our case the sites are locked down so there is a redirect to a login page so Safari tries loading it again.

and again, and again, and again

about 6 times a second safari was hitting our server. Driving the CPU usage up till the site became unresponsive for users. Once we removed the site from the two users top sites listing the server settled back down and is behaving normally again.

and now for a fancy workaround…

you can tell the browser what content to serve if it is being loaded by a ‘preview’ function using the following code.

if(window.navigator&&window.navigator.loadPurpose===”preview”){window.location.href=”http://some.other.content/in/a/folder.ext”};

so you could redirect the preview to a totally different site or you could redirect to an image

 

How did he diagnose this and fine the issue?

He had to put the XPages Toolbox on the production server. With the backend monitor he noticed all the hits to the same login page over and over. So he went to the users machine and used netstat to confirm that it had a connection opened to Safari.  Then he closed safari and saw the connection drop, But the connection came back on just opening Safari without going to out website.  So he saw the Top Sites listing and removed it.  No more connections.

He got the users IP address by running ‘tell http show thread state’ on the domino server.  That shows all the treads and IP Addresses.

WOW

He does other tricks as well.  I can struggle with something for a whole day and when I do break down and ask for help he often has the solution before I’m even done explaining the problem.

Yep.  I have the best boss in the world! It’s like working with Sherlock Holmes.

 

 

Categories
Community XPages

XPages: The Best of Times,The Worst of Times

If you’re reading this you probably already know that I like working with XPages. It’s a great tool that lets me provide business solutions to my company in a timely manner. There’s also a really really good community behind it. A lot of great people sharing wonderful information these days and I’m happy to try and contribute a little bit to it.

But I think the XPages community has suffered a big  loss lately and I thought it was worth mentioning. Recently, at least 3 KEY IBMers have moved on or out of the XPages realm.

We lost Paul Hannan a little bit ago as he moved onto new challenges in IBM. Notes in the cloud or something. Honestly I forget what it was. So he’s still close, but out of the XPages App Dev world.

A couple of days ago Phil Riand announced he was leaving IBM for something new. As far as I know Phil has been the driving force behind not only XPages itself, but the Extension Library, the very recent XPages4Bootstrap project with Mark Leusink, and I’m sure a whole lot more. Heck it was pretty much a single tweet from Phil that got the XPages Extension Library book on it’s path. And who was the key IBMer with that book? Paul Hannan!

Then today Simon O’Doherty announced he was moving to the Watson team. Don’t know who Simon is? He’s a level 2 support engineer at IBM for Notes/Domino App Dev issues. But even more then that Simon went out of his way to answer questions to the community. A quick check on Stack OverFlow shows him answering 166 questions! Wow! He does not get enough credit for his contributions to Notes/Domino App Dev.

I want to personally thank Paul, Phil and Simon for their contributions to the community.  I know they will succeed in whatever they do.

But with 3 very engaged and high profile IBMers leaving the XPages world that just seems concerning to me.

Clearly the Worst of times?

Or maybe not.

We also got out a brand new book this week. Mastering XPages 2nd Edition! Written by the App Dev team itself. There’s a TON of new content in it and I’m looking forward to digging into it. Maybe now I’ll finally understand the JSF Lifecycle. haha

In other news Mark Leusink recently updated the very important XPages Bootstrap project. Oliver Busse released a helper project for that – a template that I’m interested to check out. http://mardou.dyndns.org/hp.nsf/blogpost.xsp?documentId=C0E

Over on OpenNTF there’s been recent releases of the Unplugged XPages Mobile Controls, XPages Extension Library, XPages OpenLog Logger and more.

In my little corner of the world people still watch NotesIn9. The recent Java/JavaScript throwdown show was very well received, I have a show from Tim Tripcony to process and tons of my own show ideas that I just need to get time for. It astounds me that my Introduction to XPages show from way back in 2010 is still typically in the top 5 show downloads for the month. I have to believe that this is an indication of new people trying to get up to speed on XPages. I would like to revisit and update that show some point soon.

Clearly the Best of Times?

Which is it? The Best or the Worst of Times?

Ok so some key IBM’ers left. That in itself is not the end of the world.  I guess my biggest concern there is these three were EXTREMELY outgoing to the community. I really hope additional IBMer’s step up and take their place otherwise it’s a huge loss.

I hope IBM realizes that with their official XPages documentation “lacking”, and their plan of Documentation by “Community wiki” somewhat, shall I say, “misguided”, that the best thing they can do is encourage their key experts to engage in the community so we can get questions answered and information published. IBM preaches Social. I hope they know it’s not just about budgets, and spreadsheets. It’s not just about writing Social software but its also important for IBMers to interact with customers and business partners to share knowledge and mutually work together to make something that’s better then the sum of it’s parts.

I’m hopeful that what is happening is that XPages is just catching its breath. Some people want new challenges and others are hopefully waiting in the wings. If any IBMer wants to share something and needs help in publishing via a blog post, NotesIn9 or something else then please just let me know. I’m happy to assist with that. Just reach out. I’m pretty easy to find.

For me, I’m hopefully just catching my breath as well.  I have the best job in the world but man am I busy.  Home life isn’t any slower as the kids school year starts the final stretch.  But I’m trying to gear up for a push to at least get to 150 shows and maybe another surprise or two as well.

I’m going to call it the Best of Times.

Categories
Notes In 9 Podcast XPages

NotesIn9 142: Adding a Please Wait to XPages

In this show it’s back to XPages and demos!  I will do a quick recap on the last show as 141 seemed REALLY popular but then it’s back to work.  In this show I demo a quick tip on how you can use CLIENT SIDE JavaScript to show the user the server is processing.

UPDATE:

Fredrik Norling (@XPageDeveloper)  tweeted me a link to an OpenNTF Snippet designed to handle Partial Refreshes:

http://openntf.org/XSnippets.nsf/snippet.xsp?id=standby-dialog-custom-control

Also, Eric Tomenga sent me a nice email with even another solution.  This one looks like it uses the Ext. Library.

http://xpagesera.blogspot.com/2012/05/add-ajax-loading-control-in-xpages-for.html

I actually did look for a jQuery version as well since I’m using this inside the Bootstrap project.  I couldn’t find one but I’m sure there’s something out there.

I do love having options.

Categories
Java Learning XPages Notes In 9 Podcast XPages

NotesIn9 141: Java vs JavaScript Throwdown

This is a VERY DIFFERENT NotesIn9.  It’s less of a normal show and more if a response to a recent blog post regarding one persons opinion on the value of Java in XPages and the Value of the UI vs the backend. Actually this show is more of a really long “rant”.

This show could absolutely suck.  I pretty much broke all my rules and tried to have fun with it.  I do give my thoughts on things so maybe there’s at least a little value there.

There’s no Demo.  There is only the trainwreck.

I would say that if you do give it a shot.  Stick around for the end. That’s when I get my most immature.  For the first time ever there’s even a few bloopers at the very end.  No not me, but one of the cast members.

I want to thank Mark Roden for the original post.  Please check out the comments.  There’s a lot of good information in there.  Here’s the shortlink:

http://xpag.es/?WTF

I hope you like it.

 

Categories
Notes In 9 Podcast XPages

NotesIn9 140: SourceTree Deep Dive

In this episode Paul Withers comes back on the show for a deeper look at using SourceTree and Git Flow. This comes from the session he did with Declan Lynch at IBM Connect.  In that session they ran out of time so this should fill in some of the missing pieces that they didn’t get to cover.

I use SourceTree and HG Flow which is the Mercurial version of Git Flow in my Day Job all the time.  While there are some quirks due to Designer and you do need to be careful and understand what’s going on, it is a really nice Source Control solution and I recommend it to any Developer.

Information on the original SNT session which included slides on setting up local servers if you don’t want to use something like BitBucket or GitHub can be found here:

http://www.intec.co.uk/show-103-source-control-an-end-to-end-solution/

P.S. I’m sitting at the Philly airport and uploaded this show from my iPhone.  Don’t ya just love technology sometimes?  haha

Categories
Notes In 9 Podcast XPages

NotesIn9 139: XPages Single Copy Design

In this show, Andrew Barickman, a great new contributor comes on to talk to use about the performance benefits of the “Single Copy Design”.  He focuses on the performance benefits, not only for a web application but also for XPages In the Notes Client – XPiNC.

Categories
Java Notes In 9 XPages

NotesIn9 138: XPages Combobox ImprovementsJ

In this show I take a look at using a Combobox inside XPages.  I’m going to demo how to use Java – but it’s NOT scary Java – I show you everything.

We’re going to show the user a pretty value in the combobox but get a hold of and store the key value for our use.

I’m also going to show how you can create categories inside the combobox. That’s kinda cool I think depending on your situation.

Update:

Oliver Busse – @zeromancer1972 – has another example of this on his blog.  http://mardou.dyndns.org/Privat/osnippets.nsf/id/OBUE-9C5KDN

In that post he’s reading the data in from a view.  Cool!

 

Update 2:

Ove Stoerholt just did a blog post on comboboxes.  He discusses one “Gotcha” that you need to avoid.  A really good post!

 

Categories
Notes In 9 XPages

NotesIn9 137: XPages and Web Services

In this show, new contributor, Fredrik Norling comes on to give a neat demo on how to use Web Services with XPages.

 

The demo database and code will be available on his website as well as OpenNTF.org.

Update:  Here’s a link to Fredrik’s blog post with the links.  http://www.xpagedeveloper.com/2014/calling-web-services-from-xpages