Roda + Mutations + Jbuilder = Perfect fit for JSON API

RubyI have been a big Ruby fan for many years and during my Ruby journey I have used various frameworks like Rails, Sinatra and Cuba.

When we started to develop Kontena, a tool to manage and deploy Docker containers, we needed some a simple framework to handle our server-side API. Cuba seemed to fit perfectly to our needs, since it is lightweight and benchmark results have been quite impressive. Then we found Roda framework. Roda is originally a fork from Cuba and seems to solve some Cuba pain points.

The main difference between Roda and Sinatra is that Roda uses a routing tree instead of list of routes. Because of this Roda is 2.5 times faster than Sinatra for small applications.

What is especially nice on Roda, it has a plugin system and all parts of Roda can be overridden by plugins.

How Roda framework is used on Kontena

We are using Roda framework on a Kontena Server component. Kontena Server is a component that controls what is happening on Docker host nodes. We have structured our code quite a conventional way:

app/
….jobs/
….models/
….mutations/
….routes/
….services/
….views/
config/
spec/
config.ru
server.rb

The backbone of application consists of API endpoints that are handled by Roda routes. If you have lots of API endpoints, your route file might grow relatively large. To avoid this issue, we have used Roda’s multi_route plugin to keep route files small and clean:

# /v1/services/:id/containers
r.on ':id/containers' do |id|
  load_grid_service(id)
  r.route 'service_containers'
end
V1::ServicesApi.route('service_containers') do |r|
  # GET /v1/services/:id/containers
  r.get do
    r.is do
      @containers = @grid_service.containers
      render('containers/index')
    end
  end
end

Also in order to keep our route files small and simple, we have used mutations gem to handle business logic of operations. By using mutations gem we achieve thin routes and thin active models and separate business logic into testable units that we can test easily

Since our API returns JSON, we are using Roda’s JSON plugin for simple responses. The JSON plugin allows match blocks to return arrays or hashes, and have those arrays or hashes be converted to JSON, which is used as the response body.

For more complicated responses we are using Jbuilder gem, which works also with Roda framework very well.

base.plugin :render, engine: 'jbuilder', ext: 'json.jbuilder', views: 'app/views/v1'
r.get do
  r.is do
    @grid_service = grid_service
    render('grid_services/show')
  end
  ...
end

So far Roda, Mutations and Jbuilder combination has been worked very well for our needs. We have gain clean code structure, testable code base and good performance for our needs.

Kontena is 100% open source. If you have ideas, bug fixes, improvements,  all contributions are welcome!

Advertisements

Qt Developer Days 2014 Slides: A Solid Back End for a Solid Qt Mobile Application

My second presentation at Qt Developer Days 2014 in San Francisco was about how to build a scalable and reliable server side application with Qt Cloud Services using PaaS (Platform as a Service).

Abstract

Nowadays many mobile applications are relying more and more on server-side applications. Setting up servers and deploying a server-side application can be hard and time-consuming, but it has not to be.

Qt Cloud Services contains three different services that can be utilised to implement a robust and scalable server-side solution easily. Managed Application Runtime (MAR) is used to deploy and run applications on the cloud, Enginio Data Stroage (EDS) can be used to store data and Managed WebSocket (MWS) service makes it possible to send and receive WebSocket messages. By combining these three services it is possible to build and deploy a server-side solution without configuring any server machine.

In this presentation an example Qt mobile application demonstrates how to implement a real-time Twitter stream provided by a server-side application that is running on MAR. The server-side application stores tweets to EDS and sync new tweets to the client via WebSocket protocol.

This presentation covers:
– overview of MAR, EDS and MWS services
– basics how to create MAR, EDS and MWS instances
– example how to implement a server-side application that stores data to EDS and provides an API.
– example how to deploy the application to MAR
– example how to connect to the server-side application from Qt client application
– example how to send data to the client application with MWS.

Qt Developer Days 2014 Slides: Give a push to your Qt application with WebSockets and Qt Cloud Services

I was at Qt Developer Days 2014 in Berlin and San Francisco this year. I gave there couple of presentations about Qt Cloud Services. The first one was about WebSockets and how you can utilise them very easily with Qt Cloud Services and Qt.

Abstract

WebSockets is an advanced technology that makes it possible to establish an interactive communication session between an online client application and a server. With WebSockets you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

Qt 5.3 introduced new Qt WebSockets module that features full support for WebSocket protocol (RFC-6455). To benefit WebSocket technology with Qt, a server-side implementation is needed to control the message flow. Qt Cloud Services offer two different ways to achieve this goal:
– Managed WebSocket (MWS)
– Managed Application Runtime (MAR)

MWS is a fully managed WebSocket service that will automatically scale with users and supports build-in access control and policies.

MAR is a managed application platform-as-a-service that provides all the tools needed for server-side development.

This presentation covers:
– Overview of WebSockets and common use cases
– Overview of MWS and MAR services
– Basics how to create MWS and MAR instances
– Examples how to benefit MWS in order to send and receive WebSocket messages in a Qt client application
– Examples how to deploy a Qt WebSocket server to MAR and use it in a client application

