Fetching Live Data

Hi,

I am creating the post using LiveCollection amity create and getPost(subscribeCommunityTopic)but it does not show on the feed without reloading the app or refreshing the page. Also, the likes and comments are not being updated immediately. These also update after refreshing the page. Please help me sort these things out as I’m not getting live data while using the liveSubscription.

Best,

Hello @mustafa, please help share your code and your current sdk version. Thank you.

Hi,

I’m using the following code:

const subscribeCommunityTopic = (
    targetType: string,
    targetId: string,
    SubscriptionLevels,
  ) => {
    if (isSubscribed) return;

    if (targetType === 'community') {
      CommunityRepository.getCommunity(targetId, (data) => {
        if (data.data) {
          subscribeTopic(getCommunityTopic(data.data, SubscriptionLevels));
        }
      });
    }
  };

  //getCommunity
  async function getCommunity(id: string) {
    subscribeCommunityTopic('community', id, SubscriptionLevels.POST_AND_COMMENT);

    CommunityRepository.getCommunity(id, ({ data: community }) => {
      if (community) {
        setCommunity(community);
      }
    });
  }

  //Get Suggestion Communities
  async function getTrendingCommunities() {
    CommunityRepository.getRecommendedCommunities(
      { limit: 100 },
      ({ data: communityData }) => {
        if (communityData) {
          // Community data will handle in this block
          setSuggestedCommunities(communityData);
        }
      },
    );
  }
Also, the version is 
6.14.1

Looking forward,

Hello @mustafa , Thank you for your information, I will pass to our team to check on this.

Hello @mustafa , Thank you for sharing the code and version details. To ensure optimal performance and access to the latest features and fixes, we recommend updating your SDK version. You can verify and update to the latest version using this link: https://docs.amity.co/amity-sdk/changelogs-and-versioning/ts.

Hi,

I still have the live data fetching issues even after successfully updating the SDK version by following your provided link. Still, I’ve to reload the app to get likes, etc.
Do you think that something is missing?

Best,

Hello @mustafa , Here’s how you can address the issues you’re experiencing with the live updates for posts, likes, and comments. Please refer to the following code as a guideline:

const subscribePostTopic = useCallback((type: string, id: string) => {
    if (isSubscribed) return;

    if (type === 'user') {
      let user = {} as Amity.User;
      UserRepository.getUser(id, ({ data }) => {
        user = data;
      });
      disposers.push(
        subscribeTopic(getUserTopic(user, SubscriptionLevels.POST), () => {
          // handle errors with event subscription here
        })
      );
      isSubscribed = true;
      return;
    }

    if (type === 'community') {
      CommunityRepository.getCommunity(id, (data) => {
        if (data.data) {
          subscribeTopic(getCommunityTopic(data.data, SubscriptionLevels.POST));
        }
      });
    }
  }, []);

const getFeed = useCallback(() => {
    const unsubscribe = PostRepository.getPosts(
      {
        targetId,
        targetType,
        sortBy: 'lastCreated',
        limit: 10,
        feedType: 'published',
      },
      (data) => {
        setPostData(data);
        subscribePostTopic(targetType, targetId);
      }
    );
    setUnSubPageFunc(() => unsubscribe());
  }, [subscribePostTopic, targetId, targetType]);

If you continue to experience issues, please feel free to reach out with any further questions.

Hi,

It’s okay for live createPost, getPost and like reaction counter but in case of comment count and community following/unfollowing count, it needs to reload or refresh. Please help me to sort out this issue.

Looking for your prompt response,

Hello @mustafa , For the community following/unfollowing count, could you please provide more details regarding the issue and capture a screenshot as well? This will help us better understand and assist you further.

@mustafa For handling the comment count, please follow this code:

import { subscribeTopic, getPostTopic } from "@amityco/ts-sdk";

const getPost = () => {
  const unsubscribePost = PostRepository.getPost(
    "xxxxxxxxxxx", // your postId
    (value) => {
      const { data, loading, error } = value;
      console.log('data: ', data);

      if (data) {
        subscribeTopic(getPostTopic(data));
      }
    }
  );
};

This will allow you to subscribe to the post and monitor changes in the comment count.