Skip to content

feat(new-webui): Add client library for real-time MongoDB updates.#892

Merged
davemarco merged 15 commits into
y-scope:mainfrom
davemarco:client
May 14, 2025
Merged

feat(new-webui): Add client library for real-time MongoDB updates.#892
davemarco merged 15 commits into
y-scope:mainfrom
davemarco:client

Conversation

@davemarco

@davemarco davemarco commented May 9, 2025

Copy link
Copy Markdown
Contributor

Description

This PR is a refactor of #841
PR adds a custom hook to front end which connects to new fastify mongoDB query service #880.

The custom hook (useCursor) should requery, when any of the dependencies have changed. It is meant to be a drop-in replacement for meteor useTracker.

There are some minor server changes as well. Server changes are to prevent, rare, but potential bugs.

Checklist

  • The PR satisfies the contribution guidelines.
  • This is a breaking change and that has been indicated in the PR title, OR this isn't a
    breaking change.
  • Necessary docs have been updated, OR no docs need to be updated.

Validation performed

Replaced app with a new testing component (code below). New component can modify queries with buttons,
testing how it would actually work in new webui (say a different query by user). Subscription and unsubscription worked as expected.

import {useState} from "react";

import MongoCollectionSocket from "./api/socket/MongoCollectionSocket";
import {useCursor} from "./api/socket/useCursor";


/**
 * Test1
 *
 * @param root0
 * @param root0.collectionName
 * @return
 */
const Results1 = ({collectionName = "compression-jobs"}: {collectionName?: string}) => {
    const singleResult = useCursor(
        () => new MongoCollectionSocket(collectionName).find({}, {}),
        [collectionName]
    );

    return (
        <div>
            {singleResult.map((r, index) => (
                <div key={index}>
                    {JSON.stringify(r)}
                </div>
            ))}
        </div>
    );
};

/**
 * Test2
 *
 * @param root0
 * @param root0.collectionName
 * @param root0.limit
 * @return
 */
const Results2 = ({collectionName = "stats", limit = 5}: {collectionName?: string; limit?: number}) => {
    const singleResult = useCursor(
        () => new MongoCollectionSocket(collectionName).find({}, {limit}),
        [collectionName,
            limit]
    );

    return (
        <div>
            {singleResult.map((r, index) => (
                <div key={index}>
                    {JSON.stringify(r)}
                </div>
            ))}
        </div>
    );
};

/**
 *
 */
const App = () => {
    const [show, setShow] = useState(true);
    const [collection1, setCollection1] = useState("compression-jobs");
    const [limit, setLimit] = useState(5);

    const toggleCollection = () => {
        setCollection1("compression-jobs" === collection1 ?
            "stats" :
            "compression-jobs");
    };

    const toggleLimit = () => {
        setLimit(5 === limit ?
            1 :
            5);
    };

    return (
        <div>
            <div>
                <button
                    onClick={() => {
                        setShow(!show);
                    }}
                >
                    Toggle
                </button>
                <button onClick={toggleCollection}>
                    Switch Collection:
                    {" "}
                    {collection1}
                </button>
                {show && <Results1 collectionName={collection1}/>}
            </div>

            <p>------------------------</p>

            <div>
                <button
                    onClick={() => {
                        setShow(!show);
                    }}
                >
                    Toggle
                </button>
                <button onClick={toggleLimit}>
                    Set Limit:
                    {" "}
                    {limit}
                </button>
                {show && <Results2
                    collectionName={"stats"}
                    limit={limit}/>}
            </div>

            <p>------------------------</p>
        </div>
    );
};

export default App;

Summary by CodeRabbit

  • New Features
    • Introduced real-time MongoDB collection and cursor support over sockets for live data updates.
    • Added a React hook to subscribe to real-time MongoDB data streams.
    • Added a shared singleton socket connection for efficient WebSocket communication.
  • Configuration
    • Enhanced TypeScript and Vite configurations for improved path aliasing and WebSocket proxy support.
  • Bug Fixes
    • Improved error handling and streamlined event handling for MongoDB socket events.
  • Chores
    • Updated dependencies to support new socket and path alias features.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants