百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
Y

y-presence

> 编程语言
开源

使用 Yjs 来管理多玩家在线状态的简单 react 钩子

195 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

使用 Yjs 来管理多玩家在线状态的简单 react 钩子

y-presence

Implement and manage presence/awareness using Yjs in any React application using two simple React hooks: useSelf and useUsers.

Codesandbox demo/examples

For all the demos, you can open a new tab on your browser to observe how the presence updates in each example.

  • Multiplayer avatars: Demo | Code
  • Multiplayer cursors: Demo | Code
  • Simple room: Demo | Code

Other examples/integrations:

  • perfect-cursors: Demo | Code

Recommended Reading

  • API documentation of the Awareness CRDT

Usage

Installation

bash
yarn add y-presence
# or
npm i y-presence

Set up a shared Yjs document and connection provider

typescript
// src/store.ts
import { WebsocketProvider } from "y-websocket";
import { Doc } from "yjs";

// Create the shared doc (from Yjs)
const doc = new Doc();

// Create a provider
const provider = new WebsocketProvider(
  "wss://demos.yjs.dev",
  "y-presence-demo",
  doc
);

// Get the provider's awareness API
export const awareness = provider.awareness;

// Set the local awareness state
awareness.setLocalState({ name: "John Doe", email: "[email protected]" });

Manage the presence/awareness state in React components

typescript
// src/App.tsx

import { useUsers } from "y-presence";
import { awareness } from "./store.ts";

export default function App() {
  // Fetch all users connected in the room
  const users = useSelf(awareness);

  return 
;
}

Hooks

useUsers

The useUsers hook subscribes to updates to the awareness states of all users connected in the room. It accepts three arguments:

  1. An awareness object returned by connection provider.
  2. (Optional) A selector function that accepts a map of the awareness states and enables selecting a subset of this map. This signals React to rerender the component only when this subset has changed.
  3. (Optional) A equality function to detect if the selected subset has changed.

Example Usage:

…

useSelf

The useSelf hook subscribes to updates to the awareness state of the current user (self) in the room. It accepts three arguments:

  1. An awareness object returned by connection provider.
  2. (Optional) A selector function that accepts an awareness state object and enables selecting a subset of this object. This signals React to rerender the component only when this subset has changed.
  3. (Optional) A equality function to detect if the selected subset has changed.

Example Usage:

typescript
// Returns the awareness state of the current user (self) and rerenders when this state changes.
const self = useSelf(awareness);
// is equivalent to:
const self = useSelf(awareness, (state) => state);
// {
//    name: "John Doe",
//    email: "[email protected]"
// }

// Returns the value of the property `name` of the current user's awareness state and rerenders when this value changes
const name = useSelf(awareness, (state) => state?.name);
// "John Doe"

License

This project is licensed under MIT.

Credits

The two hooks are inspired by the Liveblocks' Presence hooks. Check out their website and documentation to learn more about their presence/awareness implementation.

Author

  • @nayajunimesh

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptmultiplayerpresenceyjs

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言