npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

y-presence

v0.2.3

Published

Implement and manage presence/awareness using [Yjs](https://github.com/yjs/yjs) in any React application using two simple React hooks: `useSelf` and `useUsers`.

Downloads

7,710

Readme

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.

Other examples/integrations:

Edit y-presence

Recommended Reading

Usage

Installation

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

Set up a shared Yjs document and connection provider

// 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

// 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 <div>Number of connected users: {users.size}</div>;
}

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:

// Returns a map of the client id to their awareness state and rerenders when any such awareness state changes
const users = useUsers(awareness);
// equivalent to:
const users = useUsers(awareness, (state) => state);
// Map {
//    3965141439 => { name: "John Doe", email: "[email protected]" }
// }

// Returns the number of users connected in the room and rerenders when this number changes
const size = useUsers(awareness, (state) => state.size);
// 1

// Returns the awareness state of the current user (self) and rerenders when this state changes. A simpler/optimized hook for this use case is also provided, and is discussed below:
const self = useUsers(awareness, (state) => state.get(awareness.clientId));
// {
//    name: "John Doe",
//    email: "[email protected]"
// }

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:

// 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