Taxonomy Icon

Serverless

Run serverless functions with stock market data at rest

Get the code View the demo

Summary

For a long time, cloud computing has helped developers move from an on-premises solution to a managed server to deploy apps. Moving apps to the cloud saves both money and time. Now, with serverless computing, developers no longer need to manage servers. All you need to do is push your code in a serverless platform and it runs on the events that you want, or you can run them directly with REST APIs. With this approach, you can quickly bring your code into production. You have more time to spend on programming your application, and after your app is done, you don’t need to worry putting it in the cloud, or managing a server for your app.

Compared to other cloud computing solutions, a serverless approach not only saves time. It also saves costs of provisioning and running a server on the cloud. Your code is run only when your app is in use, and you are not charged for idle time. You only pay for the resources you use when the code runs. A serverless platform is beneficial for startups that expect a low user base on early stages of their apps, and for all developers who are working to meet their customers’ needs.

This code pattern demonstrates one of the many use cases of serverless computing with an example of a stock market application. The code uses a time-based schedule to run the serverless functions. Serverless cron jobs (using the cron software utility) are useful for processing data at rest (for example, data in a Cloudant database). The application stores the stocks that your application users choose to follow in the database. A serverless function is configured to run every day at a specific time (although the users can change it.) One serverless function calls an external API to get market prices of the stocks and news about the stocks that the users followed. It notifies the users of the changes on the market prices on the stocks with the option to choose a threshold. The users are notified through Slack or an SMS that uses the Twilio API. This code pattern also uses Watson Natural Language Understanding to gain sentiments and emotions on the news articles about the stocks the users follow.

You can try working with this code pattern and see how you might implement your own cron jobs using serverless functions.

Description

The application demonstrates IBM Cloud Functions (based on Apache OpenWhisk) with cron jobs with data at rest in a Cloudant database. The use case demonstrates how actions work with data services and execute logic at specific times in the cron job configuration.

One function, or action, is triggered by an event (in this use case, cron triggers). This action gets data from the database and gets the news and stock market data of the companies from an external API (the IEX API). The action invokes two more functions. One function passes the news to Watson Natural Language Understanding to get its sentiments and emotions found in the article. The other function notifies the user of the app about the recent change of price in either SMS (Twilio) , Slack, or both.

When you use this code pattern, you learn the following skills:

  • Create and deploy Cloud Functions.
  • Trigger Cloud Functions using cron in the alarm trigger.
  • Use Watson Natural Language Understanding in a cloud function.
  • Process data at rest using serverless computing.

Flow

flow

  1. The users enter the stock ticker they want to follow.
  2. The alarm trigger is set on a time-based schedule using the cron utility. It invokes the get-stocks-data action on that time set.
  3. The get-stocks-data action gets the stock tickers in the Cloudant datbase and uses an external API to get market prices and news about each company.
  4. The get-stocks-data invokes two more actions: the notify and send-to-nlu actions.
  5. The notify action sends notifications about market changes to the users through SMS or Slack notification.
  6. The send-to-nlu action sends the news to Watson Natural Language Understanding to get sentiments and emotions.
  7. The sentiments and emotions are stored in the database so the users ca view the data.

Instructions

Find detailed technical steps for this code pattern in the README.md file in the GitHub repository.

  1. Clone the repo.
  2. Create IBM Cloud services.
  3. Configure external services for notifications.
  4. Deploy Cloud Functions.
  5. Start the application.