@convex-dev/presence

中文文档
0.4.0发布于 2026/07/21Apache-2.0TypeScriptES Module近 30 天下载 55.4万

Convex component for presence functionality.

原始使用文档
文档目录5 节
  1. Installation
  2. Examples
  3. Usage
  4. React Native support
  5. Additional functionality

Presence Convex Component

npm version

A Convex component for managing presence functionality, i.e., a live-updating list of users in a "room" including their status for when they were last online.

Demo of presence component

It can be tricky to implement presence efficiently, without any polling and without re-running queries every time a user sends a heartbeat message. This component implements presence via a single deployment-wide worker (a batch-worker loop) that sleeps until the next session timeout is due, such that clients only receive updates when a user joins or leaves the room and heartbeats don't schedule any work.

The most common use case for this component is via the usePresence hook, which takes care of sending heartbeart messages to the server and gracefully disconnecting a user when the tab is closed.

The Presence Component Video

Installation

npm install @convex-dev/presence

Examples

See the example directory for a simple example of how to use this component. The example-with-auth directory shows how to use the component with authentication.

There's a hosted version of example-with-auth at https://presence.previews.convex.dev.

Usage

First, add the component to your Convex app:

convex/convex.config.ts

import { defineApp } from "convex/server";
import presence from "@convex-dev/presence/convex.config.js";

const app = defineApp();
app.use(presence);
export default app;

convex/presence.ts

import { mutation, query } from "./_generated/server";
import { components } from "./_generated/api";
import { v } from "convex/values";
import { Presence } from "@convex-dev/presence";

export const presence = new Presence(components.presence);

export const heartbeat = mutation({
  args: {
    roomId: v.string(),
    userId: v.string(),
    sessionId: v.string(),
    interval: v.number(),
  },
  handler: async (ctx, { roomId, userId, sessionId, interval }) => {
    // TODO: Add your auth checks here.
    return await presence.heartbeat(ctx, roomId, userId, sessionId, interval);
  },
});

export const list = query({
  args: { roomToken: v.string() },
  handler: async (ctx, { roomToken }) => {
    // Avoid adding per-user reads so all subscriptions can share same cache.
    return await presence.list(ctx, roomToken);
  },
});

export const disconnect = mutation({
  args: { sessionToken: v.string() },
  handler: async (ctx, { sessionToken }) => {
    // Can't check auth here because it's called over http from sendBeacon.
    return await presence.disconnect(ctx, sessionToken);
  },
});

A Presence React component can be instantiated from your client code like this:

src/App.tsx

import { api } from "../convex/_generated/api";
import usePresence from "@convex-dev/presence/react";
import FacePile from "@convex-dev/presence/facepile";

export default function App(): React.ReactElement {
  const [name] = useState(() => "User " + Math.floor(Math.random() * 10000));
  const presenceState = usePresence(api.presence, "my-chat-room", name);

  return (
    <main>
      <FacePile presenceState={presenceState ?? []} />
    </main>
  );
}

This uses the basic FacePile component included with this package but you can easily copy this code and use the usePresence hook directly to implement your own styling.

React Native support

If you're using React Native, install these optional dependencies:

npx expo install react-native expo-crypto

and then import the usePresence hook from @convex-dev/presence/react-native:

import { usePresence } from "@convex-dev/presence/react-native";

Note: The component currently doesn't have a React Native equivalent, but you can easily create your own.

Additional functionality

The component interface for the Presence class is defined in src/client/index.ts. It includes additional functions for maintaining presence state and for querying presence for a given user or room.

e.g., you can use the listUser function to check if a user is online in any room.

Reach out or join the Convex Discord Community if you have any questions or feedback!

版本与发布记录

@convex-dev/presence 共发布 25 个版本,首次发布于 2025/05/28;过去 12 个月发布了 7 个版本。

版本发布日期距今
0.4.0latest2026/07/213 个月前
0.4.0-alpha.2alpha2026/07/193 个月前
0.4.0-alpha.12026/07/173 个月前
0.4.0-alpha.02026/07/173 个月前
0.3.22026/07/074 个月前
0.3.12026/06/174 个月前

关于 @convex-dev/presence 的常见问题

@convex-dev/presence 是什么?

Convex component for presence functionality.

如何安装 @convex-dev/presence?

在项目目录运行 npm install @convex-dev/presence,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(~18.3.1 || ^19.0.0)、convex(^1.36.1)、react-dom(~18.3.1 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@convex-dev/presence 支持 TypeScript 吗?

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

@convex-dev/presence 支持 ESM 还是 CommonJS?

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

@convex-dev/presence 可以商用吗?

@convex-dev/presence 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@convex-dev/presence 的下载量有多少?

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

@convex-dev/presence 还在维护吗?

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