Build a "Serverless" Live Blog System with Flybase, Twilio and StdLib

code, Twilio, StdLib

Flybase and Twilio work great together, we've posted several posts before about using these two services in one place, but what about if you want to go serverless?

StdLib.com is excellent for that. In their own words, StdLib is essentially a package manager for APIs that’s built upon new “serverless” architecture, meaning you never have to worry about managing servers or allocating resources for scale.

All you have to do is write a function or two (or three), deploy, and you’re done, and this is perfect for quickly building services with Flybase.

We've covered building a Live Blogging tool before, so I wanted to use that tool here and show how to make it work inside StdLib.

Setup

To start, you need an account with our three services:

  • Twilio to handle receiving text messages.
  • Flybase to store our data and output it to viewers in real-time.
  • Stdlib to handle the backend processing between Twilio and Flybase.

First install StdLib:

npm install lib.cli -g

Now, create a folder called stdlib and login:

$ mkdir stdlib
$ cd stdlib
$ lib init

Ok, time to create the liveblog service:

$ lib create liveblog
$ cd <username>/liveblog
$ lib function:create sms -n
$ mkdir pages
$ npm install --save async ejs flybase twilio

Where <username> is the username you are logged into StdLib with.

You will now have the following structure:

  • functions/__main__.js
  • functions/sms/__main__.js
  • pages/

Let's give this structure some code and make it work for us.

Open the file called env.json in the root of the project, it was created automatically for you, and copy the following:

Populate these variables with your Twilio and Flybase information.

Now, open functions/__main__.js and copy the following code:

This will set up our index, which will output your live blog posts.

Next, open functions/sms/__main__.js and copy the following:

This is our webhook to receive text messages. It will take the text message and any photos from Twilio and store it in your Flybase app, it will then send a reply to your phone to tell you the message was received.

Finally, we need to create our pages/index.ejs file:

This template file will be read by our index and used to display any useful information.

Get online

To put this app online, all we have to do is type:

lib up dev

This will upload your app and make it available at the following url:

https://<username>.lib.id/<service>@dev/
https://<username>.lib.id/<service>@dev/sms/

Where <username> is your StdLib username and <service> is the service you've created, in this case that would be liveblog.

Once you're done developing, you can also use

lib up -r

To publish your app in release (prod) mode. The URLs would then be:

https://<username>.lib.id/<service>/
https://<username>.lib.id/<service>/sms/

Let's stick to just dev mode for now.

Once this has been published, you can go to the https://<username>.lib.id/<service>@dev/ URL and see your live blog, there won't be any content right now, so let's create some using Twilio.

Go to your Twilio account and create a phone number, then in the settings for that number, add your /sms/ URL:

https://<username>.lib.id/<service>@dev/sms/

Remember to update <username> with your username and <service> with the name of the service.

And now you are ready to send SMS messages to your live blog and see them on the screen.

Finishing Up

You can see the full code here at the repo, download it, extend it, play with it, do whatever you wish.

This is a pretty basic example of using StdLib, Flybase and Twilio together, but it shows some of what you can do.

You can also just drop the index file into any webpage and have it output your live blog posts, since Flybase works great that way, so it's really up to you on how you want to use it.