Back

Realtime feature not working

  • 0
  • Web
  • Realtime
doomer
11 Mar, 2025, 20:36

Can anyone explain why the realtime feature not working here:

TypeScript
const initializeAppwrite = async () => {
      // Only run in browser environment

      try {
        const { client, databases } = await createClient(sessionSecret);
        if (!client || !databases) return;

        setClient(client);
        setDatabases(databases);

        // Fetch conversations
        const response = await databases.listDocuments(
          DATABASE_ID,
          CONVERSATION_COLLECTION,
          [
            Query.or([
              Query.equal("clientId", currentUserId),
              Query.equal("freelancerId", currentUserId),
            ]),
            Query.orderDesc("$updatedAt"),
          ]
        );
/*Skipped code*/
unsubscribe = client.subscribe(
          `databases.${DATABASE_ID}.collections.${CONVERSATION_COLLECTION}.documents`,
          (response) => {
            // Handle realtime updates
            if (
              response.events.includes(
                "databases.*.collections.*.documents.*.create"
              )
            ) {
              enhanceConversation(response.payload).then((enhancedConv) => {
                setConversations((prev) => [enhancedConv, ...prev]);
              });
            }
            if (
              response.events.includes(
                "databases.*.collections.*.documents.*.update"
              )
            ) {
              console.log(response.payload);
              enhanceConversation(response.payload).then((enhancedConv) => {
                setConversations((prev) => [
                  enhancedConv,
                  ...prev.filter((conv) => conv.$id !== enhancedConv.$id),
                ]);
              });
            }
          }
        );

        setLoading(false);
        return () => {
          unsubscribe();
        };
      } catch (error) {
        setError(error.message);
        setLoading(false);
      }
    };

    initializeAppwrite();
  }, [sessionSecret, currentUserId]);
TL;DR
Realtime feature is not working, and the error message displayed is "Realtime got disconnected. Reconnect will be attempted in 1 second." The issue might be due to the subscription part of the code. Make sure the code for handling "create" and "update" events is correctly structured within the subscription logic.
Kenny
11 Mar, 2025, 20:50

Are you getting any errors?

doomer
11 Mar, 2025, 20:53

Realtime got disconnected. Reconnect will be attempted in 1 seconds. <empty string>

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