Integrate your app with server-side logic and enjoy the benefits

Yesterday we published some updates to our documentation. We have just introduced a new way of writing your app's logic: now you can write it in the server-side. But why on earth are you going to write the logic in the server-side if writing it in your app's code is so easy? Well it has some important benefits:

  • Writing the business logic in the server-side lets you change it at any moment without needing your users to upgrade your application. And you avoid the problem of having users with old versions of your aplication (and thus old business logic that can be incompatible with your current rules).
  • You can write the business logic once and invoke it from any platform. So you don't need to re-implement it for every platform and worrying about edge cases that may differ between platforms, etc.
  • You can avoid using API keys completly. API keys are like passwords, if someone guesses your password he can access your information. Supporting the ability to write app code without API keys avoids this problem.

How can you write server-side logic? It's easy, you create controllers in the "Views and controllers" section as you do while developing a hosted web app with backbeam. The difference is how you write in the response. When rendering the result of a query using a HTML you would do something like this:

// supposing we have an 'item' entity'item').fetch(100, 0, function(err, items) {
    response.render('template.html', { items: items })

Now you need to send those items to the SDK but in a format the SDK can understand. This is new, and it's super easy. Instead of writing a callback function just pass the response object and backbeam will generate a JSON response that the SDK will understand:

// supposing we have an 'item' entity'item').fetch(100, 0, response)

You can try it in your browser. Write a controller similar to this and open it in your browser and you will see a JSON response with the same structure than the REST API.

The most awesome part is that the SDKs and the server-side part exchange information about the users authentication. So from the server-side you can access the current authenticated user using backbeam.currentUser() and if you change the authenticated user in the server-side or you log out the current user, the SDK will know about it. So there is a full integration between the SDKs and the server-side code.

If you want to have full control over what it is sent to the application's code you can also just send plain JSON and have the same benefits on the users authentication stuff:

// supposing we have an 'item' entity'item').fetch(100, 0, function(err, items) {
    response.json({ count: items.length }) // very simple custom JSON response

Additionally in the server-side you can determine if the request has been made from an SDK using backbeam.sdk(). With this ability you could also do conditional rendering (sending HTML or not depending on who invoked the controller). Learn how to do conditional rendering and much more in the documentation.

Finally, how do you invoke controllers from your application's code?? Very simple. This is an example on how you can do it from an iOS application (more information in the iOS SDK documentation):

[Backbeam requestObjectsFromController:@"/" method:@"GET" params:nil fetchPolicy:BBFetchPolicyRemoteOnly success:^(NSArray *objects, NSInteger totalCount, NSArray *distances, BOOL fromCache) {
    // Now do whatever you want with the array of BBObjects
} failure:^(NSError *error) {
    NSLog(@"error %@", error);

This is an example on how you can do it from an Android application (more information in the Android SDK documentation):

Backbeam.requestObjectsFromController("GET", "/", null, FetchPolicy.REMOTE_ONLY, new FetchCallback() {
    public void success(List<BackbeamObject> objects, int totalCount, boolean fromCache) {
        System.out.println("Success!! "+objects.size());