Gregivy / simpleddp

An easy to use DDP client library
MIT License
165 stars 19 forks source link

Empty data on fetch, and onChange does not work either #49

Closed hcurnor closed 3 years ago

hcurnor commented 3 years ago

Sorry for bad english.

I am using this package on my meteor app, and not being able to get the result. I have created a react function component and used this lib.

here's the code:

import React, { useState, useEffect } from "react";

const simpleDDP = require("simpleddp");
import ws from 'isomorphic-ws';

export default function TestPage(props) {
    let ddpServer = null;
    let ddpSubs = [];

    useEffect(() => {
        reactiveDataComputation();

        return () => {
            if (ddpServer) {
                ddpServer.disconnect();
                ddpServer = null
            }
            if (ddpSubs && ddpSubs.length > 0) {
                ddpSubs.forEach(s => {
                    s.remove();
                });
                ddpSubs = [];
            }
        };
    });

    const reactiveDataComputation = async () => {
        // removing old ddpServer
        if (ddpServer) {
            ddpServer.disconnect();
            ddpServer = null;
        }
        // removing old ddpSubs
        if (ddpSubs && ddpSubs.length > 0) {
            ddpSubs.forEach(s => {
                s.remove();
            });
            ddpSubs = [];
        }

        let opts = {
            endpoint: 'ws://localhost:4000/websocket',
            SocketConstructor: ws,
            reconnectInterval: 5000
        };

        ddpServer = new simpleDDP(opts);

        (async () => {
            await ddpServer.connect();
            // connection is ready here

            const sub = ddpServer.subscribe("tasks.goalTasks", 848);
            await sub.ready();

            console.log('ready ', sub.isReady());
            if (sub.isReady()) { 
                //all subs are ready here
                console.log(ddpServer.collection('tasks').fetch()) // returns empty array always
                ddpSubs.push(sub);
            }
        })();

        ddpServer.on('connected', () => {
            console.log('connected') // connected

            ddpServer.collection('tasks').onChange((state) => {
                console.log(state) // no logs
            });
        });
    }

    return (
        <div>Test Page</div>
    )
}

here's the pub i am subscribing to:

Meteor.publish('tasks.goalTasks', function (goal_id) {
  return Tasks.find({ goal_id: goal_id });
});

Can anyone help me with this?

hcurnor commented 3 years ago

nevermind, I got i it working.