Back

Not recieving realtime update

  • 1
  • Web
Samir_k
16 Jul, 2024, 07:00

So, i am trying to make a chat application but i am not recieving the realtime response and not even the error. I am give any permission to my collection to create, read, update and delete. realTime(callback) { console.log("Subscribing to real-time updates"); this.unsubscribeFn = this.client.subscribe( // returns a function databases.${conf.appwriteDatabaseId}.collections.${conf.appwriteCollectionId}.documents, (response) => { if (response.error) { console.error("Realtime error:", response.error); } else { callback(response); } }, ); }

ussage: useEffect(() => { const fetchdata = async () => { try { const response = await dbService.getPosts(); //promise dispatch(intialMsg(response.documents)); // array // console.log(response.documents); // console.log(ans); } catch (error) { console.log("error fetching"); } }; fetchdata();

TypeScript
const handleRealtimeUpdate = (res) => {
  console.log("Real-time update received", res);
  // Handle different types of events
  if (res.events.includes("databases.*.collections.*.documents.*.create")) {
    console.log("created");
    dispatch(addMsg(res.payload));
  } else if (
    res.events.includes("databases.*.collections.*.documents.*.delete")
  ) {
    console.log("deleted");
    dispatch(rmMsg(res.payload.$id));
  } else if (
    res.events.includes("databases.*.collections.*.documents.*.update")
  ) {
    console.log("update");
    dispatch(updateMsg(res.payload));
  }
};
dbService.realTime(handleRealtimeUpdate);
return () => {
  dbService.unsubscribe(); // to stop double render
};

}, [dispatch]); // show messages after dom renders

TL;DR
Developers are not receiving real-time updates in their chat application despite setting permissions correctly. The issue lies in the `handleRealtimeUpdate` function checking for events incorrectly. To fix this, modify the event checking logic to target specific collection and document actions correctly.
Samir_k
16 Jul, 2024, 07:02

useeffect code

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more