Back

[Solved] Real-time Todo Updates in Appwrite Dashboard - User-Based Subscription Issue

  • 1
  • Databases
  • Web
  • Realtime
Sam
28 May, 2023, 14:17

Hi everyone,

I'm new to Appwrite and currently working on building a Todo web app with user functionality. My goal is to enable real-time updates in the dashboard whenever a user creates a new todo item in their bucket list. Additionally, I want these updates to be reflected across all the devices where the user is logged in.

To achieve this, I'm utilizing the Appwrite real-time subscription feature. Whenever a new document is added to the collection, I subscribe to that specific collection and receive the new data as a payload.

However, I'm encountering a problem where the subscription is triggered for all users whenever a user creates a new document. This means that other users can see the data of the user who created the new document in their logged-in sessions.

Here's the code snippet I'm currently using for the subscription:

TypeScript
const subscribe = useEffect(() => {
  const unSubscribe = client.subscribe(
    "databases.6471bb9750ced865202e.collections.6471e836bd0fed2289ea.documents",
    function (response) {
      console.log("real-time data");
      console.log(response);
      console.log("real-time data");
      const updatedTodo = response.payload;
      const eventTypeList = Array.isArray(response.events)
        ? response.events
        : [response.events];
      if (
        eventTypeList.includes("databases.*.collections.*.documents.*.create") &&
        updatedTodo.userID === user.$id
      ) {
        setTodos((prevTodos) => [...prevTodos, updatedTodo]);
      }
      if (
        eventTypeList.includes("databases.*.collections.*.documents.*.delete") &&
        updatedTodo.userID === user.$id
      ) {
        setTodos((prevTodos) =>
          prevTodos.filter((todo) => todo.$id !== updatedTodo.$id)
        );
      }
    }
  );
  return () => {
    unSubscribe();
  };
}, []);
TL;DR
The user is encountering an issue with real-time updates in their Appwrite Todo web app. They have implemented the subscription feature, but the updates are being received by all users instead of just the user who created the document. The solution to this issue is to give permissions for write on the document level instead of the collection level. Additionally, the code snippet posted shows how they are currently implementing the subscription feature.
Sam
28 May, 2023, 14:17

Is there a way to modify this code to achieve user-based subscriptions? I want the subscription to be specific to the current user, so that only their updates are received and displayed in their own session.

Sam
28 May, 2023, 14:18

You can see in this video to understand the issue more precisely

Sam
28 May, 2023, 14:19
Drake
29 May, 2023, 07:26

What permissions do you have on your collection?

Sam
29 May, 2023, 07:47

I solved this issue, we should give the permissions for write in document level instead of collection level.

Sam
29 May, 2023, 07:47

[Solved] Real-time Todo Updates in Appwrite Dashboard - User-Based Subscription Issue

Lawrence franklin
4 Jun, 2023, 16:22

Hi @Sam, I also have an issue quite related. When I subscribe to the documents channel, I get a response when an event happens in the documents but my application still doesn’t update in real time. I’m unsure to make the update happen in real time. Any help?

Drake
4 Jun, 2023, 16:24

It would be best to create a new post in <#1072905050399191082> and include your code

Lawrence franklin
4 Jun, 2023, 16:35

Okay thanks

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