@camunda/session-heartbeat

中文文档
0.0.1发布于 2026/08/14LicenseRef-Camunda-1.0TypeScriptES Module零依赖近 30 天下载 7525

Activity-driven session heartbeat for Camunda webapps backed by the Camunda security library

原始使用文档
文档目录9 节
  1. Why a heartbeat is needed
  2. Installation
  3. Usage
  4. React
  5. Without React
  6. Options
  7. Choosing intervalMs
  8. Behavior worth knowing
  9. Development

@camunda/session-heartbeat

Activity-driven session heartbeat for Camunda webapps whose sessions are managed by the Camunda security library (CSL).

The package tracks genuine browser activity — pointer, keyboard, wheel, scroll, and the tab regaining visibility — and calls CSL's POST {basePath}/session/heartbeat endpoint at most once per interval, only when activity actually happened. It exists so Operate, Tasklist, Optimize, and any future adopter share one implementation instead of each reimplementing the same listener and throttle logic.

Background: CSL ADR-0042 — Configurable session idle timeout driven by client activity.

Why a heartbeat is needed

With camunda.security.session.heartbeat.enabled=true, CSL stops treating ordinary backend traffic as proof of user presence: only a call to the heartbeat endpoint extends the session. A host that enables the flag without a frontend sending heartbeats logs its users out after camunda.security.session.max-inactive-interval, however actively they are using the application.

The flag defaults to false, so adopting this package is safe and inert until a host turns it on.

Installation

npm install @camunda/session-heartbeat

react is an optional peer dependency, needed only for the ./react entry point.

Usage

React

import {useSessionHeartbeat} from '@camunda/session-heartbeat/react';

function AuthenticatedLayout() {
    useSessionHeartbeat({
        url: '/session/heartbeat',
        csrfToken: () => sessionStorage.getItem('X-CSRF-TOKEN'),
        onUnauthorized: () => {
            authenticationStore.disableSession();
        },
    });

    return <Outlet />;
}

Call it once inside the authenticated part of the application, so the heartbeat starts after login and stops when the user leaves it. Callbacks and csrfToken are read fresh on every heartbeat, so inline functions do not restart the timer; changing url, intervalMs, or enabled does.

Without React

import {createSessionHeartbeat} from '@camunda/session-heartbeat';

const stopSessionHeartbeat = createSessionHeartbeat({url: '/session/heartbeat'});

// on teardown
stopSessionHeartbeat();

Options

OptionTypeDefaultDescription
urlstring—The heartbeat endpoint. Must be the current scope's {basePath}/session/heartbeat, context path included.
intervalMsnumber60000How often activity is checked, and therefore the shortest gap between two heartbeats.
csrfTokenstring | null | undefined | (() => …)undefinedCSRF token, or a getter for it. Sent as X-CSRF-TOKEN; omitted when absent or empty.
onUnauthorized() => voidundefinedCalled when a heartbeat comes back 401 — an expired session, or a missing/stale CSRF token.
onError(failure: SessionHeartbeatFailure) => voidundefinedCalled for network errors and for any other non-OK response.
enabledboolean (./react only)trueSet false to keep the hook mounted without sending heartbeats.

Choosing intervalMs

Keep it well under the host's camunda.security.session.max-inactive-interval — a heartbeat is what resets that clock, and one lost request must not cost the user their session. A quarter of the configured interval or less is a good rule of thumb; the 60s default suits CSL's 30m default.

Behavior worth knowing

  • The endpoint needs a CSRF token. CSL exempts only /login and /logout from CSRF protection, so a session-bearing POST /session/heartbeat without a valid X-CSRF-TOKEN is rejected — with 401, not 403, since the webapp chain maps the CSRF denial onto its auth-failure handler. Pass csrfToken from wherever the application keeps it.
  • A 401 therefore means "this heartbeat was not accepted", not strictly "the session is gone". A missing or stale token produces the same status as an expired session, so onUnauthorized fires in both cases. That matches how a webapp's own request layer usually treats 401; if an application needs to tell the two apart, the response body carries a CSRF-specific detail.
  • Starting counts as activity, so the first interval always sends one heartbeat. This surfaces broken wiring immediately, at the cost of extending an abandoned session by one interval.
  • At most one heartbeat per interval, and none at all for an interval with no activity. A heartbeat is also skipped while a previous one is still in flight.
  • A 401 does not stop the heartbeat. The consumer decides what an expired session means; unmounting the hook (or setting enabled: false) is what stops it.
  • Each tab heartbeats independently. They share one session, so the cost is one extra request per tab per interval; there is no cross-tab coordination.
  • Requests are credentialed (credentials: 'include') and aborted on teardown.

Development

npm run build -w @camunda/session-heartbeat        # bundle + type declarations
npm run typecheck -w @camunda/session-heartbeat
npm run test:unit -w @camunda/session-heartbeat    # Vitest browser mode

See Session heartbeat in the monorepo docs for the adoption checklist and publishing steps.

版本与发布记录

@camunda/session-heartbeat 共发布 1 个版本,首次发布于 2026/08/14;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.0.1latest2026/08/142 个月前

关于 @camunda/session-heartbeat 的常见问题

@camunda/session-heartbeat 是什么?

Activity-driven session heartbeat for Camunda webapps backed by the Camunda security library

如何安装 @camunda/session-heartbeat?

在项目目录运行 npm install @camunda/session-heartbeat,或使用 pnpm add / yarn add / bun add。

@camunda/session-heartbeat 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@camunda/session-heartbeat 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@camunda/session-heartbeat 可以商用吗?

@camunda/session-heartbeat 使用 LicenseRef-Camunda-1.0 许可。声明的许可为 LicenseRef-Camunda-1.0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@camunda/session-heartbeat 的下载量有多少?

根据 npm 官方下载统计,@camunda/session-heartbeat 在 2026/10/09 – 2026/11/07 这 30 天内被下载 7,525 次,日均约 251 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@camunda/session-heartbeat 还在维护吗?

最新版本 0.0.1 发布于 2026/08/14(2 个月前);至今共发布 1 个版本,其中过去 12 个月发布了 1 个;首次发布于 2026/08/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。