Skip to main content

Configuring Cerebellum

When creating a new instance of Cerebellum, you can pass in an options object to customize the behavior of the Cerebellum instance. Below is a detailed explanation of the available options and their descriptions.

const CerebellumOptions = {
  autoConnect: true,
  API_KEY: "SAMPLE_API_KEY",
  authRoute: {
    endpoint: "http://localhost:3000/login",
    method: "POST",
    payload: {},
},
  reconnection: true,
  reconnectionAttempts: 5,
  reconnectionDelay: 5000,
  reconnectionDelayMax: 5000,
  timeout: 20000,
};

autoConnect

autoConnect: boolean;

Determines whether the CerebellumInit instance should automatically connect to the Cerebellum server.

  • If true:   - The instance will first check if an API_KEY is provided. If an API_KEY is present, it will create a token using this key before establishing the WebSocket connection.   - If the API_KEY is not provided, the authRoute information will be used to retrieve a token signed by the API_KEY. After obtaining the token from the auth server, CerebellumInit will attempt to establish a WebSocket connection.   - This approach avoids storing the API_KEY directly.
  • If false, the instance will not automatically connect.

Note: If both API_KEY and authRoute are provided, API_KEY takes precedence.


API_KEY

API_KEY: string;

The API key for authentication. This key is used to create a token on the front end, which is then sent to the Cerebellum servers for authentication.

If the API_KEY is provided, then a token using the provided API_KEY will be created automatically.

For local development and testing, you can use the following, when using the cerebellum development server/image

API_KEY: "SAMPLE_API_KEY";

In production environments, ensure that this key is kept secure and not exposed in client-side code. We strongly recommend using the authRoute if the autoConnect is set to true, or using the provided authentication methods discussed in the section.


authRoute

authRoute: {
  endpoint: string;
  method: "POST" | "GET";
  payload?: object;
}

An object containing details for an authentication route. Used if autoConnect is true and API_KEY is not provided, else it is ignored.

When autoconnect is true, and API_KEY is not provided, cerebellum will attempt to make an HTTP request to the endpoint using the method provided and include the payload in the request.

It expects to receive a response with a JSON web token in the following format. You can use the createToken from the @cerebellum/sdk to create a token on your authentication server.

  • endpoint:
    • Type: string
    • Description: The endpoint from which to receive the Cerebellum token.
  • method:
    • Type: "POST" | "GET"
    • Description: The HTTP method to use when requesting the Cerebellum token.
  • payload:
    • Type: object
    • Description: An optional payload to send to the endpoint.

reconnection

reconnection: boolean;

Enables automatic reconnection attempts if the connection to the server is lost.

reconnectionAttempts

reconnectionAttempts: number;

The number of reconnection attempts to make before giving up.


reconnectionDelay

reconnectionDelay: number;

The delay (in milliseconds) between reconnection attempts.


reconnectionDelayMax

reconnectionDelayMax: number;

The maximum delay (in milliseconds) between reconnection attempts.


timeout

timeout: number;

The timeout (in milliseconds) before a connection attempt is considered failed.