Reactive server-side method calls

Luke Audie
3 min readSep 30, 2017

--

Minimising data publications and helping improve security

Proposed Data Calling Architecture

My Journey

I love JS, Node and Meteor, but have been facing a few challenges ever since I left my previous job after 10 years. This software company did all its developments in JAVA and really did operate in an old school non-agile manner. To make the transition I really had to start retraining my brain.

For the most part I really do like all the new principles and ways of doing things, but there are a couple of items I really do not light, for example:

  • Over reliance on non-blocking asynchronous calls (there are times where sequence is really important and cannot be architected away)
  • Lots of data being stored and available on the client side (e.g. pub/sub, and I know you can restrict fields etc., but still)
  • All your javascript being freely available for all clients to see (i.e. sometimes you need to perform “top secret” activities)

My Solution

Objectives:

  1. Easily call server methods from the client using straight forward coding patterns
  2. Store minimal data on the client
  3. Have client-side rendering (therefore do not need tonnes of server power), but at the same time perform some of the heavy lifting on the server to help speed up loading times (hybrid approach to rendering)
  4. Being able to hide sensitive code from the client by keeping it on the server

Taking the above challenges and objects in mind I propose the following.

Using Promises on the Client in Meteor

You must check out Rob’s post on how to call server methods directly from the client using promises and async/await. It is a really elegant solution.

With this approach, it means we can push all our client-side functions onto the server and call them in a synchronous manner when needed. The first key to my solution.

Adding Reactivity

The challenge I then faced was reactivity. Really did not want to lose the advantages of the current pub/sub approach. So I took the Meteor package included in Robs post and extended it.

For more info on my new extended package check out the following Git:

Package can be installed by typing the following in your Meteor project folder:

meteor add goldenpassport:reactive-promise-call

Some example code to help illustrate how to use the solution.

Outcomes

Our template html and js pages are now lean and mean, with only the core UI related functions existing on the client. All the data processing, manipulations and complex computations can now be all performed on the server.

Room for Improvement

Sure, always! The key one for now is that the package utilises Meteor.setTimeout to poll the sever method. It works well, but could be extended to include things like “Observables”.

Update: 03/10/17: The performance I’m getting from using Observables is not nearly as good as the promise approach. Starting to really like promises!

Thanks for reading!!!

--

--

Luke Audie

Management Consultant that Lives and Breaths Processes and Customer Experiences