Deploy to a serverless function
info
Deploying to serverless functions is currently in private beta, available in the Node SDK. Please contact us if you would like us to enable it for your organization, or if you would like to see this functionality in other SDKs.
The Interval architecture relies on a constant two-way connection with your action host to make writing rich apps as simple as just writing functions. While this makes reasoning about actions and their state straightforward, it does not fit into serverless workflows.
An alternative approach is to only establish the two-way connection when a user initiates an action. Incoming requests will connect, complete your action workflow, and then disconnect, meaning each action is stateless and completes within the span of the request.
warning
Ensure that your serverless function timeouts are sufficient enough to complete the transaction from start to finish.
Your action state is still stored within the body of your action function. If your serverless function shuts down before completion the transaction will be terminated.
Installation
info
Starting with v0.22.0, in addition to being added to the private beta, you
must import the Interval class from @interval/sdk/dist/experimental
to
enable the serverless features.
Your serverless function endpoint will receive a HTTP POST request with a JSON
object as its body.
This object must be deserialized and passed to the Interval
class's
handleRequest
method, which will handle communication with Interval.
Your endpoint should return an HTTP status code of 200 upon successful
completion, or any non-200 error code if unsuccessful.
import path from "path";
// Note the experimental import path
import { Interval } from "@interval/sdk/dist/experimental";
function handleRequest(request) {
const interval = new Interval({
endpoint: "wss://<YOUR INTERVAL SERVER URL>/websocket",
apiKey: "<YOUR API KEY>", // get an API key from the Keys page in your Interval dashboard
routesDirectory: path.resolve(__dirname, "routes"),
});
const body = JSON.parse(request.body);
const successful = await interval.handleRequest(body);
}
As you can see in the example above, the request only prompts the function to create a connection to Interval. If the request body contains a valid secret single-use token then the transaction will begin after the connection is established. If not, nothing will happen.
info
Serverless functions can only be deployed with a live mode API key.
HTTP handler functions
If your infrastructure uses standard Node HTTP handler functions, you can
use the httpRequestHandler
helper function which will retrieve the input and
send the response for you.
import path from "path";
import http from "http";
// Note the experimental import path
import { Interval, io } from "@interval/sdk/dist/experimental";
const interval = new Interval({
endpoint: "wss://<YOUR INTERVAL SERVER URL>/websocket",
apiKey: "<YOUR API KEY>", // get an API key from the Keys page in your Interval dashboard
routesDirectory: path.resolve(__dirname, "routes"),
});
const server = http.createServer(interval.httpRequestHandler);
server.listen(process.env.PORT);
Any framework that operates with standard Node HTTP handler functions should work, such as express.
import path from "path";
import express from "express";
// Note the experimental import path
import { Interval, io } from "@interval/sdk/dist/experimental";
const interval = new Interval({
endpoint: "wss://<YOUR INTERVAL SERVER URL>/websocket",
apiKey: "<YOUR API KEY>", // get an API key from the Keys page in your Interval dashboard
routesDirectory: path.resolve(__dirname, "routes"),
});
const app = express();
app.use("/interval-endpoint", interval.httpRequestHandler);
app.listen(process.env.PORT);
AWS Lambda functions
If you are targeting AWS Lambda functions, you can pass the request payload
event directly to Interval
's lambdaRequestHandler
method, which returns a
suitable response object. You can perform additional logic before and after if
you like, or you can use the handler directly.
import path from "path";
// Note the experimental import path
import { Interval, io } from "@interval/sdk/dist/experimental";
const interval = new Interval({
endpoint: "wss://<YOUR INTERVAL SERVER URL>/websocket",
apiKey: "<YOUR API KEY>", // get an API key from the Keys page in your Interval dashboard
routesDirectory: path.resolve(__dirname, "routes"),
});
exports.handler = interval.lambdaRequestHandler;
Connecting Interval to your serverless function
Because Interval connects to your serverless functions on-demand, we need to know where to send the requests. After creating and deploying your serverless function, please configure your endpoint's URL on the Serverless endpoints page in the Interval dashboard. After your endpoint is configured, we'll direct requests for new transactions to that endpoint.
We'll also periodically poll your endpoint(s) to monitor availability. Actions defined from your endpoint will be marked as 'Unreachable' if the endpoint is no longer available, or if the endpoint no longer contains that action.