New Startup Scripts Preview Feature in Azure Mobile Services

Azure Mobile Services today allows you to rapidly build mobile apps using table scripts and custom APIs for server side functionality. However for certain advance scenarios it is often required to hook into the core of the mobile service backend to handle and respond to events that is otherwise not possible. To make such scenarios possible, we’ve introduced the Extensions framework. Extensions is a new preview feature where by you should be able to write code that we’ll invoke throughout life cycle of your application. To install an extension you need to have Source Control enabled and drop a script with pre-defined name into the ‘extensions’ folder inside ‘service’ folder.

First extension point that we’re introducing is called ‘Startup’ script. This is a script file with name ‘startup.js’ that exports a method called ‘startup’. We invoke the startup method when your application starts. Your application starts on the first request after it is created, restarted or has been idle for about half an hour. Startup method has following signature:

 

 exports.startup = function(context, done) { 
 // write your logic here 
 done(); 
};

 

Here context is an object that exposes certain key members that would allow you to write interesting startup scripts. The members are as follows:

  • app: This is the expressjs app object. This object gives you complete control over your application. You can write custom routes, middleware or even use socket.io to do realtime communication.
  • tables, config, etc.: All the services that you get from request.service in custom api, are also exposed directly from the context.

With access to the expressjs app object, you are only limited by your imagination in terms of what you can build with your mobile service. We’re really excited to see what you do with startup scripts. Give them a try and share your comments and suggestions with us.