Using Backbone.js models and collections with Qt and QML

Backbone QML TodoI have been a big fan of Backbone.js a long time. Together with Marionette.js it offers a solid toolkit to front-end web developers.

Lately I have worked with Qt and QML. With QML you can create nice user interfaces very easily, but what I have missed is that easy integration to backend applications. So I thought what if I try to use models and collections of backbone.js. I was quite a skeptical first, because backbone.js depends on jQuery with uses window and document objects that are not working in Qt. However I decided to try it.

Instead of jQuery, I grabbed Zepto.js that offers the same API as jQuery. I tuned the library slightly to get rid of errors related to window and document objects. There were not many of them.

Then I took underscore.js that backbone.js uses and finally backbone.js itself. I made slight changes to get them initialise correctly, but nothing big changes there.

After that I only needed to implement a simple application where I could test that everything is working. The result was much better than I expected. You can use directly Backbone’s collections and models with your Qt application and even listen events in your QML file, they just work.

https://github.com/nevalla/qml-backbone-todo

Define backbone models and collections
// app.js
var baseUrl = "https://api.engin.io/v1";
var Todo = Backbone.Model.extend({

});

var TodosCollection = Backbone.Collection.extend({
model: Todo,
url: baseUrl+”/objects/todos”,

parse: function(response) {
return response.results;
}
});

2. Use models and collections in QML file

//main.qml
function completeItem(index) {
var todo = App.todos.at(index)
todo.save({completed: true}, {
success: function(model) {
todoModel.setProperty(index, "itemProcessing", false);
},
error: function(){ console.log("error")}
} )
todoModel.set(index, todo);
}

3. Listen collection events in QML file

//main.qml
ListModel {
id: todoModel
}
Component.onCompleted: {
App.todos.on("add", function(todo) { todoModel.append(todo) });
App.getTodos();
}

So the first impact was very positive. I haven’t tested all functions of Backbone.js yet, so there may be some errors. However I don’t believe there are any fundamental errors that prevent using Backbone.js.

How to start multiple rails projects just by one command

If you do serious Rails programming, you may have multiple projects for your application. For example you may have one project for an API and another for a web UI. You might also use some background processing like sidekiq etc. Every time you need to launch these services on a development environment, you have to launch all projects in separate consoles manually.

Fortunately there is a better way to do it. By using a gem ‘subcontractor’ together with ‘foreman’ gem, you can define all the projects and services that you want to launch in the ‘Procfile’, for example:

Then you can launch all the projects and services just by one command:

foreman start

Android support ViewPager holds reference to old fragments

I was implementing a feature where I have a service that retrieves data on background and an activity receives results and updates one of the fragments with new data.

Everything went well until I rotated the screen and the application was re-created and fragments and fragments pager was initialized again. After that new data did not show up. At that point I was totally uncertain what was wrong. At first I was thinking the problem was with the service, but it turned out that nothing was wrong there. After struggling a while I noticed that the fragment that was  re-created was not attached to the view at all.

That led me to this Stack Overflow question and one answer provided a solution for me as well. The problem lies on Compatibility library v.4 ViewPager (bug report) combined with FragmentPagerAdapter. In some level ViewPager or FragmentPagerAdapter holds references to old fragments and does not refresh them correctly.

One working solution is to use FragmentStatePagerAdapter instead of FragmentPagerAdapter. However I resolved my issue with the help of this answer.

I implemented getFragmentTag() method in my FragmentPagerAdapter class

private String getFragmentTag(int pos){
    return "android:switcher:"+R.id.viewpager+":"+pos;
}

change R.id.viewpager to match with you ViewPager compnent’s id.

Then in my Activity I find reference to the Fragment like this.

...
this.mPagerAdapter  = new MyPagerAdapter(getSupportFragmentManager(), fragments, titles);
this.pager = (ViewPager)super.findViewById(R.id.viewpager);
this.pager.setAdapter(this.mPagerAdapter);
int position = 3;
this.myFragment = (MyFragment)getSupportFragmentManager().findFragmentByTag(mPagerAdapter.getFragmentTag(position));
...

And after service returned new data I simply call

...
myFragment.updateData(data);
...

Hopefully this saves someones time to figure out what is wrong instead of hours of investigation.

How to run Jasmine javascript tests in CI server (Ruby)

Jasmine.js is a great testing framework for Javascript. However running tests in Continuous Integration (CI) server is somehow problematic because the server usually does not have a screen where to open a browser.

The solution is to use Xvfb and headless gem:

Headless is the Ruby interface for Xvfb. It allows you to create a headless display straight from Ruby code, hiding some low-level action. It can also capture images and video from the virtual framebuffer.

To run your tests in CI server, do the following steps:

  1. Install xvfb
    $ sudo apt-get install xvfb
  2. Install firefox
    $ sudo apt-get install firefox
  3. Install headless gem
    $ gem install headless
    or
    add it to Gemfile
    gem ‘headless’
    and run
    $ bundle install
  4. create rake task for headless jasmine runner (https://gist.github.com/nevalla/5591233)
  5. run or add rake task to your build configuration
    rake jasmine:ci:headless