Photo by Paul Hanaoka on Unsplash

That code deployed serverless

How can Nimbella send upgrades to a Flutter app

Filippo Valle
Level Up Coding
Published in
4 min readApr 5, 2021

--

Every mobile application at a certain point needs an upgrade, bugfixes and new features have to be delivered to users. I recently realised how it is simple to use serverless to provide upgrades to existing apps.

Serverless is a cloud-native development model that allows developers to build and run applications without having to manage servers.

Once deployed, serverless apps respond and automatically scale up and down as needed. Finally, when a serverless function is sitting idle, it doesn’t cost anything.

I will discuss an example of a serverless function that answers calls from mobile applications written with Flutter.

Flutter

I considered Flutter as the framework to develop mobile applications (By the way, it was recently protagonist of a big upgrade and it can be used for desktop applications too).

Anyway, the serverless considerations in the next sections are agnostic about this and they can be easily applied to others frameworks.

Nimbella

To interact with the mobile app, it is necessary a server which provides informations about the current version and about the files to perform the upgrade. This can be done in many ways, one of the advantages of using serverless functions is that they are online only when really necessary. They can scale easily and are often cheaper than a standard server.

There are many way to deploy a serverless function, one of them is Nimbella.

Nimbella offers a free service with some limitations, but enough to start playing with it. Moreover deploying to Nimbella is extremly easy and fast. One has only to write a code with a main function that answers when the server is called.

Files tree

An example of Nimbella project is the following one.

serverlessApp
|____packages
| |____project.yaml
| |____myApp
| | |____getVersion
| | | |____.gitignore
| | | |____appcast.xml
| | | |______main__.py

A folder serverlessApp contain one package myApp with a function getVersion. getVersion contains __main__.py with the main function of this system.

Project file

All the specification of the project are inside a project.yaml file. The runtime is python3 and the web key is set to false: we are not going to deploy a web app.

bucket:  
strip: 1
packages:
- name: myApp
web: false
actions:
- name: getVersion
runtime: python:3
limits: timeout: 60000

__main__.py

The core of this system if the function main. It reads a file and returns an HTTP body with headers. The Content-Type header is set to application/rss+xml since the function is supposed to broadcast an RSS feed.

def main(params): 
with open("appcast.xml") as file:
appcast = file.read()
return {"body":appcast,
"headers":{"Content-Type":"application/rss+xml"}
}

appcast.xml is a file containing the specs for the updated app. According to Flutter documentation it is wrtitten following the sparkle schema .

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:sparkle="https://.nimbella.io/api/MyApp/getVersion"> <channel>
<title>MyApp - Appcast</title>
<item>
<title>Version 2.0.0</title>
<description>New awesome version</description> <pubDate>Thu, 11 Mar 2020 12:00:00 +0000</pubDate> <enclosure url="https://url/to/app/file" sparkle:version="2.0.0" sparkle:os="android" />
</item>
</channel>
</rss>

Deploy serverless

When all the files in the directory are in the right place it is sufficient to runnim project deploy serverlessApp and after few seconds the project will be deployed to the Nimbella infrastructure and ready to be queried by the app installed in different mobile phones.

Bonus: the infrastucture will automatically scale based on the number of requests, no matter if one or thousands of mobile phones will query it, it will always answer properly.

One needs the url of the just deployed service, this is available with the command nim action get myApp/getVersion --url

The upgrader package

The upgrader package from flutter provides all the necessary stuff to check for new updates and eventually download the upgraded version of our app.

It is necessary to create an AppCastConfiguration with the url to the Nimbella url.

final appcastURL = ‘https://.nimbella.io/api/myApp/getVersion'; final cfg = AppcastConfiguration(url: appcastURL, supportedOS: [‘android’]);

Then in the process of building our main interface it is sufficient to encapsulate the existing code into an UpgradeAlert widget.

UpgradeAlert( appcastConfig: cfg, 
debugLogging: true,
child: YourWidget())

Conclusion

That’s all, from now on it will be sufficient to update the appcast.xml file and deploy the new project to Nimbella et voilà every device will be triggered by the new update.

--

--

Interested in physics, ML application, community detection and coding. I have a Ph.D. in Complex Systems for Life Sciences