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

@hermes/react

v0.1.0

Published

Hermes is a real-time data framework for MongoDB + React. It uses native MongoDB change streams and WebSockets to ensure that the state of your React app reflects the content of your database in real-time.

Downloads

6

Readme

@hermes/react

Hermes is a real-time data framework for MongoDB + React. It uses native MongoDB change streams and WebSockets to ensure that the state of your React app reflects the content of your database in real-time.

Hermes comes in 2 parts:

  • @hermes/server: creates MongoDB change stream listeners and handles subscriptions from React.
  • @hermes/react: provides a useHermes hook to access real-time data.

React

Install with yarn add @hermes/react or npm install @hermes/react.

HermesProvider

To use Hermes in your React application, you will first need to wrap your application with the provider component HermesProvider. This should go somewhere at the root of your application structure.

The only prop that the provider requires is url, the WebSocket endpoint of your Hermes server.

import React from "react";
import ReactDOM from "react-dom/client";
import { HermesProvider } from "@hermes/react";
import App from "./App";

const root = ReactDOM.createRoot(document.getElementById("root"));

root.render(
  <HermesProvider url="wss://example.com:9000">
    <App />
  </HermesProvider>
);

useHermes

The useHermes hook allows you to access real-time data as it appears in your MongoDB database. It can only be used in a component that is a child of the HermesProvider.

The most basic usage is to just pass the name of the collection you want to return real-time documents for.

import React from "react";
import { useHermes } from "@hermes/react";

const Component = () => {
  const users = useHermes("users");

  return (
    <pre>
      {JSON.stringify(users, null, 2)}
    </pre>
  );
}

In the background, Hermes will handle registering the hook with the provider, sending a 'subscribe' message to the server, and receiving documents and changes as they occur in real-time. When the component is unmounted, the hook will be de-registered. If there are no remaining registered hooks for a specific collection, that collection will also be unsubscribed from.

MongoDB query pipelines

As a second argument, the useHermes hook can take a MongoDB aggregate pipeline. This allows you filter (and perform other operations on) the documents that are returned to the client and updated in real-time.

For example: to filter, sort, and return only 10 documents from the "users" collection, you could use:

const users = useHermes("users", [
  {
    $match: { verified: true }
  },
  {
    $sort: { createdAt: -1 }
  },
  {
    $limit: 10
  }
]);

Only the resulting documents will be returned by the useHermes hook. Importantly, real-time updates will only be sent and the component using the hook will only re-render when the returned documents change; any other changes within the same collection will be ignored.

You can have multiple hooks subscribed to the same collection using different queries. If 2 hooks return some identical documents, they will be de-duped to reduce the size of the client-side document storage.

useHermesState

The useHermesState hook returns some potentially useful information around the internal state of the Hermes provider.

import React from "react";
import { useHermesState } from "@hermes/react";

const Component = () => {
  const { url, connected, clientId, subscriptions } = useHermesState();
  
  ...
}

| Key | Description | |-----------------|----------------------------------------------------------------------| | url | The URL of the Hermes server | | connected | Boolean connected status | | clientId | The UUID of the current client | | subscriptions | A map of MongoDB collections containing subscription status for each |