Back

On fast refresh realtime subscription created multiple time but never closed.

  • 0
  • Self Hosted
  • Realtime
  • Web
debug
6 Mar, 2025, 16:51

I'm using realtime subscription in a zustand store like this : ` import { Databases, Query } from "appwrite"; import { create } from "zustand"; import { DATABASE_ID } from "../models/database"; import { PLAYOUT_STATUS_COLLECTION_ID, PlayoutStatus } from "../models/playoutStatus"; import { Client } from "../utils/client";

const databases = new Databases(Client);

export const usePlayoutStatusStore = create<PlayoutStatusState>((set) => ({ [...] subscribeToRealtime: (containerId: string) => { set((state) => { if (state.realtimeSubscription) return state;

TypeScript
  const subscription = Client.subscribe(
    `databases.${DATABASE_ID}.collections.${PLAYOUT_STATUS_COLLECTION_ID}.documents`,
    (response) => {
      const events: string[] = response.events;
      const updatedStatus = response.payload as PlayoutStatus;

      if (updatedStatus.containerId !== containerId) return;

      if (events.some((evt) => evt.includes("create") || evt.includes("update"))) {
        set({ status: updatedStatus });
      }
      if (events.some((evt) => evt.includes("delete"))) {
        set({ status: null });
      }
    }
  );
  return { realtimeSubscription: subscription };
});

}, unsubscribeFromRealtime: () => { set((state) => { if (state.realtimeSubscription) { state.realtimeSubscription(); } return { realtimeSubscription: null }; }); } })); ` but when there multiple refresh in a short time it create multiple subscription for the same client and its impossible to close the old one without making a force restart of the appwrite-realtime via docker restart appwrite-realtime.

so there is orphan connection that can be seen in the dashboard. is there a way to solve this ?

TL;DR
Developers are using a realtime subscription in a zustand store, but multiple subscriptions are created and not closed properly, leading to orphan connections in the dashboard. To solve this issue, developers should ensure they only create a new subscription if one doesn't already exist, and properly close the existing subscription before creating a new one. This can prevent the accumulation of open connections and avoid the need for a forced restart of appwrite-realtime.
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