Interval
The Interval
class is the entrypoint for defining
actions and pages.
Usage
- TypeScript
- JavaScript
import path from "path";
import { Interval } from "@interval/sdk";
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"),
});
interval.listen();
const path = require("path");
const { Interval } = require("@interval/sdk");
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"),
});
interval.listen();
Props
- TypeScript
- JavaScript
apiKey
Required
string
endpoint
Required
string
routesDirectory
Optional
string
routes
Optional
Record<string, Action | Page>
logLevel
Optional
"quiet" | "info" | "debug"
sendTimeoutMs
Optional
number
retryIntervalMs
Optional
number
maxResendAttempts
Optional
number
pingIntervalMs
Optional
number
pingTimeoutMs
Optional
number
closeUnresponsiveTimeoutMs
Optional
number
reinitializeBatchTimeoutMs
Optional
number
onError
Optional
(props: IntervalErrorProps) => void
(props: {
error: Error | unknown
route: string
routeDefinition: Action | Page | undefined
params: Record<string, string | number | boolean | null | undefined | Date | bigint>
environment: 'production' | 'development' | string
user: {
email: string
firstName: string | null
lastName: string | null
}
organization: {
name: string
slug: string
}
}) => void
apiKey
Required
string
endpoint
Required
string
routesDirectory
Optional
string
routes
Optional
Record<string, Action | Page>
logLevel
Optional
"quiet" | "info" | "debug"
sendTimeoutMs
Optional
number
retryIntervalMs
Optional
number
maxResendAttempts
Optional
number
pingIntervalMs
Optional
number
pingTimeoutMs
Optional
number
closeUnresponsiveTimeoutMs
Optional
number
reinitializeBatchTimeoutMs
Optional
number
onError
Optional
(props: IntervalErrorProps) => void
(props: {
error: Error | unknown
route: string
routeDefinition: Action | Page | undefined
params: Record<string, string | number | boolean | null | undefined | Date | bigint>
environment: 'production' | 'development' | string
user: {
email: string
firstName: string | null
lastName: string | null
}
organization: {
name: string
slug: string
}
}) => void
Methods
- TypeScript
- JavaScript
listen()
async ping()
async safelyClose()
immediatelyClose()
async enqueue(slug, { assignee, params })
async (
slug: string,
options?: {
assignee?: string,
params?: Record<string, string | number | boolean | Date | null | undefined>
}
) => Promise<{
id: string;
assignee?: string;
params?: Record<string, string | number | boolean | Date | null | undefined>;
}>
async dequeue(queuedActionId)
async (
queuedActionId: string
) => Promise<void>
async notify(config)
async ({
message: string,
title?: string,
delivery?: {
to: string;
method?: 'SLACK' | 'EMAIL';
}[],
transactionId?: string,
idempotencyKey?: string,
}) => Promise<void>
listen()
async ping()
async safelyClose()
immediatelyClose()
async enqueue(slug, { assignee, params })
async (
slug: string,
options?: {
assignee?: string,
params?: Record<string, string | number | boolean | Date | null | undefined>
}
) => Promise<{
id: string;
assignee?: string;
params?: Record<string, string | number | boolean | Date | null | undefined>;
}>
async dequeue(queuedActionId)
async (
queuedActionId: string
) => Promise<void>
async notify(config)
async ({
message: string,
title?: string,
delivery?: {
to: string;
method?: 'SLACK' | 'EMAIL';
}[],
transactionId?: string,
idempotencyKey?: string,
}) => Promise<void>
Examples
Safely shutting down a live deployment (blue-green deploys)
- TypeScript
- JavaScript
Leverage the safelyClose
method to ensure there are no service
interruptions, lost progress, or malformed data when shutting down a previous
version of a live deployment.
import path from "path";
import { Interval } from "@interval/sdk";
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"),
});
interval.listen();
process.on("SIGINT", () => {
interval
.safelyClose()
.then(() => {
console.log("Safely shut down successfully.");
process.exit(0);
})
.catch(err => {
console.error(
"Failed shutting down safely, forcibly closing connection."
);
interval.immediatelyClose();
process.exit(0);
});
});
Leverage the safelyClose
method to ensure there are no service
interruptions when shutting down a previous version of a live
deployment.
const path = require("path");
const { Interval } = require("@interval/sdk");
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"),
});
interval.listen();
process.on("SIGINT", () => {
interval
.safelyClose()
.then(() => {
console.log("Safely shut down successfully.");
process.exit(0);
})
.catch(err => {
console.error(
"Failed shutting down safely, forcibly closing connection."
);
interval.immediatelyClose();
process.exit(0);
});
});