The Freshworks app development platform includes a serverless environment that enables you to build serverless (back-end) apps. Front-end apps run in response to events such as button clicks or page loads that happen on the Freshcaller product’s UI (client). You can also create serverless apps that run in response to events such as app installation and uninstallation, events such as call establishment that occur in the Freshcaller product, or events that occur in an external product or service. To do this, you can configure event listeners that invoke callback methods. Serverless computing does involve servers, but they are abstracted away from developers. When a configured event occurs, the app logic in the callback method is run on a server, with the help of the event-specific payload passed to the callback method.
You can use the Server Method Invocation (SMI) feature, to build front-end apps that invoke a serverless component.
- The serverless apps are run in the sandbox mode. Therefore, usage of methods such as process, buffer, clearImmediate, clearInterval, clearTimeout, setImmediate, setInterval, and setTimeout in the app logic is not supported.
- The app execution timeout is 20 seconds.
Create a Serverless App
After you install NVM, Node, and the Freshworks CLI, to create a serverless app:
- From the command line, navigate to the empty directory in which you want to create the app.
- Run the following command: $ fdk create --product freshcaller --template your_first_serverless_app Or $ fdk create -p freshcaller --template your_first_serverless_app
The following app related files are created, based on the your_first_serverless_app template:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | ├── README.md ├── config | ├── iparams_test_data.json │ └── iparams.json ├── manifest.json └── server ├── lib │ └── handle-response.js ├── server.js └── test_data ├── onAppInstall.json ├── onAppUninstall.json ├── onCallCreate.json ├── onCallUpdate.json ├── onExternalEvent.json └── onScheduledEvent.json 4 directories, 12 files |
DIRECTORY/File | Description | ||
---|---|---|---|
README.md | Contains all information, code-related specifications, and additional instructions pertaining to the app. | ||
server/ | Contains all the directories and files that define the serverless app and are used to test it. The JS file follows the ES5 standard. | ||
server/lib | Contains the libraries, with helper methods, that can be used in the server.js file. | ||
server/server.js |
Contains the events registered and their corresponding callback methods. The app logic resides in the callback methods’ definitions. The event registration and callback methods are placed in the exports code block, to ensure that the code is imported and run by the server. The default server.js file contains a callback method corresponding to the onCallCreate event. The app logic in the callback method accesses the ID of the agent to whom the call is assigned, from the event payload passed to the callback method and prints a “Call assigned to <assigned_agent_id>” message on the terminal window. |
||
server/test_data | Contains sample payloads for all the events configured in the server.js file. The sample payloads are used in the local testing of apps. You can modify the content of the JSON files in this directory. Ensure not to modify the names of the JSON files. | ||
config/ | Contains the installation parameters related file. | ||
config/iparams_test_data.json | This file is deprecated. Before testing an app, navigate to https://localhost:10001/custom_configs and enter appropriate values for the configured installation parameters. |
||
config/iparams.json | Contains all the installation parameters whose values are set when the app is installed. For more information, see Installation Parameters. | ||
manifest.json |
Contains details such as the platform version the app uses, product to which the app belongs, event declarations for the app, the Node.js and FDK versions used to build, test, validate, and pack the app, and npm packages that the app uses (dependencies).
Copied
Copy
For product events, <eventName> is the appropriate product event name. For app setup events, <eventName> is onAppInstall or onAppUninstall. For external events, <eventName> is onExternalEvent. For scheduled events, <eventName> is onScheduledEvent. |
When creating a serverless custom app, in your app logic (in server.js), you can include the process.env.ENV environment variable to run different app logic in the test and production environments. To do this, you can use the following syntax:
Copied Copy1 2 3 4 5 6 7 | if(process.env.ENV === "test") { //do something } or if(process.env.ENV === "production") { //do something else } |
For a demonstration of this, see the Freshdesk serverless sample app.
Specify Dependencies
In the server.js file, you can use popular npm packages (at least 50 downloads) to leverage the back-end functionalities provided by the packages. You can also modularise and maintain the app-specific code by using helper methods that are available in certain libraries.
The default serverless app created based on the your_first_serverless_app template, uses the helper.js library file.
npm Packages
For information on how to set up a Windows environment before using npm packages, see Environment setup and configuration.
To use an npm package:
- Navigate to the manifest.json file and specify the package name and corresponding version as a dependency.
- Navigate to the server.js file and load the dependencies by using the require() method.
Format to load dependencies Copied Copy1var req = require("request"); Note: Ensure to use the require() method as the first call and not inside functions.
Format to specify the request package as a dependency Copied Copy
1 2 3 | "dependencies": { "request": "2.72.0" }, |
For example npm packages and their usage as dependencies, see External Libraries.
Libraries
To use libraries that contain helper methods:
- Store the requisite library files in the server/lib directory.
- Navigate to the server.js file and load a library file by using the require() method.
Format to load the handle-response library file Copied Copy1var handler = require("./lib/handle-response");
Test the App
- To test a serverless app, use the latest version of Chrome.
- As testing is only a simulation of events, actual data or record associated with the event is not created in the back-end. To create actual data and then test your event, publish your app as a custom app and test the events manually.
- Ensure that the JSON files that contain the sample payloads to test events, are available at <app's root directory>/server/test_data.
To simulate an event and test your app:
From the command line, navigate to the directory that contains the app related files and run the following command.$ fdk run
In the address bar of the browser, enter https://localhost:10001/web/test. A dialog box is displayed.
Click Select an event. The list of all events configured in the server.js file is displayed.
Note: To test app setup events, select onAppInstall or onAppUninstall. To test external events, select onExternalEvent. To test scheduled events, select onScheduledEvent. To test a product event, select the corresponding product event’s name. To test the default serverless app created based on the your_first_serverless_app template, select onCallCreate.Select an event. The corresponding payload is displayed. To test a different scenario other than the default, edit the payload.
Click Simulate. If the event is successfully simulated, the Simulate button changes to a Success button. If the event simulation fails because of invalid payload data, the Simulate button changes to a Failed button. Modify the payload appropriately and click Simulate.